using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Mvc.Rendering;
using Serilog;
using WorkFlowCheck.Common.DTO;
using WorkFlowCheck.Web.Services.Interfaces;

namespace WorkFlowCheck.Web.Pages.CheckListTemplate.CheckListTemplateRows
{
    public class CheckListTemplateRowEditPageModel : PageModel
    {
        private readonly ILogger<IndexModel> _logger;
        private readonly ICheckListService _checkListService;
        private readonly IBaseStockService _baseStockService;


        [BindProperty]
        public CheckListTemplateRowDTO CheckListTemplateRow { get; set; }

        [BindProperty]
        public List<SelectListItem> AnswerTypes { get; set; } =
            new[] { "I-N", "P", "SI-N", "I-SN", "PI-N", "I-PN", "V" }
            .Select(x => new SelectListItem { Value = x, Text = x })
            .ToList();

        [BindProperty]
        public List<SelectListItem> CheckPoints { get; set; }

        [BindProperty]
        public List<SelectListItem> Equipments { get; set; }

        public CheckListTemplateRowEditPageModel(ILogger<IndexModel> logger, ICheckListService checkListService, IBaseStockService baseStockService)
        {
            _checkListService = checkListService;
            _baseStockService = baseStockService;
            _logger = logger;
        }

        public async Task OnGet(int id)
        {
            await InitSelectItems();
            CheckListTemplateRow = await _checkListService.GetCheckListTemplateRow(id);
        }

        public async Task OnGetNew(int parentId)
        {
            await InitSelectItems();

            var checkListTemplateHeader = await _checkListService.GetCheckListTemplateHeader(parentId);

            if (checkListTemplateHeader != null)
            {
                int rowIndex = 1;
                int equipmentId = 0;
                int checkPointId = 0;
                
                if (checkListTemplateHeader.CheckListTemplateRowDTO.Count() > 0)
                {
                    var checkListTemplateRowMax = checkListTemplateHeader.CheckListTemplateRowDTO.OrderByDescending(o => o.RowIndex).FirstOrDefault();
                    if (checkListTemplateRowMax != null)
                    {
                        rowIndex = checkListTemplateRowMax.RowIndex + 1;
                        checkPointId=checkListTemplateRowMax.CheckPointId;
                        equipmentId= checkListTemplateRowMax.EquipmentId;   
                    }
                }

                CheckListTemplateRow = new CheckListTemplateRowDTO()
                {
                    Id = 0,
                    CheckListTemplateHeaderId = parentId,
                    RowIndex = rowIndex,
                    EquipmentId = equipmentId,  
                    CheckPointId = checkPointId,    
                    AnswerType = "I-N"
                };
            }

        }
        public async Task<IActionResult> OnPostSave([FromBody] CheckListTemplateRowDTO checkListTemplateRowDTO)
        {
            try
            {
                var result = await _checkListService.UpdateCheckListTemplateRow(checkListTemplateRowDTO);
                if (result.IsSuccess)
                {
                    return new JsonResult(new { success = true });
                }
                else
                {
                    return new JsonResult(new { success = false });
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex.Message);
            }

            return new JsonResult(new { success = false });
        }
        private async Task InitSelectItems()
        {
            var checkPoints = await _baseStockService.GetAllCheckPoints();
            this.CheckPoints = new List<SelectListItem>();
            foreach (var checkPoint in checkPoints)
            {
                this.CheckPoints.Add(new SelectListItem() { Value = checkPoint.Id.ToString(), Text = checkPoint.ShortName });
            }
            var equipments = await _baseStockService.GetAllEquipments();
            this.Equipments = new List<SelectListItem>();
            foreach (var equipment in equipments)
            {
                this.Equipments.Add(new SelectListItem() { Value = equipment.Id.ToString(), Text = equipment.ShortName });
            }
        }

    }
}
