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>
    /// ImageListCellEdit.xaml 的交互逻辑
    /// </summary>
    public partial class ImageListCellEdit : ListCellEditBase
    {
        public ImageListCellEdit()
        {
            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 = cellData?.Value?.ToString();
            this.IsSendToPreview = true;
        }

        /// <summary>
        /// 值改变时触发
        /// </summary>
        protected virtual 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="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 = dic[columnDefinition.FieldName]?.ToString();
        }
    }
}
