using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using System;
using WK_KLI_LAPS_Service.App_Data;
using WK_KLI_LAPS_Service.Models.Master;

namespace WK_KLI_LAPS_Service.Controllers.Master.Role
{
    [EnableCors("CorsPolicy")]
    [Authorize]
    [ApiController]
    [Route("api/[controller]")]
    public class RoleApiController : Controller
    {
        CommonRepository oCommonRepository = new CommonRepository();
        private IRoleService _roleService;
        public RoleApiController(IRoleService roleService)
        { _roleService = roleService; }

        [Authorize]
        [HttpGet]
        //Code to retrieve all data
        public IActionResult GetAll()
        {
            var _roleList = _roleService.GetAll();
            return Ok(_roleList);
        }

        [Authorize]
        [HttpGet("FindAllOrActiveMenus")]
        //Code to retrieve all data
        public IActionResult FindAllOrActiveMenus()
        {
            var _roleList = _roleService.FindAllOrActiveMenus();
            return Ok(_roleList);
        }

        //code to retrieve list of action
        [Authorize]
        [HttpGet("GetActionList")]
        public IActionResult GetActionList()
        {
            var _actionList = _roleService.getActionLst();
            return Ok(_actionList);
        }
        //Code to add new record
        [Authorize]
        [HttpPost("Add")]
        public IActionResult Add([FromBody] roleModel oroleModel)
        {

            int retval = 0;
            try
            {
                retval = _roleService.Add(oroleModel);
                return Ok(retval);
            }
            catch (Exception ex)
            {
                oCommonRepository.ErrorLog(ex);
                // return error message if there was an exception
                return BadRequest(new { message = ex.Message });
            }

        }

        //Code to add new record
        [Authorize]
        [HttpPost("Update")]
        public IActionResult Update([FromBody] roleModel oroleModel)
        {

            int retval = 0;
            try
            {
                retval = _roleService.Edit(oroleModel);
                return Ok(retval);
            }
            catch (Exception ex)
            {
                oCommonRepository.ErrorLog(ex);
                // return error message if there was an exception
                return BadRequest(new { message = ex.Message });
            }

        }

        [Authorize]
        [HttpPost("GetById")]
        //Code to retrieve all data
        public IActionResult GetById([FromBody] roleModel oroleModel)
        {
            var _roleList = _roleService.GetById(oroleModel);
            return Ok(_roleList);
        }

        //Code to activate or deactivate specific record
        [Authorize]
        [HttpPost("activeordeactive")]
        public IActionResult Delete([FromBody] roleModel oroleModel)
        {
            int retval = 0;
            try
            {
                retval = _roleService.Delete(oroleModel);
                return Ok(retval);
            }
            catch (Exception ex)
            {
                oCommonRepository.ErrorLog(ex);
                // return error message if there was an exception
                return BadRequest(new { message = ex.Message });
            }
        }
    }
}
