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

namespace WorkFlowCheck.API.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class SyncController : ControllerBase
    {
        private ISyncService _syncService;
        private INumberGeneratorService _numberGeneratorService;    
        public SyncController(ISyncService syncService,INumberGeneratorService numberGeneratorService)
        {
            _syncService = syncService;
            _numberGeneratorService = numberGeneratorService;   
        }


        [HttpGet("GetCheckPoint/{id}")]
        public async Task<ApiResponseDTO<CheckPointDTO>> GetCheckpointAsync(int id)
        {
            var retVal = new ApiResponseDTO<CheckPointDTO>()
            {
                IsSuccess = true,
            };
            var checkPointListDTO = await _syncService.GetCheckPointAsync(id);

            if (checkPointListDTO != null)
            {

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

            return retVal;
        }
        [HttpGet("DeleteCheckPoint/{id}")]
        public async Task<ApiResponseDTO<bool>> DeleteCheckpointAsync(int id)
        {
            var retVal = new ApiResponseDTO<bool>()
            {
                IsSuccess = true,
            };
            var response = await _syncService.DeleteCheckPointAsync(id);

            if (response != null)
            {

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

            return retVal;
        }
        [HttpGet("GetAllCheckPoints")]
        public async Task<ApiResponseDTO<List<CheckPointDTO>>> GetAllCheckpointsAsync()
        {
            var retVal = new ApiResponseDTO<List<CheckPointDTO>>()
            {
                IsSuccess = true,
            };
            var checkPointListDTO = await _syncService.GetAllCheckPointAsync();

            if (checkPointListDTO != null)
            {

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

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

            try
            {
                var result = await _syncService.UpdateCheckPointAsync(checkPointDTO);
                retVal.Data = result;

            }
            catch (Exception ex)
            {
                retVal.IsSuccess = false;
                Log.Error(ex.Message);
            }
            return retVal;
        }
        [HttpPost("UpdateAllCheckPoint")]
        public async Task<ApiResponseDTO<List<CheckPointDTO>>> UpdateAllCheckPoint([FromBody] List<CheckPointDTO> checkPointListDTO)
        {
            var retVal = new ApiResponseDTO<List<CheckPointDTO>>()
            {
                IsSuccess = true
            };

            try
            {
                var result = await _syncService.UpdateAllCheckPointAsync(checkPointListDTO);
                retVal.Data = result;
            }
            catch (Exception ex)
            {
                retVal.IsSuccess = false;
                Log.Error(ex.Message);
            }
            return retVal;
        }


        [HttpGet("GetEquipment/{id}")]
        public async Task<ApiResponseDTO<EquipmentDTO>> GetEquipmentAsync(int id)
        {
            var retVal = new ApiResponseDTO<EquipmentDTO>()
            {
                IsSuccess = true,
            };
            var equipmentDTO = await _syncService.GetEquipmentAsync(id);

            if (equipmentDTO != null)
            {

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

            return retVal;
        }
        [HttpGet("DeleteEquipment/{id}")]
        public async Task<ApiResponseDTO<bool>> DeleteEquipmentAsync(int id)
        {
            var retVal = new ApiResponseDTO<bool>()
            {
                IsSuccess = true,
            };
            var response = await _syncService.DeleteEquipmentAsync(id);

            if (response != null)
            {

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

            return retVal;
        }
        [HttpGet("GetAllEquipments")]
        public async Task<ApiResponseDTO<List<EquipmentDTO>>> GetAllEquipmentsAsync()
        {
            var retVal = new ApiResponseDTO<List<EquipmentDTO>>()
            {
                IsSuccess = true,
            };
            var equipmentListDTO = await _syncService.GetAllEquipmentAsync();

            if (equipmentListDTO != null)
            {

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

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

            try
            {
                var result = await _syncService.UpdateEquipmentAsync(EquipmentDTO);
                retVal.Data = result;

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


        [HttpGet("GetLocation/{id}")]
        public async Task<ApiResponseDTO<LocationDTO>> GetLocationAsync(int id)
        {
            var retVal = new ApiResponseDTO<LocationDTO>()
            {
                IsSuccess = true,
            };
            var locationDTO = await _syncService.GetLocationAsync(id);

            if (locationDTO != null)
            {

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

            return retVal;
        }
        [HttpGet("DeleteLocation/{id}")]
        public async Task<ApiResponseDTO<bool>> DeleteLocationAsync(int id)
        {
            var retVal = new ApiResponseDTO<bool>()
            {
                IsSuccess = true,
            };
            var response = await _syncService.DeleteLocationAsync(id);

            if (response != null)
            {

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

            return retVal;
        }
        [HttpGet("GetAllLocations")]
        public async Task<ApiResponseDTO<List<LocationDTO>>> GetAllLocationsAsync()
        {
            var retVal = new ApiResponseDTO<List<LocationDTO>>()
            {
                IsSuccess = true,
            };
            var locationListDTO = await _syncService.GetAllLocationAsync();

            if (locationListDTO != null)
            {

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

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

            try
            {
                var result = await _syncService.UpdateLocationAsync(LocationDTO);
                retVal.Data = result;

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


        [HttpGet("GetAllCheckListTemplateHeader/{userId}")]
        public async Task<ApiResponseDTO<List<CheckListTemplateHeaderDTO>>> GetAllCheckListTemplateHeaderAsync(int userId)
        {
            var retVal = new ApiResponseDTO<List<CheckListTemplateHeaderDTO>>()
            {
                IsSuccess = true,
            };
            var response = await _syncService.GetAllCheckListTemplateAsync(userId);

            if (response != null)
            {

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

            return retVal;
        }
        [HttpGet("GetAllCheckListHeader/{userId}")]
        public async Task<ApiResponseDTO<List<CheckListHeaderDTO>>> GetAllCheckListHeaderAsync(int userId)
        {
            var retVal = new ApiResponseDTO<List<CheckListHeaderDTO>>()
            {
                IsSuccess = true,
            };
            var response = await _syncService.GetAllCheckListAsync(userId);

            if (response != null)
            {

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

            return retVal;
        }

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

            try
            {
                var result = await _syncService.UpdateCheckListHeaderAsync(checkListHeaderDTO);
                retVal.Data = "Sikeres beküldés!";

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

        [HttpGet("GetAllNumbergenerators")]
        public async Task<ApiResponseDTO<List<NumberGeneratorTemplateDTO>>> GetAllNumbergeneratorsAsync()
        {
            var retVal = new ApiResponseDTO<List<NumberGeneratorTemplateDTO>>()
            {
                IsSuccess = true,
            };
            var locationListDTO = await _numberGeneratorService.GetAllNumbergeneratorsAsync();

            if (locationListDTO != null)
            {

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

            return retVal;
        }


    }
}
