private void OnDownloadCompleted(bool hasError, string filePath, ActionFileDownload action, object data, bool isSlient)
        {
            var fileSaved = !hasError;

            if (hasError)
            {
                if (!isSlient)
                {
                    UserDialogs.Instance.Alert("Download Error");
                }
            }
            else
            {
                // TODO do open file here
                switch (action)
                {
                case ActionFileDownload.Open:
                    FileService.OpenFile(filePath);
                    break;

                case ActionFileDownload.Download:
                    UserDialogs.Instance.Toast("File is Downloaded");
                    var notification = new Plugin.LocalNotification.NotificationRequest
                    {
                        NotificationId = 101,
                        Title          = "An item has downloaded ",
                        Description    = "Click to view more detail",
                        ReturningData  = filePath
                    };
                    NotificationCenter.Current.Show(notification);
                    break;
                }
            }

            if (OnFileDownloaded != null)
            {
                OnFileDownloaded.Invoke(this, new DownloadEventArgs(fileSaved, filePath, data));
            }
        }
        public async Task <string> DownloadFile(string filePath, string folder, object data = null, string fileName = null, bool stillDownloadIfExist = true, bool isDownloadFolder = true, ActionFileDownload action = ActionFileDownload.Download, string fileLocalPath = null, bool isSlient = false)
        {
            bool permission = await PermissionService.RequestExternalPermssion();

            if (!permission)
            {
                throw new Exception("Permission Denied. Can not continue, please try again.");
            }

            if (action == ActionFileDownload.Download)
            {
                RemoveAllTappEvents();
                NotificationCenter.Current.NotificationTapped += OpenFile;
                openFilesDelegate.Add(OpenFile);
            }

            // if load file from local
            if (!string.IsNullOrEmpty(fileLocalPath))
            {
                OnDownloadCompleted(false, fileLocalPath, action, data, isSlient);
                return(fileLocalPath);
            }

            string downloadFolder = folder;

            if (isDownloadFolder) // if download folder is parent
            {
                downloadFolder = Path.Combine(FileService.GetDownloadFolder(), folder);
            }

            try
            {
                if (!Directory.Exists(downloadFolder))
                {
                    Directory.CreateDirectory(downloadFolder);
                }
                using (var webClient = new CustomWebClient())
                {
                    webClient.Action = action;
                    webClient.Data   = data;
                    webClient.Headers.Add("Authorization", "Bearer " + BaseService.AccessToken);
                    using (webClient.OpenRead(filePath))
                    {
                        TotalByDownload = Convert.ToInt64(webClient.ResponseHeaders["Content-Length"]);
                    }
                    webClient.DownloadFileCompleted   += new AsyncCompletedEventHandler(Completed);
                    webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged);

                    var fullPath = Path.Combine(downloadFolder, Path.GetFileName(filePath));
                    if (fileName != null)
                    {
                        fullPath = $"{ downloadFolder }/{fileName}";
                    }

                    string fName = AppendFileNumberIfExists(fullPath, "", stillDownloadIfExist);
                    webClient.FilePath = Path.Combine(downloadFolder, Path.GetFileName(fName));

                    // just download if file not exist
                    if (!File.Exists(webClient.FilePath))
                    {
                        await webClient.DownloadFileTaskAsync(new Uri(filePath), webClient.FilePath);
                    }
                    else
                    {
                        // download successfully
                        OnDownloadCompleted(false, webClient.FilePath, action, data, isSlient);
                    }
                    return(webClient.FilePath);
                }
            }
#pragma warning disable CA1031 // Do not catch general exception types
            catch (Exception ex)
            {
                OnDownloadCompleted(true, string.Empty, action, data, isSlient);
                return(string.Empty);
            }
#pragma warning restore CA1031 // Do not catch general exception types
        }