using GalaSoft.MvvmLight.Command;
using Install.Core;
using Install.Core.Common;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;

namespace Install.View
{
    public class PgInstallVm : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public ObservableCollection<ListItem> Items { get; } = new ObservableCollection<ListItem>();

        public string InstallPath { get; set; }


        public Dictionary<string, BitmapSource> Icons { get; private set; }

        public RelayCommand NextCmd { get; private set; }
        public RelayCommand SelectPathCmd { get; private set; }
        public RelayCommand DownloadCmd { get; private set; }

        InstallWizard installWizard;
        public InstallWizard InstallWizard => installWizard;

        public PgInstallVm() 
        {
            NextCmd = new RelayCommand(Next);
            SelectPathCmd = new RelayCommand(SelectPath);
            DownloadCmd = new RelayCommand(Download);
        }
        private void Download()
        {
            //打开 下载进度界面
            WdDownload w = new WdDownload();
            w.Init();
            w.ShowDialog();

        }


        public void Init() 
        {
            installWizard = App.Resolve<InstallWizard>();

            Icons = installWizard.Icons;

            foreach (var installpack in installWizard.ToInstall) 
            {
                Items.Add(new ListItem() { Item = installpack, IsSelected = installpack.IsDefaultSelected });
            }

            InstallPath = installWizard.InstallPacks.DefaultInstallPath;
        }

        private void SelectPath()
        {
            var dbd = new System.Windows.Forms.FolderBrowserDialog();
            dbd.SelectedPath = InstallPath;

            if (dbd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                InstallPath = dbd.SelectedPath;
            }
        }

        private async void Next()
        {
            //筛选出选择的软件
            var installPacks = from item in Items where item.IsSelected select item.Item as InstallPack;

            //检测有没安装被选
            if (installPacks.Count() == 0)
            {
                //没有任何一个被选择
                System.Windows.MessageBox.Show("没有任何一个被选择", "异常", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }

            //检测路径
            if (!System.IO.Directory.Exists(InstallPath))
            {
                try
                {
                    System.IO.Directory.CreateDirectory(InstallPath);

                }
                catch
                {
                    System.Windows.MessageBox.Show(InstallPath + " 路径不能创建，重新选择", "异常", MessageBoxButton.OK, MessageBoxImage.Error);
                    return;
                }
            }

            PgProgress p = new PgProgress();
            p.Init();
            App.Resolve<NavigationService>().Navigate(p);
            await installWizard.Install(InstallPath, installPacks);
        }
    }
    
}
