using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.WebUtilities;
using Microsoft.AspNetCore.Mvc.Filters;
using economy.Models;
using economy.Helpers;
using Request = economy.Models.Product;
using Response = economy.Models.Product;
using economy.Models.Product;

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

        public PriceController(DataGoKR dataGoKR)
        {
            _dataGoKR = dataGoKR;
            productModel = new ProductModel(_dataGoKR);
        }

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

        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);
        }

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

            Response.List.Response itemList = await productModel.GetPriceItemList(request);

            var viewModel = new View<Request.List.Request, Response.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(viewModel);
        }

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

            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }
        
            Response.Detail.Response itemInfo = await productModel.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<Response.Detail.Request, Response.Detail.Response>();
            viewModel.SelectedListPerPage = request.NumOfRows;
            viewModel.Request = request;
            viewModel.Response = itemInfo;

            var queryString = new
            {
                sDate = request.StartDate,
                eDate = request.EndDate
            };

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

            return View(viewModel);
        }
    }
}
