using Microsoft.AspNetCore.Mvc;
using Serilog;
using WorkFlowCheck.BL.Services;
using WorkFlowCheck.BL.Services.Interfaces;
using WorkFlowCheck.Common.DTO;

namespace WorkFlowCheck.API.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class CheckListController : ControllerBase
    {
        private ICheckListService _checkListService;
        public CheckListController(ICheckListService checkListService)
        {
            _checkListService = checkListService;
        }

        [HttpGet("GetCheckListHeader/{id}")]
        public async Task<ApiResponseDTO<CheckListHeaderDTO>> GetCheckListHeader(int id)
        {
            var retVal = new ApiResponseDTO<CheckListHeaderDTO>()
            {
                IsSuccess = true,
            };
            var checkListHeaderDTO = await _checkListService.GetCheckListHeaderAsync(id);

            if (checkListHeaderDTO != null)
            {

                retVal.IsSuccess = true;
                retVal.Data = checkListHeaderDTO;
            }
            else
            {
                retVal.IsSuccess = false;
                retVal.Errors.Add("No data!");
            }

            return retVal;
        }
        [HttpGet("CreatePDF/{id}")]
        public async Task<ApiResponseDTO<byte[]>> CreatePDF(int id)
        {
            var retVal = new ApiResponseDTO<byte[]>()
            {
                IsSuccess = true,
            };
            var checkListHeader = await _checkListService.GetCheckListHeaderAsync(id);
            var response = await _checkListService.CreatePDF(id);

            if (response != null)
            {

                retVal.IsSuccess = true;
                retVal.Data = response;
            }
            else
            {
                retVal.IsSuccess = false;
                retVal.Errors.Add("No data!");
            }

            return retVal;
        }
        [HttpGet("GetAllCheckListHeaders")]
        public async Task<ApiResponseDTO<List<CheckListHeaderDTO>>> GetAllCheckListHeadersAsync()
        {
            var retVal = new ApiResponseDTO<List<CheckListHeaderDTO>>()
            {
                IsSuccess = true,
            };
            var checkPointListDTO = await _checkListService.GetAllCheckListHeaderAsync();

            if (checkPointListDTO != null)
            {

                retVal.IsSuccess = true;
                retVal.Data = checkPointListDTO;
            }
            else
            {
                retVal.IsSuccess = false;
                retVal.Errors.Add("No data!");
            }

            return retVal;
        }
        [HttpPost("UpdateCheckListHeader")]
        public async Task<ApiResponseDTO<CheckListHeaderDTO>> UpdateCheckListHeader([FromBody] CheckListHeaderDTO checkListHeaderDTO)
        {
            var retVal = new ApiResponseDTO<CheckListHeaderDTO>()
            {
                IsSuccess = true
            };

            try
            {
                var result = await _checkListService.UpdateCheckListHeaderAsync(checkListHeaderDTO);
                retVal.Data = result;

            }
            catch (Exception ex)
            {
                retVal.IsSuccess = false;
                Log.Error(ex.Message);
            }
            return retVal;
        }
        [HttpPost("StartNewCheckList")]
        public async Task<ApiResponseDTO<CheckListHeaderDTO>> StartNew([FromBody] CheckListHeaderNewDTO checkListHeaderNewDTO)
        {
            var retVal = new ApiResponseDTO<CheckListHeaderDTO>()
            {
                IsSuccess = true
            };

            try
            {
                var response = await _checkListService.StartNewCheckListAsync(checkListHeaderNewDTO);
                if (response != null)
                {
                    retVal.Data = response;
                }
            }
            catch (Exception ex)
            {

                Log.Error(ex.Message);
            }
            return retVal;
        }


        [HttpGet("GetCheckListTemplateHeader/{id}")]
        public async Task<ApiResponseDTO<CheckListTemplateHeaderDTO>> GetCheckListTemplateHeader(int id)
        {
            var retVal = new ApiResponseDTO<CheckListTemplateHeaderDTO>()
            {
                IsSuccess = true,
            };
            var CheckListTemplateHeaderDTO = await _checkListService.GetCheckListTemplateHeaderAsync(id);

            if (CheckListTemplateHeaderDTO != null)
            {

                retVal.IsSuccess = true;
                retVal.Data = CheckListTemplateHeaderDTO;
            }
            else
            {
                retVal.IsSuccess = false;
                retVal.Errors.Add("No data!");
            }

            return retVal;
        }
        [HttpGet("GetAllCheckListTemplateHeaders")]
        public async Task<ApiResponseDTO<List<CheckListTemplateHeaderDTO>>> GetAllCheckListTemplateHeadersAsync()
        {
            var retVal = new ApiResponseDTO<List<CheckListTemplateHeaderDTO>>()
            {
                IsSuccess = true,
            };
            var checkPointListDTO = await _checkListService.GetAllCheckListTemplateHeaderAsync();

            if (checkPointListDTO != null)
            {

                retVal.IsSuccess = true;
                retVal.Data = checkPointListDTO;
            }
            else
            {
                retVal.IsSuccess = false;
                retVal.Errors.Add("No data!");
            }

            return retVal;
        }
        [HttpPost("UpdateCheckListTemplateHeader")]
        public async Task<ApiResponseDTO<CheckListTemplateHeaderDTO>> UpdateCheckListTemplateHeader([FromBody] CheckListTemplateHeaderDTO CheckListTemplateHeaderDTO)
        {
            var retVal = new ApiResponseDTO<CheckListTemplateHeaderDTO>()
            {
                IsSuccess = true
            };

            try
            {
                var result = await _checkListService.UpdateCheckListTemplateHeaderAsync(CheckListTemplateHeaderDTO);
                retVal.Data = result;

            }
            catch (Exception ex)
            {
                retVal.IsSuccess = false;
                Log.Error(ex.Message);
            }
            return retVal;
        }


        [HttpGet("GetCheckListTemplateRow/{id}")]
        public async Task<ApiResponseDTO<CheckListTemplateRowDTO>> GetCheckListTemplateRow(int id)
        {
            var retVal = new ApiResponseDTO<CheckListTemplateRowDTO>()
            {
                IsSuccess = true,
            };
            var checkListTemplateRowDTO = await _checkListService.GetCheckListTemplateRowAsync(id);

            if (checkListTemplateRowDTO != null)
            {

                retVal.IsSuccess = true;
                retVal.Data = checkListTemplateRowDTO;
            }
            else
            {
                retVal.IsSuccess = false;
                retVal.Errors.Add("No data!");
            }

            return retVal;
        }
        [HttpPost("UpdateCheckListTemplateRow")]
        public async Task<ApiResponseDTO<CheckListTemplateRowDTO>> UpdateCheckListTemplateRow([FromBody] CheckListTemplateRowDTO CheckListTemplateRowDTO)
        {
            var retVal = new ApiResponseDTO<CheckListTemplateRowDTO>()
            {
                IsSuccess = true
            };

            try
            {
                var result = await _checkListService.UpdateCheckListTemplateRowAsync(CheckListTemplateRowDTO);
                retVal.Data = result;

            }
            catch (Exception ex)
            {
                retVal.IsSuccess = false;
                Log.Error(ex.Message);
            }
            return retVal;
        }

        [HttpGet("GetCheckListRow/{id}")]
        public async Task<ApiResponseDTO<CheckListRowDTO>> GetCheckListRow(int id)
        {
            var retVal = new ApiResponseDTO<CheckListRowDTO>()
            {
                IsSuccess = true,
            };
            var checkListRowDTO = await _checkListService.GetCheckListRowAsync(id);

            if (checkListRowDTO != null)
            {

                retVal.IsSuccess = true;
                retVal.Data = checkListRowDTO;
            }
            else
            {
                retVal.IsSuccess = false;
                retVal.Errors.Add("No data!");
            }

            return retVal;
        }
        [HttpPost("UpdateCheckListRow")]
        public async Task<ApiResponseDTO<CheckListRowDTO>> UpdateCheckListRow([FromBody] CheckListRowDTO CheckListRowDTO)
        {
            var retVal = new ApiResponseDTO<CheckListRowDTO>()
            {
                IsSuccess = true
            };

            try
            {
                var result = await _checkListService.UpdateCheckListRowAsync(CheckListRowDTO);
                retVal.Data = result;

            }
            catch (Exception ex)
            {
                retVal.IsSuccess = false;
                Log.Error(ex.Message);
            }
            return retVal;
        }
    }
}
