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

namespace WK_KLI_LAPS_Service.Controllers.Master.Error
{
    [EnableCors("CorsPolicy")]
    [Authorize]
    [Route("api/[controller]")]
    [ApiController]
    public class ErrorApiController : ControllerBase
    {
        CommonRepository oCommonRepository = new CommonRepository();
        private IErrorService _Errorservices;
        public ErrorApiController(IErrorService ErrorServices)
        {
            _Errorservices = ErrorServices;

        }

        [AllowAnonymous]
        [HttpGet("Getall")]
        public IActionResult Getall()
        {
            try
            {
                var list = _Errorservices.GetAll();
                var stagelst = _Errorservices.Getstagelst();

                return Ok(new
                {
                    errorlst=list,
                    stagelst=stagelst,
                });
            }
            catch (Exception ex)
            {
                oCommonRepository.ErrorLog(ex);
                // return error message if there was an exception
                return BadRequest(new { message = ex.Message });
            }
        }

        [Authorize]
        [HttpGet("GetActiveError")]
        //Code to retrieve active data
        public IActionResult GetActiveError()
        {
            var _list = _Errorservices.GetActiveError();
            return Ok(_list);
        }

        [AllowAnonymous]
        [HttpPost("Edit_Error")]
        public IActionResult Edit_Error(errorModel data)
        {
            try
            {
                var res = _Errorservices.Edit_Error(data);

                return Ok();
            }
            catch (Exception ex)
            {
                oCommonRepository.ErrorLog(ex);
                // return error message if there was an exception
                return BadRequest(new { message = ex.Message });
            }
        }

        [AllowAnonymous]
        [HttpPost("Active_Error")]
        public IActionResult Active_Error(errorModel data)
        {
            try
            {
                var res = _Errorservices.Active_Error(data);

                return Ok();
            }
            catch (Exception ex)
            {
                oCommonRepository.ErrorLog(ex);
                // return error message if there was an exception
                return BadRequest(new { message = ex.Message });
            }
        }

        [AllowAnonymous]
        [HttpPost("Add_Error")]
        public IActionResult Add_Error(errorModel data)
        {
            try
            {
                var res = _Errorservices.Add_Error(data);

                return Ok();
            }
            catch (Exception ex)
            {
                oCommonRepository.ErrorLog(ex);
                // return error message if there was an exception
                return BadRequest(new { message = ex.Message });
            }
        }
        /*[AllowAnonymous]
        [HttpGet("Get_Errorlst/{jobid}/{lotid}/{tranid}")]
        public IActionResult Get_Errorlst(long jobid,long lotid,long tranid)
        {
            try
            {
                var list = new {jobid=jobid,lotid=lotid, tranid=tranid};
                string input = JsonConvert.SerializeObject(list);
                var res = _Errorservices.Get_Errorlst(input);

                return Ok(res);
            }
            catch (Exception ex)
            {
                oCommonRepository.ErrorLog(ex);
                // return error message if there was an exception
                return BadRequest(new { message = ex.Message });
            }
        }*/
        [AllowAnonymous]
        [HttpGet("Get_Errorlst")]
        public IActionResult Get_Errorlst()
        {
            try
            {
                var res = _Errorservices.Get_Errorlst();

                return Ok(res);
            }
            catch (Exception ex)
            {
                oCommonRepository.ErrorLog(ex);
                // return error message if there was an exception
                return BadRequest(new { message = ex.Message });
            }
        }


    }

}

