using LiteDB.Engine;
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VIZ.Framework.Core;
using VIZ.Package.Domain;
using VIZ.Package.Service;

namespace VIZ.Package.Module
{
    /// <summary>
    /// Radio类型编辑面板模型
    /// </summary>
    public class RadioEditPanelModel : EditPanelModelBase
    {
        // ============================================================
        // Property
        // ============================================================

        #region EditValue -- 编辑值

        private int editValue;
        /// <summary>
        /// 编辑值
        /// </summary>
        public int EditValue
        {
            get { return editValue; }
            set
            {
                editValue = value;
                this.RaisePropertyChanged(nameof(EditValue));
                this.OnEditValueChanged();
            }
        }

        #endregion

        #region ItemsSource -- 数据项集合

        private List<string> itemsSource;
        /// <summary>
        /// 数据项集合
        /// </summary>
        public List<string> ItemsSource
        {
            get { return itemsSource; }
            set { itemsSource = value; this.RaisePropertyChanged(nameof(ItemsSource)); }
        }

        #endregion

        // ============================================================
        // Message
        // ============================================================

        // ============================================================
        // Public Function
        // ============================================================

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="controlObject">控制对象</param>
        /// <param name="controlField">控制字段</param>
        public override void Update(ControlObjectModel controlObject, ControlFieldNodeModel controlField)
        {
            base.Update(controlObject, controlField);

            this.IsSendToPreview = false;

            string[] pars = controlField?.FieldDetail?.Split(new char[] { ':' }, StringSplitOptions.None);
            string choices = string.Empty;
            if (pars.Length >= 8)
            {
                choices = pars[7];
            }
            choices = choices.Replace("choices=", string.Empty);
            this.ItemsSource = choices.Split('|').ToList();
            int.TryParse(controlField?.Value, out int value);
            this.EditValue = value;

            this.IsSendToPreview = true;
        }

        /// <summary>
        /// 更新动态数据
        /// </summary>
        /// <param name="listCellEdit">列单元格编辑器</param>
        /// <param name="columnDefinition">列定义</param>
        /// <param name="rowHandle">行号</param>
        /// <param name="row">行数据</param>
        public override void UpdateDynamic(ListCellEditBase listCellEdit, GridColumnControlFieldDefinition columnDefinition, int rowHandle, ExpandoObject row)
        {
            base.UpdateDynamic(listCellEdit, columnDefinition, rowHandle, row);

            IDictionary<string, object> dic = row as IDictionary<string, object>;

            this.IsSendToPreview = false;

            string[] pars = columnDefinition?.ControlField?.FieldDetail?.Split(new char[] { ':' }, StringSplitOptions.None);
            string choices = string.Empty;
            if (pars.Length >= 8)
            {
                choices = pars[7];
            }
            choices = choices.Replace("choices=", string.Empty);
            this.ItemsSource = choices.Split('|').ToList();
            string str = dic?[columnDefinition.FieldName]?.ToString().Trim();
            int.TryParse(str, out int value);
            this.EditValue = value;

            this.IsSendToPreview = true;
        }

        /// <summary>
        /// 获取字段值
        /// </summary>
        /// <returns>字段值</returns>
        public override string GetFieldValue()
        {
            return this.EditValue.ToString();
        }

        // ============================================================
        // Private Function
        // ============================================================

        /// <summary>
        /// 值改变时触发
        /// </summary>
        private void OnEditValueChanged()
        {
            // 不需要向预览发送值
            if (!this.IsSendToPreview)
                return;

            // 没有预览连接
            if (ApplicationDomainEx.PreviewConn == null)
                return;

            // 没有控制对象或控制字段
            if (this.ControlObject == null || this.ControlField == null)
                return;

            // 正常模式编辑
            if (this.FieldEditMode == FieldEditMode.Normal)
            {
                this.ControlField.Value = this.EditValue.ToString();

                if (!this.ControlField.IsCustom)
                {
                    this.VizCommandControlObjectService.SetControlObjectValue(
                                    ApplicationDomainEx.PreviewConn,
                                    ApplicationDomainEx.CurrentPage.GetVizScene(),
                                    this.ControlObject.TreeNodePath,
                                    this.ControlField.FieldIdentifier,
                                    this.ControlField.Value);
                }
                else
                {
                    this.VizCommandControlObjectService.SetCustomControlFieldValue(ApplicationDomainEx.PreviewConn, new List<ControlFieldNodeModel> { this.ControlField });
                }

                return;
            }

            // 没有列信息或行数据
            if (this.ColumnDefinition == null || this.Row == null)
                return;

            // 动态模式编辑
            if (this.FieldEditMode == FieldEditMode.Dynamic)
            {
                IDictionary<string, object> dic = this.Row as IDictionary<string, object>;
                dic[this.ColumnDefinition.FieldName] = this.EditValue.ToString();

                this.ListCellEdit.UpdateEditValue(this.ColumnDefinition, this.RowHandle, this.Row);

                return;
            }
        }
    }
}
