using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using WK_KLI_LAPS_Service.App_Data;
using WK_KLI_LAPS_Service.Models.Configuration;

namespace WK_KLI_LAPS_Service.Controllers.Configuration.Outline
{
    [Route("api/[controller]")]
    [ApiController]
    public class OutlineConfigurationApiController : ControllerBase
    {
        CommonRepository oCommonRepository = new CommonRepository();

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

        [Authorize]
        [HttpGet("getOutlineByoutlineconfigId/{outlineconfigId}")]
        public object getOutlineByoutlineconfigId(int outlineconfigId)
        {
            var _actionList = _IOutlineConfigurationService.getOutlineByoutlineconfigId(outlineconfigId);
            return Ok(_actionList);
        }

        [Authorize]
        [HttpGet("GetActiveOutline")]
        //Code to retrieve active data
        public IActionResult GetActiveOutline()
        {
            var _list = _IOutlineConfigurationService.GetActiveOutline();
            return Ok(_list);
        }
        //Code to add new record
        [Authorize]
        [HttpPost("Add")]
        public IActionResult Add([FromBody] List<outlineConfigurationModel> oOutlineConfigurationApiController)
        {
            try
            {
                string inputstr = JsonConvert.SerializeObject(oOutlineConfigurationApiController);
                var retval = _IOutlineConfigurationService.Add(inputstr, "insert");
                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] List<outlineConfigurationModel> oOutlineConfigurationApiController)
        {
            try
            {
                string inputstr = JsonConvert.SerializeObject(oOutlineConfigurationApiController);
                var retval = _IOutlineConfigurationService.Edit(inputstr, "update");
                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] outlineConfigurationModel oOutlineConfigurationApiController)
        {
            try
            {
                string inputstr = JsonConvert.SerializeObject(oOutlineConfigurationApiController);
                var retval = _IOutlineConfigurationService.Delete(inputstr, "activeordeactive");
                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("headermapping")]
        public IActionResult HeaderMapping([FromBody] outlineConfigurationModel oOutlineConfigurationApiController)
        {
            try
            {
                string inputstr = JsonConvert.SerializeObject(oOutlineConfigurationApiController);
                var retval = _IOutlineConfigurationService.HeaderMapping(inputstr, "headermapping");
                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("getOutlineDetailByoutlineconfigId/{outlineconfigId}")]
        public object getOutlineDetailByoutlineconfigId(int outlineconfigId)
        {
            var _actionList = _IOutlineConfigurationService.getOutlineDetailByoutlineconfigId(outlineconfigId);
            return Ok(_actionList);
        }
    }
}