using DeviceRepairAndOptimization.Models;
using DeviceRepairAndOptimization.Utils;
using Newtonsoft.Json;
using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace DeviceRepairAndOptimization.Data
{
    public class DriveMaintenanceManger
    {
        private static DriveMaintenanceManger manager;
        public static DriveMaintenanceManger Instance()
        {
            if (manager == null)
                manager = new DriveMaintenanceManger();
            return manager;
        }

        /// <summary>
        /// 获取所有的保养记录数据
        /// </summary>
        /// <returns></returns>
        public APIResponseData GetAll(string filter = "")
        {
            APIResponseData result = new APIResponseData { Code = -1, Data = null, Message = "获取数据失败" };
            try
            {
                switch (Models.Config.Configurations.Properties.ConnType?.ToLower())
                {
                    case "api":
                        result = ApiHelper.Instance.SendMessage(new CsharpHttpHelper.HttpItem
                        {
                            URL = ServiceRoute.GetAllMaintenanceData,
                            Method = "Post",
                            ContentType = "application/json",
                            Postdata = JsonConvert.SerializeObject(new { querystring = filter }),
                        });

                        break;
                    case "sql":
                        result = MaintenanceForm.MaintenanceRecordManager.Instance().GetAll(filter);
                        break;
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Data = null;
                result.Message = ex.Message;
            }
            return result;
        }


        /// <summary>
        /// 获取单条数据
        /// </summary>
        /// <param name="device"></param>
        /// <returns></returns>
        public APIResponseData Single(DriveInfomationModel device)
        {
            APIResponseData result = new APIResponseData { Code = -1, Data = null, Message = "获取数据失败" };
            try
            {
                switch (Models.Config.Configurations.Properties.ConnType?.ToLower())
                {
                    case "api":
                        result = ApiHelper.Instance.SendMessage(new CsharpHttpHelper.HttpItem
                        {
                            URL = "api/MaintenanceRecord/Single",
                            Method = "Post",
                            ContentType = "application/json",
                            Postdata = JsonConvert.SerializeObject(device.ToArrayAndCompress()),
                        });

                        break;
                    case "sql":
                        result = MaintenanceForm.MaintenanceRecordManager.Instance().Single(device);
                        break;
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Data = null;
                result.Message = ex.Message;
            }
            return result;
        }

        /// <summary>
        /// 获取保养数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public APIResponseData GetDetailById(int id)
        {
            APIResponseData result = new APIResponseData { Code = -1, Data = null, Message = "获取数据失败" };
            try
            {
                switch (Models.Config.Configurations.Properties.ConnType?.ToLower())
                {
                    case "api":
                        result = ApiHelper.Instance.SendMessage(new CsharpHttpHelper.HttpItem
                        {
                            URL = "api/MaintenanceRecord/GetDetailById",
                            Method = "Post",
                            ContentType = "application/json",
                            Postdata = JsonConvert.SerializeObject(new { AutoID = id })
                        });

                        break;
                    case "sql":

                        result = MaintenanceForm.MaintenanceRecordManager.Instance().GetDetailById(id);

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

        /// <summary>
        /// 新增保养数据
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public APIResponseData Insertable(MaintenanceRecordSubmit entity)
        {
            APIResponseData result = new APIResponseData { Code = -1, Data = null, Message = "插入数据失败" };
            try
            {
                switch (Models.Config.Configurations.Properties.ConnType?.ToLower())
                {
                    case "api":
                        result = ApiHelper.Instance.SendMessage(new CsharpHttpHelper.HttpItem
                        {
                            URL = ServiceRoute.AddMaintenanceData,
                            Method = "Post",
                            ContentType = "application/json",
                            Postdata = JsonConvert.SerializeObject(entity.ToArrayAndCompress())
                        });

                        break;
                    case "sql":

                        result = MaintenanceForm.MaintenanceRecordManager.Instance().Insertable(entity, new HeaderModel
                        {
                            ClientMac = ComputerHelper.GetMacAddress,
                            ClientName = ComputerHelper.GetComputerName,
                            IPAddress = ComputerHelper.GetIPAddress,
                            Operator = GlobalInfo.CurrentUser.AutoID,
                            OperatorName = GlobalInfo.CurrentUser.RealName
                        });

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