using Microsoft.AspNetCore.Authorization;
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.Plugin
{
    [Route("api/[controller]")]
    [ApiController]
    public class PluginApiController : ControllerBase
    {
        CommonRepository oCommonRepository = new CommonRepository();

        private IPluginService _pluginService;
        public PluginApiController(IPluginService pluginService)
        {
            _pluginService = pluginService;
        }

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

        [Authorize]
        [HttpGet("GetActivePlugin")]
        //Code to retrieve active data
        public IActionResult GetActivePlugin()
        {
            var _list = _pluginService.GetActivePlugin();
            return Ok(_list);
        }
        //Code to add new record
        [Authorize]
        [HttpPost("Add")]
        public IActionResult Add([FromBody] pluginModel oPluginModel)
        {
            int retval = 0;
            try
            {
                retval = _pluginService.Add(oPluginModel);
                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]
        [HttpPost("Update")]
        public IActionResult Update([FromBody] pluginModel oPluginModel)
        {
            int retval = 0;
            try
            {
                retval = _pluginService.Edit(oPluginModel);
                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] pluginModel oPluginModel)
        {
            int retval = 0;
            try
            {
                retval = _pluginService.Delete(oPluginModel);
                return Ok(retval);
            }
            catch (Exception ex)
            {
                oCommonRepository.ErrorLog(ex);
                // return error message if there was an exception
                return BadRequest(new { message = ex.Message });
            }
        }
    }
}