using Mall.Model.Extend.TradePavilion;
using Mall.Repository.TradePavilion;
using System;
using System.Collections.Generic;
using System.Text;
using VT.FW.AOP.CustomerAttribute;
using Mall.Common.Enum;
using VT.FW.DB;
using Newtonsoft.Json.Linq;
using Mall.Common.Plugin;

namespace Mall.Module.TradePavilion
{
    /// <summary>
    /// 自定义表单处理类
    /// </summary>
    public class CustomFormModule
    {
        /// <summary>
        /// 自定义表单仓储层对象
        /// </summary>
        private readonly RB_Custom_FormRepository custom_FormRepository = new RB_Custom_FormRepository();

        /// <summary>
        /// 获取自定义表单分页列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="rowsCount"></param>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Custom_Form_Extend> GetCustomFormPageModule(int pageIndex, int pageSize, out long rowsCount, RB_Custom_Form_Extend query)
        {
            return custom_FormRepository.GetCustomFormPageRepository(pageIndex, pageSize, out rowsCount, query);
        }

        /// <summary>
        /// 获取自定义表单列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Custom_Form_Extend> GetCustomFormListModule(RB_Custom_Form_Extend query)
        {
            return custom_FormRepository.GetCustomFormListRepository(query);
        }

        /// <summary>
        /// 检查表单是否存在
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool CheckExistsCustomFormModule(RB_Custom_Form_Extend model)
        {
            IList<WhereHelper> list = new List<WhereHelper>
            {
                new WhereHelper(nameof(RB_Custom_Form_Extend.FormType), model.FormType),
                new WhereHelper(nameof(RB_Custom_Form_Extend.Status), (int)DateStateEnum.Normal),
                new WhereHelper(nameof(RB_Custom_Form_Extend.TenantId), model.TenantId)
            };
            if (model.Id > 0)
            {
                list.Add(new WhereHelper(nameof(RB_Custom_Form_Extend.Id), model.Id, OperatorEnum.NotEqual));
            }
            return custom_FormRepository.Exists(list);

        }

        /// <summary>
        /// 新增修改自定义表单
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [TransactionCallHandler]
        public virtual bool SetCustomFormModule(RB_Custom_Form_Extend model)
        {
            bool flag = false;
            if (model.Id > 0)
            {
                Dictionary<string, object> fileds = new Dictionary<string, object>()
                {
                    { nameof(RB_Custom_Form_Extend.FormName),model.FormName.Trim()},
                    { nameof(RB_Custom_Form_Extend.FormType),model.FormType},
                    { nameof(RB_Custom_Form_Extend.FormData),model.FormData},
                    { nameof(RB_Custom_Form_Extend.UpdateBy),model.UpdateBy},
                    { nameof(RB_Custom_Form_Extend.UpdateDate),model.UpdateDate},
                };
                flag= custom_FormRepository.Update(fileds, new WhereHelper(nameof(RB_Custom_Form_Extend.Id), model.Id));
            }
            else
            {
                var newId = custom_FormRepository.Insert(model);
                model.Id = newId;
                flag = newId > 0;
            }
            return flag;
        }

        /// <summary>
        /// 根据编号获取自定义表单
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public RB_Custom_Form_Extend GetCustomFormModule(object Id)
        {
            var extModel = custom_FormRepository.GetEntity<RB_Custom_Form_Extend>(Id);
            if (extModel != null)
            {
                extModel.FormDataList = AnalyzeFormComponent(extModel.FormData);
            }
            return extModel;
        }

        /// <summary>
        /// 解析表单组件
        /// </summary>
        /// <param name="formData"></param>
        /// <returns></returns>
        public List<FormDataItem> AnalyzeFormComponent(string formData)
        {
            List<FormDataItem> dataList = new List<FormDataItem>();
            if (!string.IsNullOrEmpty(formData))
            {

                JArray jArray = JArray.Parse(formData);
                if (jArray != null && jArray.Count > 0)
                {
                    foreach (var jItem in jArray)
                    {
                        JObject jObj = JObject.Parse(jItem.ToString());
                        FormDataItem dataItem = new FormDataItem()
                        {
                            CompKey = jObj.GetStringValue("CompKey"),
                            CompData = new object(),
                            isCked=false,
                        };
                        if (!string.IsNullOrEmpty(dataItem.CompKey))
                        {
                            switch (dataItem.CompKey)
                            {
                                //单行文本
                                case "SingleLineText":
                                    JObject singleObj = JObject.Parse(jObj.GetStringValue("CompData"));
                                    TextItem txtItem = new TextItem()
                                    {
                                        Name = singleObj.GetStringValue("Name"),
                                        WordsLength = singleObj.GetInt("WordsLength"),
                                        IsRequire = singleObj.GetBoolValue("IsRequire"),
                                    };
                                    dataItem.CompData = txtItem;
                                    break;
                                //多行文本
                                case "MultiLineText":
                                    JObject multiObj = JObject.Parse(jObj.GetStringValue("CompData"));
                                    TextItem multiItem = new TextItem()
                                    {
                                        Name = multiObj.GetStringValue("Name"),
                                        WordsLength = multiObj.GetInt("WordsLength"),
                                        IsRequire = multiObj.GetBoolValue("IsRequire"),
                                    };
                                    dataItem.CompData = multiItem;
                                    break;
                                //下拉框
                                case "DorpDownList":
                                    JObject dropdownObj = JObject.Parse(jObj.GetStringValue("CompData"));
                                    SelectItem selectItem = new SelectItem()
                                    {
                                        Name = dropdownObj.GetStringValue("Name"),
                                        IsMultiple = dropdownObj.GetBoolValue("IsMultiple"),
                                        IsRequire = dropdownObj.GetBoolValue("IsRequire"),
                                        OptionValue = "",
                                    };
                                    dataItem.CompData = selectItem;
                                    break;
                                //图片上传组件
                                case "ImageUploadComp":
                                    JObject imgObj = JObject.Parse(jObj.GetStringValue("CompData"));
                                    UploadItem imgItem = new UploadItem()
                                    {
                                        Name = imgObj.GetStringValue("Name"),
                                        FileCount = imgObj.GetInt("FileCount"),
                                        FileSizeLimit = imgObj.GetInt("FileSizeLimit"),
                                    };
                                    dataItem.CompData = imgItem;
                                    break;
                                //视频上传组件
                                case "VideoUploadComp":
                                    JObject videoObj = JObject.Parse(jObj.GetStringValue("CompData"));
                                    UploadItem videoItem = new UploadItem()
                                    {
                                        Name = videoObj.GetStringValue("Name"),
                                        FileCount = videoObj.GetInt("FileCount"),
                                        FileSizeLimit = videoObj.GetInt("FileSizeLimit"),
                                    };
                                    dataItem.CompData = videoItem;
                                    break;
                            }
                            dataList.Add(dataItem);
                        }
                    }
                }
            }
            return dataList;
        }


        /// <summary>
        /// 根据编号删除自定义表单
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public bool RemoveCustomFormModule(int Id)
        {
            Dictionary<string, object> fileds = new Dictionary<string, object>()
            {
                { nameof(RB_Custom_Form_Extend.Status),(int)DateStateEnum.Delete}
            };
            return custom_FormRepository.Update(fileds, new WhereHelper(nameof(RB_Custom_Form_Extend.Id),Id));
        }
    }
}
