using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Api.SignToken;
using Hangfire;
using MadRunFabric.Common;
using MadRunFabric.Model.HangfireApi.Model;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;

namespace HangfireApi.Controllers
{
    [ApiVersion("6.0")]
    [Produces("application/json")]
    [Route("api/[controller]")]
    public class EquipmentController : BaseController
    {
        private readonly ILogger<EquipmentController> _logger;
        private readonly IEquipmentService _equipmentService;
        public EquipmentController(
            ILogger<EquipmentController> logger, IEquipmentService equipmentService
            )
        {
            _logger = logger;
            _equipmentService = equipmentService;
        }

        #region 保养计划
        /// <summary>
        /// 保养计划
        /// </summary>
        /// <param name="id">设备id</param>
        /// <param name="action"></param>
        /// <param name="day">周期天数</param>
        /// <param name="hour">周期小时</param>
        /// <returns></returns>
        [HttpPost("addwomaintainbase")]
        public IActionResult AddWoMaintainBaseAsync(string id, int plancycle, int planinterval, int plantimes)
        {
            _logger.LogError("AddWoMaintainBaseAsync start");
            string dd = "";
            #region
            //ptype: 1每天，2每周，3每月 
            string cycle = "";
            if (plancycle == (int)EnumPlanCycle.yearly)
            {
                //每年执行
                dd = Cron.Yearly();
                cycle = "每年";
            }
            else if (plancycle == (int)EnumPlanCycle.monthly)
            {
                if (planinterval > 1)
                {//间隔>1 即为几月执行一次
                    dd = Cron.MonthInterval(planinterval);
                }
                else
                {//每月第几天执行1次
                    dd = Cron.Monthly();
                }
                cycle = "每月";
            }
            else if (plancycle == (int)EnumPlanCycle.darly)
            {
                if (planinterval > 1)
                {//间隔>1 即为几天执行一次
                    dd = Cron.DayInterval(planinterval);
                }
                else
                {//每天执行几次
                    dd = Cron.Daily();
                }
                cycle = "每天";
            }
            #endregion
            _logger.LogError("保养计划 Cron start cycle:" + cycle + " express:" + dd);
            RecurringJob.AddOrUpdate(id, () => httpAsync(id), dd, TimeZoneInfo.Local);
            _logger.LogError("AddWoMaintainBaseAsync end");
            return Success($"定时任务执行  Recurring ");
        }
        public async Task httpAsync(string id)
        {
            var res = await _equipmentService.AddWoMaintainBaseAsync(id);
            _logger.LogError("AddWoMaintainBaseAsync res =" + res);
        }
        #region
        ///// <summary>
        ///// 保养计划
        ///// </summary>
        ///// <param name="id">设备id</param>
        ///// <param name="action"></param>
        ///// <param name="day">周期天数</param>
        ///// <param name="hour">周期小时</param>
        ///// <returns></returns>
        //[HttpPost("addwomaintainbase")]
        //public IActionResult AddWoMaintainBaseAsync(string proname, string sysname, string equipname, 
        //    string id, string expirytime, int advancelong,string content,string maintype, 
        //    int plancycle, int plandate, int planhour, int planminute = 0)
        //{

        //    _logger.LogError("AddWoMaintainBaseAsync start");
        //    string dd = "";
        //    #region
        //    //ptype: 1每天，2每周，3每月 
        //    string cycle = "";
        //    if (plancycle == (int)EnumPlanCycle.yearly)
        //    {
        //        dd = Cron.Yearly(planhour, planminute);//每天执行
        //        cycle = "每年";
        //    }
        //    else if (plancycle == (int)EnumPlanCycle.monthly)
        //    {
        //        dd = Cron.Monthly(plandate, planhour);
        //        cycle = "每月";
        //    }
        //    else if (plancycle == (int)EnumPlanCycle.weekly)
        //    {
        //        dd = Cron.Weekly((DayOfWeek)plandate, planhour);
        //        cycle = "每周";
        //    }
        //    else if (plancycle == (int)EnumPlanCycle.darly)
        //    {
        //        dd = Cron.Daily(planhour, planminute);//每天执行
        //        cycle = "每天";
        //    }
        //    #endregion
        //    _logger.LogError("保养计划 Cron start cycle:" + cycle + " express:" + dd);
        //    RecurringJob.AddOrUpdate(id, () => httpAsync(proname, sysname, equipname, id, expirytime, advancelong,content,maintype), dd, TimeZoneInfo.Local);
        //    _logger.LogError("AddWoMaintainBaseAsync end");
        //    return Success($"定时任务执行  Recurring ");
        //}
        //public async Task httpAsync(string proname, string sysname, string equipname, string id, string expirytime, int advancelong, string content, string maintype)
        //{
        //    var res = await _equipmentService.AddWoMaintainBaseAsync(proname, sysname, equipname, id, expirytime, advancelong,content,maintype);
        //    _logger.LogError("AddWoMaintainBaseAsync res =" + res);
        //}
        #endregion
        ///// <summary>
        ///// 保养计划
        ///// </summary>
        ///// <param name="id">设备id</param>
        ///// <param name="action"></param>
        ///// <param name="day">周期天数</param>
        ///// <param name="hour">周期小时</param>
        ///// <returns></returns>
        //public IActionResult Recurring(string id,string url, HttpContent content, int day,int hour)
        //{
        //    RecurringJob.AddOrUpdate(id, () => httpAsync(clmodel), dd, TimeZoneInfo.Local);
        //    return Success($"定时任务执行  Recurring ");
        //}
        #endregion

        #region 巡检计划

        /// <summary>
        /// 设备运行计划
        /// </summary>
        /// <param name="id">设备id</param>
        /// <param name="action"></param>
        /// <param name="day">周期天数</param>
        /// <param name="hour">周期小时</param>
        /// <returns></returns>
        [HttpPost("addwoequipmentrunning")]
        public IActionResult AddWoEquipmentRunningAsync(string id, int plancycle, int planinterval, int plantimes)
        {

            _logger.LogError("AddWoEquipmentRunningAsync start");
            string dd = "";
            #region
            if(planinterval>1)
            {//间隔>1 即为几天执行一次
                dd = Cron.DayInterval(planinterval);
            }
            else
            {//每天执行几次
                dd = Cron.Daily();
            }
            #region
            ////ptype: 1每天，2每周，3每月 
            //string cycle = "每天";
            //if (plancycle == (int)EnumPlanCycle.darly)
            //    dd = Cron.Daily(planhour, planminute);//每天执行
            //else if (plancycle == (int)EnumPlanCycle.weekly)
            //{
            //    dd = Cron.Weekly((DayOfWeek)plandate, planhour);
            //    cycle = "每周";
            //}
            //else if (plancycle == (int)EnumPlanCycle.monthly)
            //{
            //    dd = Cron.Monthly(plandate, planhour);
            //    cycle = "每月";
            //}
            //else if (plancycle == (int)EnumPlanCycle.hourly)
            //{
            //    dd = Cron.HourInterval(planhour);//每小时执行
            //    cycle = "小时";
            //}
            //else if (plancycle == (int)EnumPlanCycle.minutely)
            //{
            //    dd = Cron.MinuteInterval(planhour);
            //    cycle = "分钟";
            //}
            #endregion
            #endregion
            _logger.LogError("巡检计划 Cron start  express:" + dd + ",planinterval:" + planinterval + ",plantimes" + plantimes);
            RecurringJob.AddOrUpdate(id, () => httpRunningAsync(id), dd, TimeZoneInfo.Local);
            _logger.LogError("AddWoEquipmentRunningAsync end");
            return Success($"定时任务执行  Recurring ");
        }
        public async Task httpRunningAsync(string id)
        {
            _logger.LogError("EquipmentController/httpRunningAsync  ");
            var res = await _equipmentService.AddWoEquipmentRunningAsync(id);
            _logger.LogError("AddWoEquipmentRunningAsync res =" + res);
        }

        #endregion
        
        #region 超时工单
        [HttpPost("savetimeout")]
        public IActionResult SaveTimeOutAsync(string id, int wotype, int plancycle, int planinterval)
        {
            _logger.LogError("SaveTimeOutAsync start");
            #region
            string dd = "0/5 * * * *";//每5分钟执行一次
            //ptype: 超时任务周期：4小时，5分钟，6秒
            string cycle = "小时";
            if (plancycle == (int)EnumPlanCycle.hourly)
            {
                dd = Cron.HourInterval(planinterval);//每小时执行
                cycle = "小时";
            }
            else if (plancycle == (int)EnumPlanCycle.minutely)
            {
                dd = Cron.MinuteInterval(planinterval);
                cycle = "分钟";
            }
            #endregion
            _logger.LogError("超时工单 Cron start cycle:" + cycle+" express:" + dd);
            RecurringJob.AddOrUpdate(id, () => httpTimeOutAsync(wotype), dd, TimeZoneInfo.Local);
            _logger.LogError("SaveTimeOutAsync end");
            return Success($"定时任务执行  Recurring ");
        }
        public async Task httpTimeOutAsync(int wotype)
        {
            var res = await _equipmentService.SaveTimeOutAsync(wotype);
            _logger.LogError("SaveTimeOutAsync res =" + res);
        }
        #endregion

        #region 超时系统评价维修工单
        [HttpPost("timeoutevaluate")]
        public IActionResult UpdateTimeOutEvaluateAsync(string id, int score = 5, string createusername = "系统", int channel = 5)
        {
            _logger.LogError("SaveTimeOutAsync start");

            string dd = "0/5 * * * *";//每5分钟执行一次

            _logger.LogError("超时系统评价维修工单 Cron start  express:" + dd);
            RecurringJob.AddOrUpdate(id, () => _equipmentService.TimeOutEvaluateAsync(score, createusername, channel), dd, TimeZoneInfo.Local);

            _logger.LogError("UpdateTimeOutEvaluateAsync end");
            return Success($"定时任务执行  Recurring ");
        }

        #endregion

        #region 关闭任务
        [HttpPost("remove")]
        public IActionResult removeAsync(string id)
        {
            RecurringJob.RemoveIfExists(id);
            return Success($"关闭定时任务执行  Recurring ");
        }
        #endregion
    }
}