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

namespace SolarPower.Controllers
{
    public class NoticeScheduleController : MyBaseController<NoticeScheduleController>
    {
        private readonly INoticeScheduleRepository noticeScheduleRepository;

        public NoticeScheduleController(INoticeScheduleRepository noticeScheduleRepository) : base()
        {
            this.noticeScheduleRepository = noticeScheduleRepository;
        }

        public IActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public async Task<ActionResult> NoticeTable(NoticeTableSearch info)
        {
            List<NoticeScheduleTable> NoticeScheduleTable = new List<NoticeScheduleTable>();
            ApiResult<List<NoticeScheduleTable>> apiResult = new ApiResult<List<NoticeScheduleTable>>();
            try
            {
                NoticeScheduleTable = await noticeScheduleRepository.GetNoticeTable(info);
                foreach(NoticeScheduleTable a in NoticeScheduleTable)
                {
                    a.EmailTypeName = a.EmailType switch 
                    {
                        0 => "日報表",
                        1 => "月報表",
                        2 => "綜合報表",
                        3 => "運維通知",
                        4 => "異常通知",
                        _ => ""
                    };
                    a.IsDeliveryName = a.IsDelivery switch
                    {
                        1 => "成功",
                        2 => "失敗",
                        _ => ""
                    };
                }
                apiResult.Code = "0000";
                apiResult.Data = NoticeScheduleTable;
            }
            catch (Exception exception)
            {
                apiResult.Code = "9999";
                apiResult.Msg = exception.ToString();

                Logger.LogError("【" + controllerName + "/" + actionName + "】" + "info=" + System.Text.Json.JsonSerializer.Serialize(info));
                Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message);
            }
            var result = Json(new
            {
                data = apiResult
            });
            return result;
        }
    }
}
