using System;
using System.Collections.Generic;
using System.Dynamic;
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.Navigation;
using System.Windows.Shapes;
using VIZ.Package.Domain;
using VIZ.Package.Service;

namespace VIZ.Package.Module
{
    /// <summary>
    /// DupletListCellEdit.xaml 的交互逻辑
    /// </summary>
    public partial class DupletListCellEdit : ListCellEditBase
    {
        public DupletListCellEdit()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 数据上下文改变时触发
        /// </summary>
        /// <param name="e">事件参数</param>
        protected override void OnDataContextChanged(DependencyPropertyChangedEventArgs e)
        {
            DevExpress.Xpf.Grid.EditGridCellData cellData = e.NewValue as DevExpress.Xpf.Grid.EditGridCellData;

            this.IsSendToPreview = false;
            this.PART_Text.EditValue = this.FormatText(cellData?.Value?.ToString());
            this.IsSendToPreview = true;
        }

        /// <summary>
        /// 值改变时触发
        /// </summary>
        private void EditValueChanged(object sender, DevExpress.Xpf.Editors.EditValueChangedEventArgs e)
        {
            // 是否需要发送至预览
            if (!this.IsSendToPreview)
                return;

            // 预览连接不存在
            if (ApplicationDomainEx.PreviewConn == null)
                return;

            DevExpress.Xpf.Grid.EditGridCellData cellData = this.DataContext as DevExpress.Xpf.Grid.EditGridCellData;
            if (cellData == null)
                return;

            GridColumnControlFieldDefinition columnDefinition = cellData.Column.DataContext as GridColumnControlFieldDefinition;
            if (columnDefinition == null)
                return;

            // 设置值
            IDictionary<string, object> dic = cellData.Row as IDictionary<string, object>;
            dic[columnDefinition.FieldName] = e.NewValue?.ToString() ?? string.Empty;

            // 向Viz发送指令
            this.VizCommandControlObjectService.SetControlObjectListValue(
                ApplicationDomainEx.PreviewConn,
                ApplicationDomainEx.CurrentPage.GetVizScene(),
                columnDefinition.ControlObject.TreeNodePath,
                columnDefinition.ControlField.FieldIdentifier,
                cellData.RowData.RowHandle.Value,
                columnDefinition.FieldName,
                e.NewValue?.ToString() ?? string.Empty);
        }

        /// <summary>
        /// 显示编辑窗口
        /// </summary>
        private void EditClick(object sender, RoutedEventArgs e)
        {
            DevExpress.Xpf.Grid.EditGridCellData cellData = this.DataContext as DevExpress.Xpf.Grid.EditGridCellData;
            if (cellData == null)
                return;

            GridColumnControlFieldDefinition columnDefinition = cellData.Column.DataContext as GridColumnControlFieldDefinition;
            if (columnDefinition == null)
                return;

            FieldEditWindow window = this.GetFieldEditWindow();
            FieldEditWindowModel vm = window.DataContext as FieldEditWindowModel;

            vm.Update(this, columnDefinition, cellData.RowData.RowHandle.Value, cellData.Row as ExpandoObject);

            window.ShowInTaskbar = true;
            window.Visibility = Visibility.Visible;
        }

        /// <summary>
        /// 格式化文本
        /// </summary>
        /// <param name="text">输入文本</param>
        /// <returns></returns>
        private string FormatText(string text)
        {
            if (string.IsNullOrWhiteSpace(text))
                return text;

            string[] parts = text.Split(' ');

            double x = 0;
            double y = 0;

            if (parts.Length >= 1)
            {
                double.TryParse(parts[0], out x);
            }
            if (parts.Length >= 2)
            {
                double.TryParse(parts[1], out y);
            }

            return $"{x} {y}";
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="columnDefinition">列定义</param>
        /// <param name="rowHandle">行号</param>
        /// <param name="row">行数据</param>
        public override void UpdateEditValue(GridColumnControlFieldDefinition columnDefinition, int rowHandle, ExpandoObject row)
        {
            IDictionary<string, object> dic = row as IDictionary<string, object>;
            if (dic == null)
            {
                this.PART_Text.EditValue = null;
                return;
            }

            this.PART_Text.EditValue = this.FormatText(dic[columnDefinition.FieldName]?.ToString());
        }
    }
}
