using Backend.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Repository.BackendRepository.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Backend.Controllers
{
    public class ServicePlanController : MybaseController<ServicePlanController>
    {
        private readonly IBackendRepository backendRepository;
        public ServicePlanController(IBackendRepository backendRepository)
        {
            this.backendRepository = backendRepository;
        }
        public IActionResult Index()
        {
            return View();
        }

        public async Task<ApiResult<string>> SavePlan (BackgroundServicePlan plan)
        {
            ApiResult<string> apiResult = new ApiResult<string>();
            try
            {
                if (plan.Id == 0)
                {
                    Dictionary<string, object> Plan = new Dictionary<string, object>()
                    {
                        { "@status", plan.status},
                        { "@plan_name", plan.Plane_name},
                        { "@building_guid", plan.building_guid},
                        { "@target_table", plan.Target_table},
                        { "@execution_time", plan.Execution_time},
                        { "@execution_type", plan.Execution_type},
                        { "@start_time", plan.Start_time}
                    };
                    if(plan.End_time == "0001-01-01 00:00:00")
                    {
                        Plan.Add("@end_time", null);
                    }
                    else
                    {
                        Plan.Add("@end_time", plan.End_time);
                    }

                    await backendRepository.AddOneByCustomTable(Plan, "background_service_plan");

                    apiResult.Code = "0000";
                    apiResult.Msg = "新增成功";
                }
                else
                {
                    Dictionary<string, object> Plan = new Dictionary<string, object>()
                    {
                        { "@status", plan.status},
                        { "@plan_name", plan.Plane_name},
                        { "@building_guid", plan.building_guid},
                        { "@target_table", plan.Target_table},
                        { "@execution_time", plan.Execution_time},
                        { "@execution_type", plan.Execution_type},
                        { "@start_time", plan.Start_time}
                    };
                    if (plan.End_time == "0001-01-01 00:00:00")
                    {
                        Plan.Add("@end_time", null);
                    }
                    else
                    {
                        Plan.Add("@end_time", plan.End_time);
                    }
                    await backendRepository.UpdateOneByCustomTable(Plan, "background_service_plan", "id='" + plan.Id + "'");

                    apiResult.Code = "0000";
                    apiResult.Msg = "修改成功";
                }
            }
            catch (Exception exception)
            {
                apiResult.Code = "9999";
                apiResult.Msg = "系統內部錯誤,請聯絡管理者。";
                string json = System.Text.Json.JsonSerializer.Serialize(plan);
                Logger.LogError("【" + controllerName + "/" + actionName + "】" + json);
                Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message);
            }
            return apiResult;
        }

        public async Task<ApiResult<List<PlanTable>>> GetPlanTable()
        {
            ApiResult<List<PlanTable>> apiResult = new ApiResult<List<PlanTable>>();
            try
            {
                var plans = await backendRepository.GetAllAsync<PlanTable>("select bsp.*,b.full_name building from background_service_plan bsp left join building b on bsp.building_guid = b.building_guid where bsp.deleted = 0 order by bsp.id desc");
                foreach(var plan in plans)
                {
                    var timetype = plan.Execution_type switch
                    {
                        0 => "分",
                        1 => "小時",
                        2 => "天",
                        3 => "週",
                        4 => "月",
                        _ => "",
                    };

                    plan.execution = "每" + plan.Execution_time + timetype;

                    plan.time = plan.Start_time + @"<br>-<br>";
                    if(plan.End_time != "0001-01-01 00:00:00")
                    {
                        plan.time += plan.End_time;
                    }
                }
                apiResult.Code = "0000";
                apiResult.Data = plans;

            }
            catch (Exception exception)
            {
                apiResult.Code = "9999";
                apiResult.Msg = "系統內部錯誤,請聯絡管理者。";
                Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message);
            }
            return apiResult;
        }

        public async Task<ApiResult<BackgroundServicePlan>> GetonePlan (int id)
        {
            ApiResult<BackgroundServicePlan> apiResult = new ApiResult<BackgroundServicePlan>();
            try
            {
                var plan = await backendRepository.GetOneAsync<BackgroundServicePlan>("background_service_plan", $" id = {id}");
                apiResult.Data = plan;
                apiResult.Code = "0000";
            }
            catch (Exception exception)
            {
                apiResult.Code = "9999";
                apiResult.Msg = "系統內部錯誤,請聯絡管理者。";
                Logger.LogError("【" + controllerName + "/" + actionName + "】" + id);
                Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message);
            }
            return apiResult;
        }

        [HttpPost]
        public async Task<ApiResult<string>> DeletePlan(int id)
        {
            ApiResult<string> apiResult = new ApiResult<string>();

            try
            {
                await backendRepository.DeleteOne(id.ToString(), "background_service_plan", "id");

                apiResult.Code = "0000";
                apiResult.Msg = "刪除成功";
            }
            catch (Exception exception)
            {
                apiResult.Code = "9999";
                apiResult.Msg = "系統內部錯誤,請聯絡管理者。";
                Logger.LogError("【" + controllerName + "/" + actionName + "】" + "id=" + id);
                Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message);
            }

            return apiResult;
        }
    }
}