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.WorkFlow
{
    [Route("api/[controller]")]
    [ApiController]
    public class WorkFlowConfigurationApiController : ControllerBase
    {
        CommonRepository oCommonRepository = new CommonRepository();

        private IWorkFlowConfigurationService _WorkFlowConfigurationservices;
        public WorkFlowConfigurationApiController(IWorkFlowConfigurationService WorkFlowConfigurationServices)
        {
            _WorkFlowConfigurationservices = WorkFlowConfigurationServices;

        }

        //Code to add new record
        [AllowAnonymous]
        [HttpPost("Add")]
        public IActionResult Add([FromBody] workflowdefinitionModel oworkflowdefinitionModel)
        {
            //int retval = 0;
            try
            {
                var retval = _WorkFlowConfigurationservices.Add(oworkflowdefinitionModel);
                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
        [AllowAnonymous]
        [HttpPost("activeordeactive")]
        public IActionResult Delete([FromBody] workflowdefinitionModel oworkflowdefinitionModel)
        {
            try
            {
                var retval = _WorkFlowConfigurationservices.Delete(oworkflowdefinitionModel);
                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] workflowdefinitionModel oworkflowdefinitionModel)
        {
            var _mappedWorkFlowDefinition = _WorkFlowConfigurationservices.GetById(oworkflowdefinitionModel, "getbyid");
            return Ok(_mappedWorkFlowDefinition);
        }

        //code to update stage movement details
        [Authorize]
        [HttpPost("UpdateStageMovement")]
        public IActionResult UpdateStageMovement([FromBody] List<workflowdefinitionModel> data)
        {


            try
            {
                string inputstr = JsonConvert.SerializeObject(data);
                var _mappedWorkFlowDefinition = _WorkFlowConfigurationservices.Edit(inputstr, "updatestagemovement");
                return Ok(_mappedWorkFlowDefinition);
            }
            catch (Exception ex)
            {
                oCommonRepository.ErrorLog(ex);
                // return error message if there was an exception
                return BadRequest(new { message = ex.Message });
            }

        }
        //code to update stage movement details
        [Authorize]
        [HttpPost("UpdateStageOrder")]
        public IActionResult UpdateStageOrder([FromBody] List<workflowdefinitionModel> data)
        {


            try
            {
                string inputstr = JsonConvert.SerializeObject(data);
                var _mappedWorkFlowDefinition = _WorkFlowConfigurationservices.Edit(inputstr, "updatestageorder");
                return Ok(_mappedWorkFlowDefinition);
            }
            catch (Exception ex)
            {
                oCommonRepository.ErrorLog(ex);
                // return error message if there was an exception
                return BadRequest(new { message = ex.Message });
            }

        }

        [Authorize]
        [HttpPost("GetFlowChart")]
        //Code to retrieve all data
        public IActionResult GetFlowChart([FromBody] workflowdefinitionModel oworkflowdefinitionModel)
        {
            var _mappedFlowChart = _WorkFlowConfigurationservices.GetFlowChart(oworkflowdefinitionModel);
            return Ok(_mappedFlowChart);
        }

        //code to update stage movement details
        [Authorize]
        [HttpPost("PostStageConfig")]
        public IActionResult PostStageConfig([FromBody] workflowdefinitionModel data)
        {
            try
            {
                string inputstr = JsonConvert.SerializeObject(data);
                var _mappedWorkFlowDefinition = _WorkFlowConfigurationservices.Edit(inputstr, "updatestageconfig");
                return Ok(_mappedWorkFlowDefinition);
            }
            catch (Exception ex)
            {
                oCommonRepository.ErrorLog(ex);
                // return error message if there was an exception
                return BadRequest(new { message = ex.Message });
            }
        }

        [Authorize]
        [HttpPost("UpdateInOutFile")]
        public IActionResult UpdateInOutFile([FromBody] List<inoutfileModel> oinoutfileModel)
        {
            try
            {
                string inputstr = JsonConvert.SerializeObject(oinoutfileModel);
                var _mappedWorkFlowDefinition = _WorkFlowConfigurationservices.UpdateInOutFile(inputstr, "insert");
                return Ok(_mappedWorkFlowDefinition);
            }
            catch (Exception ex)
            {
                oCommonRepository.ErrorLog(ex);
                // return error message if there was an exception
                return BadRequest(new { message = ex.Message });
            }
        }

        [Authorize]
        [HttpPost("GetByWfdId")]
        //Code to retrieve all data
        public IActionResult GetByWfdId([FromBody] workflowdefinitionModel oworkflowdefinitionModel)
        {
            var _mappedWorkFlowDefinition = _WorkFlowConfigurationservices.GetById(oworkflowdefinitionModel, "getbywfdid");
            return Ok(_mappedWorkFlowDefinition);
        }

        [Authorize]
        [HttpPost("IOGetByWfdId")]
        //Code to retrieve all data
        public IActionResult IOGetByWfdId([FromBody] inoutfileModel oinoutfileModel)
        {
            var _mappedWorkFlowDefinition = _WorkFlowConfigurationservices.IOGetByWfdId(oinoutfileModel, "getbywfdid");
            return Ok(_mappedWorkFlowDefinition);
        }

        [Authorize]
        [HttpPost("UpdateInOutFileOnEmpty")]
        public IActionResult UpdateInOutFileOnEmpty([FromBody] inoutfileModel oinoutfileModel)
        {
            try
            {
                string inputstr = JsonConvert.SerializeObject(oinoutfileModel);
                var _mappedWorkFlowDefinition = _WorkFlowConfigurationservices.UpdateInOutFile(inputstr, "updateonempty");
                return Ok(_mappedWorkFlowDefinition);
            }
            catch (Exception ex)
            {
                oCommonRepository.ErrorLog(ex);
                // return error message if there was an exception
                return BadRequest(new { message = ex.Message });
            }
        }

        //code to update stage movement details
        [Authorize]
        [HttpPost("UpdateStageMetaFields")]
        public IActionResult UpdateStageMetaFields([FromBody] workflowdefinitionModel data)
        {
            try
            {
                string inputstr = JsonConvert.SerializeObject(data);
                var _mappedWorkFlowDefinition = _WorkFlowConfigurationservices.Edit(inputstr, "updatestagemetafields");
                return Ok(_mappedWorkFlowDefinition);
            }
            catch (Exception ex)
            {
                oCommonRepository.ErrorLog(ex);
                // return error message if there was an exception
                return BadRequest(new { message = ex.Message });
            }

        }


    }
}