using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Windows;
using System.Windows.Forms;
using uBIMEarthTools.Utils;
using MessageBox = System.Windows.MessageBox;

namespace uBIMEarthTools.Commands.DesignDataEntry
{
    public class DesignDataEntry : INotifyPropertyChanged
    {
        private string filePath = "选择文件";
        private ObservableCollection<string> familyNames;
        private string selectedFamily;
        private ObservableCollection<string> linkFiles;
        private ObservableCollection<string> types;
        private double depth = 1.5;
        private string selectedType;
        private string selectedLink;

        public string FilePath
        {
            get => filePath;
            set
            {
                filePath = value;
                PropertyChanged.Notify(() => FilePath);
            }
        }

        public ObservableCollection<string> FamilyNames
        {
            get => familyNames;
            set
            {
                familyNames = value;
                PropertyChanged.Notify(() => FamilyNames);
            }
        }

        public string SelectedFamily
        {
            get => selectedFamily;
            set
            {
                selectedFamily = value;
                PropertyChanged.Notify(() => SelectedFamily);
            }
        }

        public ObservableCollection<string> LinkFiles
        {
            get => linkFiles;
            set
            {
                linkFiles = value;
                PropertyChanged.Notify(() => LinkFiles);
            }
        }

        public string SelectedLink
        {
            get => selectedLink;
            set
            {
                selectedLink = value;
                Types = LinkDictionary[value];
                PropertyChanged.Notify(() => SelectedLink);
            }
        }

        public ObservableCollection<string> Types
        {
            get => types;
            set
            {
                types = value;
                PropertyChanged.Notify(() => Types);
            }
        }
        public string SelectedType
        {
            get => selectedType;
            set
            {
                selectedType = value;
                PropertyChanged.Notify(() => SelectedType);
            }
        }

        public double Depth
        {
            get => depth;
            set
            {
                depth = value;
                PropertyChanged.Notify(() => Depth);
            }
        }

        public Dictionary<string, ObservableCollection<string>> LinkDictionary { get; set; } =
            new Dictionary<string, ObservableCollection<string>>();

        public DelegateCommand SelectFileCommand { get; set; }
        public DelegateCommand OkCommand { get; set; }
        public DelegateCommand CancelCommand { get; set; }

        public bool Current { get; set; } = true;
        public bool Surface { get; set; } = false;

        public DesignDataEntry()
        {
            SelectFileCommand = new DelegateCommand() { ExecuteCommand = OpenFile };
            OkCommand = new DelegateCommand() { ExecuteCommand = Ok };
            CancelCommand = new DelegateCommand() { ExecuteCommand = Cancel };
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void OpenFile(object obj)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Title = "选择Excel文件…";
            openFileDialog.Filter = "Excel文件|*.xlsx";
            openFileDialog.RestoreDirectory = false;
            openFileDialog.FilterIndex = 1;
            if (openFileDialog.ShowDialog() == DialogResult.OK)
                FilePath = openFileDialog.FileName;
        }

        public void Ok(object obj)
        {
            if (!File.Exists(filePath))
            {
                MessageBox.Show("文件不存在!");
                return;
            }
            if (string.IsNullOrEmpty(SelectedFamily))
            {
                MessageBox.Show("未选择对应的族");
                return;
            }
            (obj as Window).DialogResult = true;
            (obj as Window).Close();
        }
        public void Cancel(object obj)
        {
            (obj as Window).DialogResult = false;
            (obj as Window).Close();
        }
    }
}