using DeviceRepair.Models;
using DeviceRepair.Utils;
using NLog;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using TsSFCDevice.Client.Biz.Base.Service;
using TsSFCDevice.Client.Biz.Base.Utils;

namespace TsSFCDevice.Client.Biz.Impl
{
    public class CustomFieldRepository
    {
        private readonly Logger log;
        private static CustomFieldRepository manager;

        private IDictionary<string, string> m_ApiParameters;
        /// <summary>
        /// API输入参数
        /// </summary>
        public IDictionary<string, string> ApiParameters
        {
            get
            {
                return m_ApiParameters;
            }
            set
            {
                m_ApiParameters = value;
            }
        }

        public static CustomFieldRepository Instance
        {
            get
            {
                if (manager == null)
                    manager = new CustomFieldRepository();
                return manager;
            }
        }

        public CustomFieldRepository()
        {
            log = LogManager.GetCurrentClassLogger();
            m_ApiParameters = new Dictionary<string, string>();
        }

        internal string GetParameters(string cOperator = "", bool bFlag = true)
        {

            return ParametersObject.GetInstance(cOperator).GetJsonSerialized(m_ApiParameters);
        }

        public IList<FieldsInfo> GetDatas(string[] FieldCodes)
        {
            try
            {
                IList<FieldsInfo> Data = new List<FieldsInfo>();

                byte[] btResults = null;
                ApiParameters?.Clear();
                ApiParameters.Add("FieldCode", string.Join(",", FieldCodes));

                var Rtn = Utility.SfcBizService.CurrentSvc.GetDatas(DeviceSvc.SysModelType.CustomField, GetParameters(), out btResults);
                if (Rtn.Code != 1)
                {
                    throw new Exception(Rtn.Message);
                }
                DataSet dsResults = CompressionHelper.ExactDataSet(btResults);

                return DTOHelper<FieldsInfo>.DataTableToList(dsResults.Tables[0]);
            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
                throw ex;
            }
        }

        /// <summary>
        /// 检验数据是否存在
        /// </summary>
        /// <param name="Code"></param>
        /// <param name="Text"></param>
        /// <returns></returns>
        public bool Get_Field_Exists(string Code, string Text)
        {
            try
            {
                ApiParameters?.Clear();
                ApiParameters.Add("Code", Code);
                ApiParameters.Add("Text", Text);

                bool BeExists = true;

                var Rtn = Utility.SfcBizService.CurrentSvc.Get_Field_Exists(GetParameters(), out BeExists); ;
                if (Rtn.Code != 1)
                {
                    throw new Exception(Rtn.Message);
                }

                return BeExists;
            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
                throw ex;
            }
        }

        /// <summary>
        /// 修改状态
        /// </summary>
        /// <param name="FieldID"></param>
        /// <param name="Description"></param>
        /// <param name="Status"></param>
        /// <returns></returns>
        public APIResponseData Change_Field_Status(int FieldID, string Description, bool Status)
        {
            try
            {
                ApiParameters?.Clear();
                ApiParameters.Add("AutoID", FieldID + "");
                ApiParameters.Add("Status", Status + "");
                ApiParameters.Add("Description", Description);

                var Rtn = Utility.SfcBizService.CurrentSvc.Change_Field_Status(GetParameters());
                if (Rtn.Code != 1)
                {
                    throw new Exception(Rtn.Message);
                }
                return new APIResponseData { Code = 1, Message = Rtn.Message };
            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
                throw ex;
            }
        }

        /// <summary>
        /// 数据新增或者编辑
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public APIResponseData InsertOrEdit_Field_Data(FieldsInfo entity)
        {
            try
            {
                ApiParameters?.Clear();

                DataSet DataContent = new DataSet();
                DataTable table = (new List<FieldsInfo> { entity }).ToDataTable();
                DataContent.Tables.Add(table);

                var apiResponseData = Utility.SfcBizService.CurrentSvc.InsertOrEdit_Field_Data(GetParameters(), DataContent);
                if (apiResponseData.Code != 1)
                {
                    return new APIResponseData { Code = -1, Message = apiResponseData.Message };
                }

                return new APIResponseData { Code = 1 };
            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
                throw ex;
            }
        }
    }
}
