using CsharpHttpHelper;
using DeviceRepair.DataAccess;
using DeviceRepair.Models;
using DeviceRepair.Models.Preserve;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;

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

        /// <summary>
        /// 点检表数据新增
        /// </summary>
        /// <param name="Item"></param>
        /// <returns></returns>
        public APIResponseData InsertSingle(MaintenanceFormVersionInfo Item)
        {
            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.FormInsertSingle}",
                            Method = "Post",
                            ContentType = "application/json; charset=utf-8",
                            Postdata = JsonConvert.SerializeObject(Item)
                        });
                        break;

                    default:
                        apiResponseData = FormAccess.Instance.InsertSingle(Item, GlobalInfo.OperationInfo);
                        break;
                }
            }
            catch (Exception ex)
            {
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message;
            }
            return apiResponseData;
        }


        /// <summary>
        /// 判断点检表数据是否存在
        /// </summary>
        /// <param name="Item"></param>
        /// <returns></returns>
        public APIResponseData DataIsExists(MaintenanceFormVersionInfo Item)
        {
            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.FormDataIsExists}",
                            Method = "Post",
                            ContentType = "application/json; charset=utf-8",
                            Postdata = JsonConvert.SerializeObject(Item)
                        });
                        break;

                    default:
                        apiResponseData = FormAccess.Instance.DataIsExists(Item);
                        break;
                }
            }
            catch (Exception ex)
            {
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message;
            }
            return apiResponseData;
        }

        /// <summary>
        /// 文件上传
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public async Task<APIResponseData> UploadFile(string filePath)
        {
            APIResponseData apiResponseData = new APIResponseData { Code = -1, Message = "操作失败！" };
            try
            {
                string FileExtension = Path.GetExtension(filePath);
                string newName = $"{Guid.NewGuid()}{FileExtension}";

                switch (DeviceRepair.Utils.Config.Configurations.Properties.ConnType?.ToLower())
                {
                    case "api":
                        using (var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read))
                            apiResponseData = await ApiHelper.Instance.UploadFileAsync(ServiceRouteConstValue.UploadForm, fileStream, newName);
                        break;

                    default:

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

        /// <summary>
        /// 获取单条数据
        /// </summary>
        /// <param name="PrimaryKey"></param>
        /// <returns></returns>
        public APIResponseData GetSingle(int PrimaryKey)
        {
            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.GetFormGetSingle}?pk={PrimaryKey}",
                            Method = "Get",
                            ContentType = "application/json; charset=utf-8",//返回类型    可选项有默认值
                        });
                        break;

                    default:
                        apiResponseData = FormAccess.Instance.GetSingle(PrimaryKey);
                        break;
                }
            }
            catch (Exception ex)
            {
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message;
            }
            return apiResponseData;
        }

        /// <summary>
        /// 根据关键字筛选点检表
        /// </summary>
        /// <param name="filterValue"></param>
        /// <returns></returns>
        public APIResponseData GetQuery(string filterValue)
        {
            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.FormDataQuery}?FilterString={filterValue}",
                            Method = "Get",
                            ContentType = "application/json; charset=utf-8",//返回类型    可选项有默认值
                        });
                        break;

                    default:
                        apiResponseData = FormAccess.Instance.GetQuery(filterValue);
                        break;
                }
            }
            catch (Exception ex)
            {
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message;
            }
            return apiResponseData;
        }

        /// <summary>
        /// 修改点检表备注信息
        /// </summary>
        /// <param name="Item"></param>
        /// <returns></returns>
        public APIResponseData ChangeRemark(MaintenanceFormVersionInfo Item)
        {
            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.FormRemarkChange}",
                            Method = "Post",
                            ContentType = "application/json; charset=utf-8",
                            Postdata = JsonConvert.SerializeObject(Item)
                        });
                        break;
                    default:
                        apiResponseData = FormAccess.Instance.ChangeRemark(Item, GlobalInfo.OperationInfo);
                        break;
                }
            }
            catch (Exception ex)
            {
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message;
            }
            return apiResponseData;
        }

        /// <summary>
        /// 单行数据翻转当前状态(启用/禁用)
        /// </summary>
        /// <param name="AutoID"></param>
        /// <param name="UserID"></param>
        /// <returns></returns>
        public APIResponseData ChangeStauts(int PrimaryKey)
        {
            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.FormDataStatusChange}?pk={PrimaryKey}",
                            Method = "Post",
                            ContentType = "application/json; charset=utf-8",//返回类型    可选项有默认值
                        });
                        break;
                    default:
                        apiResponseData = FormAccess.Instance.ChangeStauts(PrimaryKey, GlobalInfo.OperationInfo);
                        break;
                }
            }
            catch (Exception ex)
            {
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message;
            }
            return apiResponseData;
        }

        /// <summary>
        /// 获取设备信息树形结构 -- 按名称分类
        /// </summary>
        /// <returns></returns>
        public APIResponseData GetDeviceInformationTreeView()
        {
            APIResponseData apiResponseData = new APIResponseData { Code = -1 };
            try
            {
                switch (DeviceRepair.Utils.Config.Configurations.Properties.ConnType?.ToLower())
                {
                    case "api":

                        apiResponseData = ApiHelper.Instance.SendMessage(new HttpItem
                        {
                            URL = ServiceRouteConstValue.GetDeviceInformationTreeView,
                            Method = "Get",
                            ContentType = "application/json; charset=utf-8",
                        });

                        break;
                    case "sql":

                        apiResponseData = FormAccess.Instance.GetDeviceInformationTreeView();

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

        /// <summary>
        /// 分配点检表到指定设备
        /// </summary>
        /// <param name="lst"></param>
        /// <returns></returns>
        public APIResponseData AssigningFormToDevices(List<SubmitAssignFormsToDevices> lst)
        {
            APIResponseData apiResponseData = new APIResponseData { Code = -1 };

            try
            {
                switch (DeviceRepair.Utils.Config.Configurations.Properties.ConnType?.ToLower())
                {
                    case "api":
                        apiResponseData = ApiHelper.Instance.SendMessage(new HttpItem
                        {
                            URL = ServiceRouteConstValue.AssigningFormToDevices,
                            Method = "Post",
                            ContentType = "application/json;charset=utf-8",
                            Postdata = JsonConvert.SerializeObject(lst)
                        });
                        break;
                    default:
                        apiResponseData = FormAccess.Instance.AssigningFormToDevices(lst, GlobalInfo.OperationInfo);
                        break;
                }
            }
            catch (Exception ex)
            {
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message;
            }
            return apiResponseData;
        }

    }
}
