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

namespace WorkFlowCheck.API.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class UserController : ControllerBase
    {
        private IUserService _userService;

        public UserController(IUserService userService)
        {
            _userService = userService;
        }
        
        [HttpGet("GetUser/{id}")]
        public async Task<ApiResponseDTO<UserDTO>> GetUser(int id)
        {
            var retVal = new ApiResponseDTO<UserDTO>()
            {
                IsSuccess = true,
            };
            var userDTO = await _userService.GetUserAsync(id);

            if (userDTO != null)
            {
                if (userDTO.Id == 0)
                {
                    retVal.IsSuccess = false;
                    retVal.Errors.Add("No match!");
                    retVal.Data = userDTO;
                }
                else
                {
                    retVal.IsSuccess = true;
                    retVal.Data = userDTO;
                }
            }
            else
            {
                retVal.IsSuccess = false;
                retVal.Errors.Add("No data!");
            }

            return retVal;
        }
        [HttpGet("GetAllUsers")]
        public async Task<ApiResponseDTO<List<UserDTO>>> GetAllUsers()
        {
            var retVal = new ApiResponseDTO<List<UserDTO>>()
            {
                IsSuccess = true,
            };
            var userDTO = await _userService.GetAllUserAsync();

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

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

            try
            {
                var result = await _userService.UpdateUserAsync(userDTO);
                retVal.Data = result;

            }
            catch (Exception ex)
            {
                retVal.IsSuccess = false;
                Log.Error(ex.Message);
            }
            return retVal;
        }
        [HttpPost("Authenticate")]
        public async Task<ApiResponseDTO<UserDTO>> Authenticate([FromBody] UserSimpleDTO userSimpleDTO)
        {
            var retVal = new ApiResponseDTO<UserDTO>()
            {
                IsSuccess = true
            };
            if (string.IsNullOrEmpty(userSimpleDTO.UserName) || string.IsNullOrEmpty(userSimpleDTO.Password))
            {
                retVal.IsSuccess = false;
                retVal.Errors.Add("Nem megfelelo felhasználói név vagy jelszó");
                return retVal;
            }
            try
            {
                var userDTO_Response = await _userService.Authenticate(userSimpleDTO.UserName, userSimpleDTO.Password);
                if (userDTO_Response != null)
                {
                    if (string.IsNullOrEmpty(userDTO_Response.UserName) == false && userDTO_Response.UserName == userSimpleDTO.UserName)
                    {
                        retVal.IsSuccess = true;
                        retVal.Data = userDTO_Response;
                        Log.Information($"Sikeres azonosítás! {userSimpleDTO.UserName}");
                    }
                    else
                    {
                        var error = $"Nem megfelelo felhasználó vagy jelszó! {userSimpleDTO.UserName}";
                        retVal.IsSuccess = false;
                        retVal.Errors.Add(error);
                        Log.Information(error);
                    }
                }
                else
                {
                    var error = $"Nem megfelelo felhasználó vagy jelszó! {userSimpleDTO.UserName}";
                    retVal.IsSuccess = false;
                    retVal.Errors.Add(error);
                    Log.Information(error);
                }
            }
            catch (Exception ex)
            {

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

        [HttpGet("GetRole/{id}")]
        public async Task<ApiResponseDTO<RoleDTO>> GetRole(int id)
        {
            var retVal = new ApiResponseDTO<RoleDTO>()
            {
                IsSuccess = true,
            };
            var RoleDTO = await _userService.GetRoleAsync(id);

            if (RoleDTO != null)
            {
                if (RoleDTO.Id == 0)
                {
                    retVal.IsSuccess = false;
                    retVal.Errors.Add("No match!");
                    retVal.Data = RoleDTO;
                }
                else
                {
                    retVal.IsSuccess = true;
                    retVal.Data = RoleDTO;
                }
            }
            else
            {
                retVal.IsSuccess = false;
                retVal.Errors.Add("No data!");
            }

            return retVal;
        }
        [HttpGet("GetAllRoles")]
        public async Task<ApiResponseDTO<List<RoleDTO>>> GetAllRoles()
        {
            var retVal = new ApiResponseDTO<List<RoleDTO>>()
            {
                IsSuccess = true,
            };
            var RoleDTO = await _userService.GetAllRoleAsync();

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

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

            try
            {
                var result = await _userService.UpdateRoleAsync(roleDTO);
                retVal.Data = result;

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


        [HttpGet("GetUserRole/{id}")]
        public async Task<ApiResponseDTO<UserRoleDTO>> GetUserRole(int id)
        {
            var retVal = new ApiResponseDTO<UserRoleDTO>()
            {
                IsSuccess = true,
            };
            var RoleDTO = await _userService.GetUserRoleAsync(id);

            if (RoleDTO != null)
            {
                if (RoleDTO.Id == 0)
                {
                    retVal.IsSuccess = false;
                    retVal.Errors.Add("No match!");
                    retVal.Data = RoleDTO;
                }
                else
                {
                    retVal.IsSuccess = true;
                    retVal.Data = RoleDTO;
                }
            }
            else
            {
                retVal.IsSuccess = false;
                retVal.Errors.Add("No data!");
            }

            return retVal;
        }
        [HttpGet("GetAllUserRoles")]
        public async Task<ApiResponseDTO<List<UserRoleDTO>>> GetAllUserRoles()
        {
            var retVal = new ApiResponseDTO<List<UserRoleDTO>>()
            {
                IsSuccess = true,
            };
            var RoleDTO = await _userService.GetAllUserRoleAsync();

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

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

            try
            {
                var result = await _userService.UpdateUserRoleAsync(userRoleDTO);
                retVal.Data = result;

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


        [HttpGet("GetRoleCheckListTemplateHeader/{id}")]
        public async Task<ApiResponseDTO<RoleCheckListTemplateHeaderDTO>> GetRoleCheckListTemplateHeader(int id)
        {
            var retVal = new ApiResponseDTO<RoleCheckListTemplateHeaderDTO>()
            {
                IsSuccess = true,
            };
            var roleCheckListTemplateHeaderDTO = await _userService.GetRoleCheckListTemplateHeaderAsync(id);

            if (roleCheckListTemplateHeaderDTO != null)
            {
                if (roleCheckListTemplateHeaderDTO.Id == 0)
                {
                    retVal.IsSuccess = false;
                    retVal.Errors.Add("No match!");
                    retVal.Data = roleCheckListTemplateHeaderDTO;
                }
                else
                {
                    retVal.IsSuccess = true;
                    retVal.Data = roleCheckListTemplateHeaderDTO;
                }
            }
            else
            {
                retVal.IsSuccess = false;
                retVal.Errors.Add("No data!");
            }

            return retVal;
        }
        [HttpGet("GetAllRoleCheckListTemplateHeaders")]
        public async Task<ApiResponseDTO<List<RoleCheckListTemplateHeaderDTO>>> GetAllRoleCheckListTemplateHeaders()
        {
            var retVal = new ApiResponseDTO<List<RoleCheckListTemplateHeaderDTO>>()
            {
                IsSuccess = true,
            };
            var results = await _userService.GetAllRoleCheckListTemplateHeadersAsync();

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

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

            try
            {
                var result = await _userService.UpdateRoleCheckListTemplateHeaderAsync(roleCheckListTemplateHeaderDTO);
                retVal.Data = result;

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

        [HttpGet("GetRoleCheckPoint/{id}")]
        public async Task<ApiResponseDTO<RoleCheckPointDTO>> GetRoleCheckPoint(int id)
        {
            var retVal = new ApiResponseDTO<RoleCheckPointDTO>()
            {
                IsSuccess = true,
            };
            var roleCheckPointDTO = await _userService.GetRoleCheckPointAsync(id);

            if (roleCheckPointDTO != null)
            {
                if (roleCheckPointDTO.Id == 0)
                {
                    retVal.IsSuccess = false;
                    retVal.Errors.Add("No match!");
                    retVal.Data = roleCheckPointDTO;
                }
                else
                {
                    retVal.IsSuccess = true;
                    retVal.Data = roleCheckPointDTO;
                }
            }
            else
            {
                retVal.IsSuccess = false;
                retVal.Errors.Add("No data!");
            }

            return retVal;
        }
        [HttpGet("GetAllRoleCheckPoints")]
        public async Task<ApiResponseDTO<List<RoleCheckPointDTO>>> GetAllRoleCheckPoints()
        {
            var retVal = new ApiResponseDTO<List<RoleCheckPointDTO>>()
            {
                IsSuccess = true,
            };
            var results = await _userService.GetAllRoleCheckPointsAsync();

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

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

            try
            {
                var result = await _userService.UpdateRoleCheckPointAsync(roleCheckPointDTO);
                retVal.Data = result;

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