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;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace WK_KLI_LAPS_Service.Controllers.Master.FileType
{
    [EnableCors("CorsPolicy")]
    [Authorize]
    [ApiController]
    [Route("api/[controller]")]
    public class FileTypeApiController : Controller
    {
        CommonRepository oCommonRepository = new CommonRepository();
        private IFileTypeService _fileTypeService;
        public FileTypeApiController(IFileTypeService fileTypeService)
        { _fileTypeService = fileTypeService; }

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

        [Authorize]
        [HttpGet("GetActiveFileType")]
        //Code to retrieve all data
        public IActionResult GetActiveFileType()
        {
            var _fileTypeList = _fileTypeService.GetActiveFileType();
            return Ok(_fileTypeList);
        }

        //Code to add new record
        [Authorize]
        [HttpPost("Add")]
        public IActionResult Add([FromBody] filetypeModel ofiletypeModel)
        {

            int retval = 0;
            try
            {
                retval = _fileTypeService.Add(ofiletypeModel);
                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] filetypeModel ofiletypeModel)
        {

            int retval = 0;
            try
            {
                retval = _fileTypeService.Edit(ofiletypeModel);
                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] filetypeModel ofiletypeModel)
        {
            var _fileTypeList = _fileTypeService.GetById(ofiletypeModel);
            return Ok(_fileTypeList);
        }

        //Code to activate or deactivate specific record
        [Authorize]
        [HttpPost("activeordeactive")]
        public IActionResult Delete([FromBody] filetypeModel ofiletypeModel)
        {
            int retval = 0;
            try
            {
                retval = _fileTypeService.Delete(ofiletypeModel);
                return Ok(retval);
            }
            catch (Exception ex)
            {
                oCommonRepository.ErrorLog(ex);
                // return error message if there was an exception
                return BadRequest(new { message = ex.Message });
            }
        }
        [Authorize]
        [HttpGet("GetToExport")]
        //Code to retrieve all data
        public IActionResult GetToExport()
        {
            var _fileTypeList = _fileTypeService.GetToExport();
            return Ok(_fileTypeList);
        }

        [Authorize]
        [HttpPost("dataToImport")]
        public IActionResult DataToImport([FromBody] filetypeModel[] ofiletypeModel)
        {
            int retval = 0;
            try
            {
                retval = _fileTypeService.dataToImport(ofiletypeModel);
                return Ok(retval);
            }
            catch (Exception ex)
            {
                oCommonRepository.ErrorLog(ex);
                // return error message if there was an exception
                return BadRequest(new { message = ex.Message });
            }
        }

    }
}
