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 PgUpdateVm : INotifyPropertyChanged
    {
        public OPTION Option { get; set; } = OPTION.AUTORUN;

        public event PropertyChangedEventHandler PropertyChanged;

        /// <summary>
        /// 有能新安装的程序
        /// </summary>
        public bool HasNew { get; private set; }

        public Dictionary<string, BitmapSource> Icons { get; private set; }
        public ObservableCollection<ListItem> Items { get; } = new ObservableCollection<ListItem>();




        public RelayCommand NextCmd { get; private set; }
        public RelayCommand NewCmd { get; private set; }

        public RelayCommand DownloadCmd { get; private set; }

        InstallWizard installWizard;
        public InstallWizard InstallWizard => installWizard;

        public PgUpdateVm() 
        {
            NextCmd = new RelayCommand(Next);
            NewCmd = new RelayCommand(New);
            DownloadCmd = new RelayCommand(Download);
        }

        private void Download()
        {
            //打开 下载进度界面
            WdDownload w = new WdDownload();
            w.Init();
            w.ShowDialog();
            
        }

        public void Init() 
        {
            installWizard = Application.Current.Properties[nameof(InstallWizard)] as InstallWizard;

            Icons = installWizard.Icons;

            foreach (var installInfo in installWizard.HasInstalled)
            {
                Items.Add(new ListItem() { Item = installInfo, IsSelected = installInfo.NeedToUpdate });
            }

            HasNew = installWizard.ToInstall.Count > 0;
        }
        private void New()
        {
            PgInstall p = new PgInstall();
            p.Init();
            (Application.Current.Properties["NavigationService"] as NavigationService).Navigate(p);
        }

        private async void Next()
        {
            //检测有没选
            var items = from item in Items where item.IsSelected select item.Item as InstallInfo;
            if (items.Count()==0)
            {
                System.Windows.MessageBox.Show("没有任何一个被选择", "异常", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }
            PgProgress p = new PgProgress();
            p.Init();
            (Application.Current.Properties["NavigationService"] as NavigationService).Navigate(p);

            switch (Option)
            {
                case OPTION.AUTORUN:
                    {
                        await installWizard.SetAutoRun(items);
                    }
                    break;
                case OPTION.UPDATE:
                    {
                        await installWizard.Update(items);
                    }
                    break;
                case OPTION.REMOVE:
                    {
                        await installWizard.Remove(items);
                    }
                    break;
            }
        }
    }

    public enum OPTION
    {
        AUTORUN,
        UPDATE,
        REMOVE
    }
}
