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

namespace WK_KLI_LAPS_Service.Controllers.Configuration.Outline.Outline
{
    [EnableCors("CorsPolicy")]
    [Authorize]
    [Route("api/[controller]")]
    [ApiController]
    public class OutlineApiController : ControllerBase
    {

        CommonRepository oCommonRepository = new CommonRepository();

        private IOutlineService _Outlineservices;
        public OutlineApiController(IOutlineService OutlineServices)
        {
            _Outlineservices = OutlineServices;

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

        [Authorize]
        [HttpPost("GetAllByUserid")]
        //Code to retrieve all data
        public IActionResult GetAllByUserid([FromBody] outlineModel ooutlineModel)
        {
            var _list = _Outlineservices.GetAllByUserid(ooutlineModel);
            return Ok(_list);
        }

        [Authorize]
        [HttpGet("GetActiveOutline")]
        //Code to retrieve active data
        public IActionResult GetActiveOutline()
        {
            var _list = _Outlineservices.GetActiveOutline();
            return Ok(_list);
        }
        //Code to add new record
        [Authorize]
        [HttpPost("Add")]
        public IActionResult Add([FromBody] outlineModel ooutlineModel)
        {

            int retval = 0;
            try
            {
                retval = _Outlineservices.Add(ooutlineModel);
                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] outlineModel ooutlineModel)
        {
            int retval = 0;
            try
            {
                retval = _Outlineservices.Edit(ooutlineModel);
                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("SaveAsOutline")]
        public IActionResult SaveAsOutline([FromBody] outlineModel ooutlineModel)
        {
            try
            {
                string inputstr = JsonConvert.SerializeObject(ooutlineModel);
                var retval = _Outlineservices.SaveAsOutline(inputstr);
                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] outlineModel ooutlineModel)
        {
            int retval = 0;
            try
            {
                retval = _Outlineservices.Delete(ooutlineModel);
                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] outlineModel ofiletypeModel)
        {
            var _fileTypeList = _Outlineservices.GetById(ofiletypeModel);
            return Ok(_fileTypeList);
        }

        [Authorize]
        [HttpPost("GetAllByIdForStatusCheck")]
        //Code to retrieve all data
        public IActionResult getAllByIdForStatusCheck([FromBody] outlineModel ofiletypeModel)
        {
            var _fileTypeList = _Outlineservices.getAllByIdForStatusCheck(ofiletypeModel);
            return Ok(_fileTypeList);
        }


    }
}