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

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

        public DayController(DataGoKR dataGoKR)
        {
            _dataGoKR = dataGoKR;
        }

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

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

            if (itemList 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<Models.Holiday.Request, Models.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(Models.Anniversary.Request request)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

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

            if (itemList 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<Models.Anniversary.Request, Models.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(Models.Seasonal.Request request)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

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

            if (itemList 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<Models.Seasonal.Request, Models.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(Models.Sundry.Request request)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

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

            if (itemList 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<Models.Sundry.Request, Models.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);
        }
    }
}
