using CsharpHttpHelper;
using DeviceRepairAndOptimization.Models;
using DeviceRepairAndOptimization.Utils;
using Newtonsoft.Json;
using System;
using System.Threading.Tasks;

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

        /// <summary>
        /// 获取当前年月的待保修项
        /// </summary>
        /// <returns></returns>
        public APIResponseData GetCurrentMonthPlanTips()
        {
            APIResponseData result = new APIResponseData { Code = -1, Message = "未能获取到数据！" };
            try
            {
                switch (Models.Config.Configurations.Properties.ConnType?.ToLower())
                {
                    case "api":
                        result = ApiHelper.Instance.SendMessage(new HttpItem
                        {
                            URL = ServiceRoute.GetCurrentMonthPlanTips,
                            Method = "Post",
                            ContentType = "application/json;charset=utf-8"
                        });
                        break;

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


        public async Task<int> GetCurrentMonthPlanTipsCountAsync(ResultHandler handler)
        {
            int Count = -1;

            //(s) =>
            //{
            //    //获取返回值
            //    APIResponseData apiResponseData = JsonConvert.DeserializeObject<APIResponseData>(s.Html);
            //    Count = apiResponseData.ToInt();

            //}

            try
            {
                switch (Models.Config.Configurations.Properties.ConnType?.ToLower())
                {
                    case "api":
                        ApiHelper.Instance.SendMessageAsync(new HttpItem
                        {
                            URL = "api/plan/GetCurrentMonthPlanTipsCountAsync",
                            Method = "Post",
                            ContentType = "application/json;charset=utf-8"
                        }, handler);
                        break;

                    default:
                        APIResponseData result = await Data.PlanManager.PlanAccess.Instance.GetCurrentMonthPlanTipsCountAsync();
                        Count = result.ToInt();
                        break;
                }
            }
            catch (Exception ex)
            {

            }
            return Count;
        }


        /// <summary>
        /// 获取当前年月的待保修项数量
        /// </summary>
        /// <returns></returns>
        public APIResponseData GetCurrentMonthPlanTipsCount()
        {
            APIResponseData result = new APIResponseData { Code = -1, Message = "未能获取到数据！" };
            try
            {
                switch (Models.Config.Configurations.Properties.ConnType?.ToLower())
                {
                    case "api":
                        result = ApiHelper.Instance.SendMessage(new HttpItem
                        {
                            URL = ServiceRoute.GetCurrentMonthPlanTipsCount,
                            Method = "Post",
                            ContentType = "application/json;charset=utf-8"
                        });
                        break;

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