using BDU.Models;
using MahApps.Metro.Controls;
using PropertyChanged;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using TSMaster;
using static BDU.Models.ParameterModel;

namespace BDU.Windows
{
    /// <summary>
    /// ParameterSettingWindow.xaml 的交互逻辑
    /// </summary>
    [AddINotifyPropertyChangedInterface]
    public partial class ParameterSettingWindow : MetroWindow
    {
        public bool IsSaved { get; private set; } = false;

        public ParameterModel? Parameter { get; set; } = new();

        public ObservableCollection<Type> Types { get; set; } =
            [
            typeof(string), typeof(bool),
            typeof(byte),typeof(short), typeof(int), typeof(long), typeof(float), typeof(double),typeof(DateTime),typeof(TimeSpan),typeof(TLIBCAN),typeof(TLIBCANFD),
            typeof(byte[]), typeof(short[]), typeof(ushort[]), typeof(int[]), typeof(long[]), typeof(float[]), typeof(double[]),typeof(DateTime[]),typeof(TimeSpan[]),typeof(TLIBCAN[]),typeof(TLIBCANFD[]),
            typeof(object)
            ];

        public ObservableCollection<string> Categorys { get; set; } = new(Enum.GetNames(typeof(ParameterCategory)));

        public Array? EnumValues { get; set; }

        public ParameterSettingWindow()
        {
            InitializeComponent();
            DataContext = this;
        }

        public ParameterSettingWindow(ParameterModel parameter)
        {
            InitializeComponent();
            DataContext = this;
            this.Parameter = parameter;
        }

        private void Cancel_Click(object sender, RoutedEventArgs e)
        {
            Parameter = null;
            this.Close();
        }

        private void Save_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrWhiteSpace(Parameter?.Name) || Parameter.Type == null)
            {
                MessageBox.Show("缺少必填项", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }
            else if (Parameter!.Name == "Result")
            {
                MessageBox.Show("参数名不允许为\"Result\"", "警告", MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }
            if (Parameter!.Value == null)
            {
                Parameter.Value = CreateDefaultValue(Parameter.Type);
            }
            IsSaved = true;
            this.Close();
        }

        private static object? CreateDefaultValue(Type type)
        {
            if (type.IsValueType) return Activator.CreateInstance(type);
            return null;
        }

        private void GroupBox_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (e.ChangedButton == MouseButton.Left)
            {
                DragMove();
            }
        }

        private void ParaType_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (Parameter == null) return;
            if (Parameter!.Type != null && Parameter!.Type.BaseType == typeof(Enum))
            {
                EnumValues = Parameter?.Type?.IsEnum == true ? Enum.GetValues(Parameter.Type) : null;
            }
        }
    }
}
