using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using HRM.BO;

namespace HRM.UI.Controllers.Payroll
{
    [Route("api/jv")]
    [ApiController]
    public class JvController : ControllerBase
    {
        private readonly IJVSetupService _jvSetupService;
        private readonly IJVSetupCCService _jvSetupCcService;
        private readonly IJVSetupDetailService _jvSetupDetailService;
        private readonly IJVTypeService _jvTypeService;

        public JvController(IJVSetupService jvSetupService,
            IJVSetupCCService jvSetupCcService,
            IJVSetupDetailService jvSetupDetailService,
            IJVTypeService jvTypeService)
        {
            this._jvSetupService = jvSetupService;
            this._jvSetupCcService = jvSetupCcService;
            this._jvSetupDetailService = jvSetupDetailService;
            this._jvTypeService = jvTypeService;
        }

        // JV Setup
        [HttpGet("getJVSetupById/{id}")]
        public ActionResult GetJVSetupById(int id)
        {
            JVSetup jvSetup = new JVSetup();
            try
            {
                jvSetup = _jvSetupService.Get(id);
            }
            catch (Exception e)
            {
                return StatusCode(StatusCodes.Status500InternalServerError, e.Message);
            }

            return Ok(jvSetup);
        }

        [HttpGet("getAllJVSetup")]
        public ActionResult GetAllJVSetup()
        {
            List<JVSetup> jvSetups = new List<JVSetup>();
            try
            {
                jvSetups = _jvSetupService.Get();
            }
            catch (Exception e)
            {
                return StatusCode(StatusCodes.Status500InternalServerError, e.Message);
            }

            return Ok(jvSetups);
        }

        [HttpGet("getJVSetupListByJVType/{jvSetupId}")]
        public ActionResult GetJVSetupListByJVType(int jvSetupId)
        {
            List<JVSetup> jvSetups = new List<JVSetup>();
            try
            {
                jvSetups = _jvSetupService.GetByJVType(jvSetupId);
            }
            catch (Exception e)
            {
                return StatusCode(StatusCodes.Status500InternalServerError, e.Message);
            }

            return Ok(jvSetups);
        }

        [HttpGet("GetLatestSetupByJVType/{jvtypeid}")]
        public ActionResult GetLatestSetupByJVType(int jvtypeid)
        {
            List<JVSetup> jvSetups = new List<JVSetup>();
            CurrentUser currentUser = CurrentUser.GetCurrentUser(HttpContext.User);

            try
            {
                jvSetups = _jvSetupService.GetLatestSetupByJVType((int)currentUser.PayrollTypeID, jvtypeid);
            }
            catch (Exception e)
            {
                return StatusCode(StatusCodes.Status500InternalServerError, e.Message);
            }

            return Ok(jvSetups);
        }


        [HttpGet("getJVSetupByJvSetupIdAndEffectDate/{jvSetupId}/{effectDate}")]
        public ActionResult GetJVSetupByJvSetupIdAndEffectDate(int jvSetupId, DateTime effectDate)
        {
            List<JVSetup> jvSetups = new List<JVSetup>();
            try
            {
                jvSetups = _jvSetupService.GetByJVTypeAndEffectDate(jvSetupId, effectDate);
            }
            catch (Exception e)
            {
                return StatusCode(StatusCodes.Status500InternalServerError, e.Message);
            }

            return Ok(jvSetups);
        }

        [HttpGet("getSequenceNo")]
        public ActionResult GetSequenceNo()
        {
            int ans;
            try
            {
                ans = _jvSetupService.GetSequenceNo();
            }
            catch (Exception e)
            {
                return StatusCode(StatusCodes.Status500InternalServerError, e.Message);
            }

            return Ok(ans);
        }

        [HttpGet("getJVSetupMaxDate/{givenDate}")]
        public ActionResult GetJVSetupMaxDate(DateTime givenDate)
        {
            DateTime date;
            try
            {
                date = _jvSetupService.GetMaxDate(givenDate);
            }
            catch (Exception e)
            {
                return StatusCode(StatusCodes.Status500InternalServerError, e.Message);
            }

            return Ok(date);
        }

        [HttpGet("getJVSetupListByMonthDate/{monthDate}")]
        public ActionResult GetJVSetupListByMonthDate(DateTime monthDate)
        {
            List<JVSetup> jvSetups = new List<JVSetup>();
            try
            {
                jvSetups = _jvSetupService.GetByMonthDate(monthDate);
            }
            catch (Exception e)
            {
                return StatusCode(StatusCodes.Status500InternalServerError, e.Message);
            }

            return Ok(jvSetups);
        }

        [HttpGet("getJVSetupChildByParentId/{jvSetupId}")]
        public ActionResult GetJVSetupChildByParentId(int jvSetupId)
        {
            List<JVSetupDetail> jvSetupDetails = new List<JVSetupDetail>();
            try
            {
                jvSetupDetails = _jvSetupService.GetChildByParentID(jvSetupId);
            }
            catch (Exception e)
            {
                return StatusCode(StatusCodes.Status500InternalServerError, e.Message);
            }

            return Ok(jvSetupDetails);
        }

        [HttpGet("getJVSetupByJVTypeAndMonth/{jvTypeId}/{lastDate}")]
        public ActionResult GetJVSetupByJVTypeAndMonth(int jvTypeId, DateTime lastDate)
        {
            List<JVSetup> jvSetups = new List<JVSetup>();
            try
            {
                jvSetups = _jvSetupService.GetByJVTypeAndMonth(jvTypeId, lastDate);
            }
            catch (Exception e)
            {
                return StatusCode(StatusCodes.Status500InternalServerError, e.Message);
            }

            return Ok(jvSetups);
        }

        [HttpPost]
        [Route("saveJVSetup")]
        public ActionResult SaveJVSetup(JVSetup ojvSetup)
        {
            CurrentUser currentUser = CurrentUser.GetCurrentUser(HttpContext.User);

            if (ojvSetup.IsNew == true)
            {
                ojvSetup.CreatedBy = currentUser.UserID;
                ojvSetup.CreatedDate = DateTime.Today;
                ojvSetup.PayrollTypeID = (int)currentUser.PayrollTypeID;

            }
            else
            {
                ojvSetup.ModifiedBy = currentUser.UserID;
                ojvSetup.ModifiedDate = DateTime.Today;

            }
            ojvSetup.MonthDate = (DateTime) currentUser.NextPayProcessDate;
            try
            {
                _jvSetupService.Save(ojvSetup);
            }
            catch (Exception e)
            {
                return StatusCode(StatusCodes.Status500InternalServerError, e.Message);
            }

            return Ok(ojvSetup.ID);
        }

        [HttpPost]
        [Route("updateSequenceJVSetup")]
        public ActionResult UpdateSequenceJVSetup(List<JVSetup> jvSetups)
        {
            try
            {
                _jvSetupService.UpdateSequence(jvSetups);
            }
            catch (Exception e)
            {
                return StatusCode(StatusCodes.Status500InternalServerError, e.Message);
            }

            return Ok();
        }

        [HttpPost]
        [Route("deleteJVSetup")]
        public ActionResult DeleteJVSetup(JVSetup item)
        {
            try
            {
                _jvSetupService.Delete(item.ID);
            }
            catch (Exception e)
            {
                return StatusCode(StatusCodes.Status500InternalServerError, e.Message);
            }

            return Ok();
        }

        // JVSetupCC
        [HttpGet("getJVSetupCCById/{id}")]
        public ActionResult GetJVSetupCCById(int id)
        {
            JVSetupCC jvSetupCC = new JVSetupCC();
            try
            {
                jvSetupCC = _jvSetupCcService.Get(id);
            }
            catch (Exception e)
            {
                return StatusCode(StatusCodes.Status500InternalServerError, e.Message);
            }

            return Ok(jvSetupCC);
        }

        [HttpGet("getAllJVSetupCC")]
        public ActionResult GetAllJVSetupCC(int id)
        {
            List<JVSetupCC> jvSetupCCs = new List<JVSetupCC>();
            try
            {
                jvSetupCCs = _jvSetupCcService.Get();
            }
            catch (Exception e)
            {
                return StatusCode(StatusCodes.Status500InternalServerError, e.Message);
            }

            return Ok(jvSetupCCs);
        }

        [HttpGet("getJVSetupCCByJVSetupId/{jvSetupId}")]
        public ActionResult GetJVSetupCCByJVSetupId(int jvSetupId)
        {
            List<JVSetupCC> jvSetupCCs = new List<JVSetupCC>();
            try
            {
                jvSetupCCs = _jvSetupCcService.GetByJVSetup(jvSetupId);
            }
            catch (Exception e)
            {
                return StatusCode(StatusCodes.Status500InternalServerError, e.Message);
            }

            return Ok(jvSetupCCs);
        }



        [HttpPost]
        [Route("saveJVSetupCC")]
        public ActionResult SaveJVSetupCC(JVSetupCC jvSetupCc)
        {
            try
            {
                _jvSetupCcService.Save(jvSetupCc);
            }
            catch (Exception e)
            {
                return StatusCode(StatusCodes.Status500InternalServerError, e.Message);
            }

            return Ok();
        }

        [HttpPost]
        [Route("deleteJVSetupCCById")]
        public ActionResult DeleteJVSetupCCById(int id)
        {
            try
            {
                _jvSetupCcService.Delete(id);
            }
            catch (Exception e)
            {
                return StatusCode(StatusCodes.Status500InternalServerError, e.Message);
            }

            return Ok();
        }

        // JVSetupDetail

        [HttpGet("getJVSetupDetailById/{id}")]
        public ActionResult GetJVSetupDetailById(int id)
        {
            JVSetupDetail jvSetupDetail = new JVSetupDetail();
            try
            {
                jvSetupDetail = _jvSetupDetailService.Get(id);
            }
            catch (Exception e)
            {
                return StatusCode(StatusCodes.Status500InternalServerError, e.Message);
            }

            return Ok(jvSetupDetail);
        }

        [HttpGet("getAllJVSetupDetail")]
        public ActionResult GetAllJVSetupDetail()
        {
            List<JVSetupDetail> jvSetupDetails = new List<JVSetupDetail>();
            try
            {
                jvSetupDetails = _jvSetupDetailService.Get();
            }
            catch (Exception e)
            {
                return StatusCode(StatusCodes.Status500InternalServerError, e.Message);
            }

            return Ok(jvSetupDetails);
        }

        [HttpGet("getJVSetupDetailByJVSetupId/{jvSetupId}")]
        public ActionResult GetJVSetupDetailByJVSetupId(int jvSetupId)
        {
            List<JVSetupDetail> jvSetupDetails = new List<JVSetupDetail>();
            try
            {
                jvSetupDetails = _jvSetupDetailService.GetByJVSetup(jvSetupId);
            }
            catch (Exception e)
            {
                return StatusCode(StatusCodes.Status500InternalServerError, e.Message);
            }

            return Ok(jvSetupDetails);
        }

        [HttpPost]
        [Route("saveJVSetupDetail")]
        public ActionResult SaveJVSetupDetail(JVSetupDetail jvSetupDetail)
        {
            try
            {
                _jvSetupDetailService.Save(jvSetupDetail);
            }
            catch (Exception e)
            {
                return StatusCode(StatusCodes.Status500InternalServerError, e.Message);
            }

            return Ok(jvSetupDetail);
        }

        [HttpPost]
        [Route("deleteJVSetupDetailById")]
        public ActionResult DeleteJVSetupDetailById(int id)
        {
            try
            {
                _jvSetupDetailService.Delete(id);
            }
            catch (Exception e)
            {
                return StatusCode(StatusCodes.Status500InternalServerError, e.Message);
            }

            return Ok();
        }

        // JVType

        [HttpGet("getJVTypeById/{id}")]
        public ActionResult GetJVTypeById(int id)
        {
            JVType jvType = new JVType();
            try
            {
                jvType = _jvTypeService.Get(id);
            }
            catch (Exception e)
            {
                return StatusCode(StatusCodes.Status500InternalServerError, e.Message);
            }

            return Ok(jvType);
        }

        [HttpGet("getAllJVType")]
        public ActionResult GetAllJVType()
        {
            List<JVType> jvTypes = new List<JVType>();
            try
            {
                jvTypes = _jvTypeService.Get();
            }
            catch (Exception e)
            {
                return StatusCode(StatusCodes.Status500InternalServerError, e.Message);
            }

            return Ok(jvTypes);
        }

        [HttpGet("getJVTypeByStatus/{status}")]
        public ActionResult GetJVTypeByStatus(EnumStatus status)
        {
            List<JVType> jvTypes = new List<JVType>();
            try
            {
                jvTypes = _jvTypeService.Get(status);
            }
            catch (Exception e)
            {
                return StatusCode(StatusCodes.Status500InternalServerError, e.Message);
            }

            return Ok(jvTypes);
        }

        [HttpPost]
        [Route("saveJVType")]
        public ActionResult SaveJVType(JVType jvType)
        {
            try
            {
                _jvTypeService.Save(jvType);
            }
            catch (Exception e)
            {
                return StatusCode(StatusCodes.Status500InternalServerError, e.Message);
            }

            return Ok(jvType);
        }

        [HttpPost]
        [Route("deleteJVTypeById")]
        public ActionResult DeleteJVTypeById(int id)
        {
            try
            {
                _jvTypeService.Delete(id);
            }
            catch (Exception e)
            {
                return StatusCode(StatusCodes.Status500InternalServerError, e.Message);
            }

            return Ok();
        }
    }
}