using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using static Ramitta.winDataGrid;

namespace Ramitta
{
    public partial class winDataGrid : UserControl, INotifyPropertyChanged
    {
        public DataGrid DataGridControl => xDataGrid;

        public ObservableCollection<
            Dictionary<string, object>
            > Rows
        { get; private set; }

        public Dictionary<string,object> ColumnsName=new();

        public winDataGrid()
        {
            InitializeComponent();
            Rows = new();
            xDataGrid.ItemsSource = Rows;
        }

        // 定义列类型的枚举
        public enum ColumnType
        {
            Button,
            CheckBox,
            ComboBox,
            TextBox,
            Label
        }

        public void InitColumns(params (string columnName, ColumnType columnType)[] columns)
        {
            // 循环添加列
            foreach (var column in columns)
            {
                AddColumn(column.columnName, column.columnType);
            }
        }

        private void SetBindingToProperty(FrameworkElementFactory elementFactory, DependencyProperty property, string path)
        {
            elementFactory.SetValue(property,
                new Binding(path)
                {
                    Mode = BindingMode.TwoWay,
                    UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
                });
        }

        public void AddColumn(string columnName, ColumnType columnType)
        {
            DataGridTemplateColumn column = new DataGridTemplateColumn();
            FrameworkElementFactory? elementFactory=null;
            ColumnsName.Add(columnName, columnType);

            switch (columnType)
            {
                case ColumnType.Button:
                    elementFactory = new FrameworkElementFactory(typeof(Button));
                    SetBindingToProperty(elementFactory, Button.ContentProperty, $"[{columnName}].Content");
                    break;
                case ColumnType.CheckBox:
                    elementFactory = new FrameworkElementFactory(typeof(CheckBox));
                    SetBindingToProperty(elementFactory, CheckBox.IsCheckedProperty, $"[{columnName}].IsChecked");
                    break;
                case ColumnType.ComboBox:
                    elementFactory = new FrameworkElementFactory(typeof(ComboBox));
                    elementFactory.SetValue(ComboBox.SelectedIndexProperty, 0);
                    SetBindingToProperty(elementFactory, ComboBox.ItemsSourceProperty, $"[{columnName}].ItemsSource");
                    SetBindingToProperty(elementFactory, ComboBox.SelectedValueProperty, $"[{columnName}].SelectedValue");
                    break;
                case ColumnType.Label:
                    elementFactory = new FrameworkElementFactory(typeof(Label));
                    SetBindingToProperty(elementFactory, Label.ContentProperty, $"[{columnName}].Content");
                    SetBindingToProperty(elementFactory, Label.BackgroundProperty, $"[{columnName}].Background");
                    break;
                case ColumnType.TextBox:
                    elementFactory = new FrameworkElementFactory(typeof(TextBox));
                    SetBindingToProperty(elementFactory, TextBox.TextProperty, $"[{columnName}].Text");
                    SetBindingToProperty(elementFactory, TextBox.BackgroundProperty, $"[{columnName}].Background");
                    break;
            }
            DataTemplate dataTemplate = new DataTemplate() { VisualTree=elementFactory};
            
            column.Header = columnName;
            column.CellTemplate = dataTemplate;
            column.CellEditingTemplate = dataTemplate;
            xDataGrid.Columns.Add( column );
        }

        

        public Dictionary<string, object> AddRow(){
            var keys = ColumnsName.Keys.ToList();
            var row = new Dictionary<string, object> { };

            foreach (var key in keys) {

                switch (ColumnsName[key])
                {
                    case ColumnType.Button:
                        row.Add(key, new Button());
                        break;
                    case ColumnType.CheckBox:
                        row.Add(key, new CheckBox());
                        break;
                    case ColumnType.ComboBox:
                        row.Add(key, new ComboBox());
                        break;
                    case ColumnType.TextBox:
                        row.Add(key, new TextBox()); 
                        break;
                    case ColumnType.Label:
                        row.Add(key, new Label());
                        break;
                }
            }

            Rows.Add(row);
            return row;
        }

        public void Clear() { 
            Rows.Clear();
        }

        public int Count()
        {
            return Rows.Count();
        }

        public void DeleteRow(Dictionary<string, object> row)
        {
            // 从 Rows 集合中移除指定的行
            if (Rows.Contains(row))
            {
                Rows.Remove(row);
            }
        }

        public event PropertyChangedEventHandler? PropertyChanged;
        protected void OnPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

    }
}
