using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.WebUtilities;
using Microsoft.AspNetCore.Mvc.Filters;
using economy.Models;
using economy.Helpers;
using economy.Models.Price.Domestic;
using Item = economy.Models.Price.Domestic.Item;

namespace economy.Controllers.Price.Domestic
{
    public class ItemController : Controller
    {
        private readonly DataGoKR _dataGoKR;
        private readonly DomesticModel _domesticModel;
        private Dictionary<string, string> _queryString;

        public ItemController(DataGoKR dataGoKR)
        {
            _dataGoKR = dataGoKR;
            _domesticModel = new DomesticModel(_dataGoKR, null);
            _queryString = [];
        }

        public override void OnActionExecuting(ActionExecutingContext context)
        {
            _queryString = QueryHelpers.ParseQuery(HttpContext.Request.QueryString.Value).ToDictionary(k => k.Key, v => string.Join(",", v.Value));

            ViewBag.QueryString = _queryString;

            base.OnActionExecuting(context);
        }

        public async Task<IActionResult> Index(Item.List.Request request)
        {
            return await List(request);
        }

        // 품목별 시세
        [HttpGet("Price/Domestic/Item/List.cshtml")]
        public async Task<IActionResult> List(Item.List.Request request)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            Item.List.Response itemList = await _domesticModel.GetPriceItemList(request);

            var viewModel = new View<Item.List.Request, Item.List.Response>();
            viewModel.SelectedListPerPage = request.NumOfRows;
            viewModel.Request = request;
            viewModel.Response = itemList;

            var pagination = new Pagination(itemList.Body?.TotalCount, request.PageNo, request.NumOfRows, null);
            viewModel.Pagination = pagination;

            return View("/Views/Price/Domestic/Item/List.cshtml", viewModel);
        }

        // 품목 가격 정보
        [HttpGet("Price/Domestic/Item/Detail/{item}")]
        public async Task<IActionResult> Detail([FromRoute] string item, [Bind] Item.Detail.Request request)
        {
            request.ItemCode = item;

            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            Item.Detail.Response itemInfo = await _domesticModel.GetPriceItemInfo(request);

            if (itemInfo.Body is not null)
            {
                int listNum = Common.CalcListNumber(itemInfo.Body.TotalCount, request.PageNo, request.NumOfRows);
                itemInfo.Body.Items.ItemList = itemInfo.Body.Items.ItemList.Select((row, index) =>
                {
                    row.Num = listNum - index;
                    return row;
                }).ToList();
            }

            var viewModel = new View<Item.Detail.Request, Item.Detail.Response>();
            viewModel.SelectedListPerPage = request.NumOfRows;
            viewModel.Request = request;
            viewModel.Response = itemInfo;

            var queryString = new
            {
                sDate = request.StartDate.ToString("yyyy-MM-dd"),
                eDate = request.EndDate.ToString("yyyy-MM-dd")
            };

            var pagination = new Pagination(itemInfo.Body?.TotalCount, request.PageNo, request.NumOfRows, queryString);
            viewModel.Pagination = pagination;

            return View("/Views/Price/Domestic/Item/Detail.cshtml", viewModel);
        }
    }
}
