/*private void BtnChooseFolder_Click(object sender, RoutedEventArgs e)
         * {
         *  System.Windows.Forms.FolderBrowserDialog dialog = new System.Windows.Forms.FolderBrowserDialog();
         *
         *  if (thisSeries!= null && !string.IsNullOrEmpty(thisSeries.DefaultFolder) && Directory.Exists(thisSeries.DefaultFolder))
         *      dialog.SelectedPath = thisSeries.DefaultFolder;
         *
         *  if (thisSeries != null && dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
         *  {
         *      thisSeries.DefaultFolder = dialog.SelectedPath;
         *      thisSeries.Save();
         *      RefreshRecords();
         *  }
         * }*/

        void AnimeFolderSummaryControl_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            try
            {
                if (DataContext == null)
                {
                    AnimeFolderSummaryRecords.Clear();
                    return;
                }

                RefreshRecords();
            }
            catch (Exception ex)
            {
                Utils.ShowErrorMessage(ex);
            }
        }
        private void RefreshRecords()
        {
            try
            {
                IsDataLoading         = true;
                IsDataFinishedLoading = false;

                AnimeFolderSummaryRecords.Clear();

                TotalFileCount = 0;
                double fileSize = 0;


                VM_AniDB_Anime anime = DataContext as VM_AniDB_Anime;
                if (anime == null)
                {
                    return;
                }

                VM_AnimeSeries_User serie = (VM_AnimeSeries_User)VM_ShokoServer.Instance.ShokoServices.GetSeriesForAnime(anime.AnimeID,
                                                                                                                         VM_ShokoServer.Instance.CurrentUser.JMMUserID);

                if (serie == null)
                {
                    return;
                }
                thisSeries = serie;

                Dictionary <string, AnimeFolderSummary> folders = new Dictionary <string, AnimeFolderSummary>();

                foreach (VM_VideoLocal vid in anime.AllVideoLocals)
                {
                    TotalFileCount++;
                    fileSize += (double)vid.FileSize;
                    foreach (CL_VideoLocal_Place vplace in vid.Places)
                    {
                        if (!folders.ContainsKey(vplace.GetFileDirectory()))
                        {
                            AnimeFolderSummary fs = new AnimeFolderSummary();
                            fs.FolderName    = vplace.GetFileDirectory();
                            fs.FileCount     = 0;
                            fs.TotalFileSize = 0;
                            folders[vplace.GetFileDirectory()] = fs;
                        }

                        folders[vplace.GetFileDirectory()].FileCount     = folders[vplace.GetFileDirectory()].FileCount + 1;
                        folders[vplace.GetFileDirectory()].TotalFileSize = folders[vplace.GetFileDirectory()].TotalFileSize +
                                                                           vid.FileSize;
                    }
                }

                foreach (AnimeFolderSummary afs in folders.Values)
                {
                    afs.IsDefaultFolder = false;

                    if (!string.IsNullOrEmpty(thisSeries.DefaultFolder))
                    {
                        if (thisSeries.DefaultFolder.Equals(afs.FolderName, StringComparison.InvariantCultureIgnoreCase))
                        {
                            afs.IsDefaultFolder = true;
                        }
                    }
                }
                AnimeFolderSummaryRecords.ReplaceRange(folders.Values);
                TotalFileSize         = Formatting.FormatFileSize(fileSize);
                IsDataLoading         = false;
                IsDataFinishedLoading = true;
            }
            catch (Exception ex)
            {
                Utils.ShowErrorMessage(ex);
            }
        }
        private void RefreshRecords()
        {
            try
            {
                IsDataLoading         = true;
                IsDataFinishedLoading = false;

                AnimeFolderSummaryRecords.Clear();

                TotalFileCount = 0;
                double fileSize = 0;


                AniDB_AnimeVM anime = this.DataContext as AniDB_AnimeVM;
                if (anime == null)
                {
                    return;
                }

                JMMServerBinary.Contract_AnimeSeries contract = JMMServerVM.Instance.clientBinaryHTTP.GetSeriesForAnime(anime.AnimeID,
                                                                                                                        JMMServerVM.Instance.CurrentUser.JMMUserID.Value);

                if (contract == null)
                {
                    return;
                }
                thisSeries = new AnimeSeriesVM(contract);

                Dictionary <string, AnimeFolderSummary> folders = new Dictionary <string, AnimeFolderSummary>();

                foreach (VideoLocalVM vid in anime.AllVideoLocals)
                {
                    TotalFileCount++;
                    fileSize += (double)vid.FileSize;
                    if (!folders.ContainsKey(vid.FileDirectory))
                    {
                        AnimeFolderSummary fs = new AnimeFolderSummary();
                        fs.FolderName              = vid.FileDirectory;
                        fs.FileCount               = 0;
                        fs.TotalFileSize           = 0;
                        folders[vid.FileDirectory] = fs;
                    }

                    folders[vid.FileDirectory].FileCount     = folders[vid.FileDirectory].FileCount + 1;
                    folders[vid.FileDirectory].TotalFileSize = folders[vid.FileDirectory].TotalFileSize + vid.FileSize;
                }

                bool foundDefault = false;
                foreach (AnimeFolderSummary afs in folders.Values)
                {
                    afs.IsDefaultFolder = false;

                    if (!string.IsNullOrEmpty(thisSeries.DefaultFolder))
                    {
                        if (thisSeries.DefaultFolder.Equals(afs.FolderName, StringComparison.InvariantCultureIgnoreCase))
                        {
                            afs.IsDefaultFolder = true;
                            foundDefault        = true;
                        }
                    }
                    AnimeFolderSummaryRecords.Add(afs);
                }

                /*if (!foundDefault)
                 * {
                 *  if (!string.IsNullOrEmpty(thisSeries.DefaultFolder))
                 *  {
                 *      AnimeFolderSummary afs = new AnimeFolderSummary();
                 *      afs.FolderName = thisSeries.DefaultFolder;
                 *      afs.FileCount = 0;
                 *      afs.TotalFileSize = 0;
                 *      afs.IsDefaultFolder = true;
                 *      AnimeFolderSummaryRecords.Add(afs);
                 *  }
                 * }*/


                TotalFileSize = Utils.FormatFileSize(fileSize);


                IsDataLoading         = false;
                IsDataFinishedLoading = true;
            }
            catch (Exception ex)
            {
                Utils.ShowErrorMessage(ex);
            }
        }