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.FileMode
{
    [EnableCors("CorsPolicy")]
    [Authorize]
    [ApiController]
    [Route("api/[controller]")]
    public class FileModeApiController : ControllerBase
    {
        CommonRepository oCommonRepository = new CommonRepository();

        private IFileModeService _FileModeservices;
        public FileModeApiController(IFileModeService FileModeServices)
        {
            _FileModeservices = FileModeServices;

        }


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

        [Authorize]
        [HttpGet("GetActiveFileMode")]
        //Code to retrieve active data
        public IActionResult GetActiveFileMode()
        {
            var _list = _FileModeservices.GetActiveFileMode();
            return Ok(_list);
        }
        //Code to add new record
        [Authorize]
        [HttpPost("Add")]
        public IActionResult Add([FromBody] filemodeModel ofilemodeModel)
        {

            int retval = 0;
            try
            {
                retval = _FileModeservices.Add(ofilemodeModel);
                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 update specific record
        [Authorize]
        [HttpPut("Update")]
        public IActionResult Update([FromBody] filemodeModel ofilemodeModel)
        {
            int retval = 0;
            try
            {
                retval = _FileModeservices.Edit(ofilemodeModel);
                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 activate or deactivate specific record
        [Authorize]
        [HttpPost("activeordeactive")]
        public IActionResult Delete([FromBody] filemodeModel ofilemodeModel)
        {
            int retval = 0;
            try
            {
                retval = _FileModeservices.Delete(ofilemodeModel);
                return Ok(retval);
            }
            catch (Exception ex)
            {
                oCommonRepository.ErrorLog(ex);
                // return error message if there was an exception
                return BadRequest(new { message = ex.Message });
            }
        }
    }
}