using CsharpHttpHelper;
using DeviceRepair.DataAccess;
using DeviceRepair.Models;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DeviceRepairAndOptimization.Biz
{
    public class MaintenanceManager
    {
        private static MaintenanceManager manager;
        public static MaintenanceManager Instance
        {
            get
            {
                if (manager == null)
                    manager = new MaintenanceManager();
                return manager;
            }
        }

        /// <summary>
        /// 根据关键字筛选点检表
        /// </summary>
        /// <param name="filterValue"></param>
        /// <returns></returns>
        public APIResponseData GetDatas(DeviceWarrantyRequestFormFilter FilterInfo)
        {
            APIResponseData apiResponseData = new APIResponseData { Code = -1, Message = "未能获取到数据！" };
            try
            {
                switch (DeviceRepair.Utils.Config.Configurations.Properties.ConnType?.ToLower())
                {
                    case "api":

                        apiResponseData = ApiHelper.Instance.SendMessage(new HttpItem
                        {
                            URL = $"{ServiceRouteConstValue.GetMaintenanceDatas}",
                            Method = "Post",
                            ContentType = "application/json; charset=utf-8",
                            Postdata = JsonConvert.SerializeObject(FilterInfo)
                        });

                        break;

                    default:

                        apiResponseData = MaintenanceAccess.Instance.GetDatas(FilterInfo);

                        break;
                }
            }
            catch (Exception ex)
            {
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message;
            }
            return apiResponseData;
        }

        /// <summary>
        /// 设备维修
        /// </summary>
        /// <param name="Entity"></param>
        /// <returns></returns>
        public APIResponseData DeviceMaintion(DeviceWarrantyRequestMaintaionView Entity)
        {
            APIResponseData apiResponseData = new APIResponseData { Code = -1, Message = "操作未能成功！" };
            try
            {
                switch (DeviceRepair.Utils.Config.Configurations.Properties.ConnType?.ToLower())
                {
                    case "api":

                        apiResponseData = ApiHelper.Instance.SendMessage(new HttpItem
                        {
                            URL = $"{ServiceRouteConstValue.DeviceMaintion}",
                            Method = "Post",
                            ContentType = "application/json; charset=utf-8",
                            Postdata = JsonConvert.SerializeObject(Entity)
                        });

                        break;

                    default:

                        apiResponseData = MaintenanceAccess.Instance.DeviceMaintion(Entity.TodbModel(), Entity.AccessoriesItems, GlobalInfo.OperationInfo);

                        break;
                }
            }
            catch (Exception ex)
            {
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message;
            }
            return apiResponseData;
        }

        /// <summary>
        /// 修改停机单 停机状态
        /// </summary>
        /// <param name="AutoID"></param>
        /// <param name="Status"></param>
        /// <returns></returns>
        public APIResponseData ChangeDownStatus(int AutoID, bool Status)
        {
            APIResponseData apiResponseData = new APIResponseData { Code = -1, Message = "操作未能成功！" };
            try
            {
                switch (DeviceRepair.Utils.Config.Configurations.Properties.ConnType?.ToLower())
                {
                    case "api":

                        apiResponseData = ApiHelper.Instance.SendMessage(new HttpItem
                        {
                            URL = $"{ServiceRouteConstValue.ChangeDownStatus}?AutoID={AutoID}&Status={(Status ? 1 : 0)}",
                            Method = "Post",
                            ContentType = "application/json; charset=utf-8",
                            Postdata = $"AutoID={AutoID}&Status={(Status ? 1 : 0)}"
                        });

                        break;

                    default:

                        apiResponseData = MaintenanceAccess.Instance.ChangeDownStatus(AutoID, Status, GlobalInfo.OperationInfo);

                        break;
                }
            }
            catch (Exception ex)
            {
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message;
            }
            return apiResponseData;
        }

        /// <summary>
        /// 双重确认
        /// </summary>
        /// <param name="AutoID"></param>
        /// <param name="t">1:技术人员确认  2:工程师或维修主管确认</param>
        /// <returns></returns>
        public APIResponseData DoubleValidateMaintenance(int AutoID, int t)
        {
            APIResponseData apiResponseData = new APIResponseData { Code = -1, Message = "操作未能成功！" };
            try
            {
                switch (DeviceRepair.Utils.Config.Configurations.Properties.ConnType?.ToLower())
                {
                    case "api":

                        apiResponseData = ApiHelper.Instance.SendMessage(new HttpItem
                        {
                            URL = $"{ServiceRouteConstValue.DoubleValidateMaintenance}?AutoID={AutoID}&t={t}",
                            Method = "Post",
                            ContentType = "application/json; charset=utf-8"
                        });

                        break;

                    default:

                        apiResponseData = MaintenanceAccess.Instance.ValidateMaintenance(AutoID, t, GlobalInfo.OperationInfo);

                        break;
                }
            }
            catch (Exception ex)
            {
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message;
            }
            return apiResponseData;
        }
    }
}
