using Microsoft.AspNetCore.Mvc;
using economy.Helpers;
using economy.Models;
using economy.Models.Days;
using Anniversary = economy.Models.Days.Anniversary;
using Holiday = economy.Models.Days.Holiday;
using Seasonal = economy.Models.Days.Seasonal;
using Sundry = economy.Models.Days.Sundry;

namespace economy.Controllers
{
    public class DaysController : Controller
    {
        private readonly DataGoKR _dataGoKR;
        private Dictionary<string, string> _queryString;

        public DaysController(DataGoKR dataGoKR)
        {
            _dataGoKR = dataGoKR;
            _queryString = [];
        }

        // 공휴일/국경일
        public async Task<IActionResult> Holiday(Holiday.Request request)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            DaysModel holiday = new DaysModel(_dataGoKR);
            Holiday.Response itemList = await holiday.GetHoliday(request);

            if (itemList.Body is not null)
            {
                itemList.Body.Items.ItemList = itemList.Body.Items.ItemList.Select((row, index) =>
                {
                    row.Num += (index + 1);
                    row.LocDate = Common.StringToDateFormat(row.LocDate);
                    return row;
                }).ToList();
            }

            var viewModel = new View<Holiday.Request, Holiday.Response>();
            viewModel.Request = request;
            viewModel.Response = itemList;

            // 출력 연도 지정
            ViewData["years"] = Enumerable.Range(2000, DateTime.Now.Year - 2000 + 1).Reverse();
            ViewData["type"] = "Holiday";

            return View(viewModel);
        }

        // 기념일
        public async Task<IActionResult> Anniversary(Anniversary.Request request)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            DaysModel anniversary = new DaysModel(_dataGoKR);
            Anniversary.Response itemList = await anniversary.GetAnniversary(request);

            if (itemList.Body is not null)
            {
                itemList.Body.Items.ItemList = itemList.Body.Items.ItemList.Select((row, index) =>
                {
                    row.Num += (index + 1);
                    row.LocDate = Common.StringToDateFormat(row.LocDate);
                    return row;
                }).ToList();
            }

            var viewModel = new View<Anniversary.Request, Anniversary.Response>();
            viewModel.Request = request;
            viewModel.Response = itemList;

            // 출력 연도 지정
            ViewData["years"] = Enumerable.Range(2000, DateTime.Now.Year - 2000 + 1).Reverse();
            ViewData["type"] = "Anniversary";

            return View(viewModel);
        }

        // 24절기
        public async Task<IActionResult> Seasonal(Seasonal.Request request)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            DaysModel seasonal = new DaysModel(_dataGoKR);
            Seasonal.Response itemList = await seasonal.GetSeasonal(request);

            if (itemList.Body is not null)
            {
                itemList.Body.Items.ItemList = itemList.Body.Items.ItemList.Select((row, index) =>
                {
                    row.Num += (index + 1);
                    row.LocDate = Common.StringToDateFormat(row.LocDate);
                    return row;
                }).ToList();
            }

            var viewModel = new View<Seasonal.Request, Seasonal.Response>();
            viewModel.Request = request;
            viewModel.Response = itemList;

            // 출력 연도 지정
            ViewData["years"] = Enumerable.Range(2000, DateTime.Now.Year - 2000 + 1).Reverse();
            ViewData["type"] = "Seasonal";

            return View(viewModel);
        }


        // 잡절
        public async Task<IActionResult> Sundry(Sundry.Request request)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            DaysModel sundry = new DaysModel(_dataGoKR);
            Sundry.Response itemList = await sundry.GetSundry(request);

            if (itemList.Body is not null)
            {
                itemList.Body.Items.ItemList = itemList.Body.Items.ItemList.Select((row, index) =>
                {
                    row.Num += (index + 1);
                    row.LocDate = Common.StringToDateFormat(row.LocDate);
                    return row;
                }).ToList();
            }

            var viewModel = new View<Sundry.Request, Sundry.Response>();
            viewModel.Request = request;
            viewModel.Response = itemList;

            // 출력 연도 지정
            ViewData["years"] = Enumerable.Range(2000, DateTime.Now.Year - 2000 + 1).Reverse();
            ViewData["type"] = "Sundry";

            return View(viewModel);
        }
    }
}
