using System;
using System.Collections.Generic;
using HRM.BO;
using HRM.DA;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace HRM.UI.Controllers.Adhoc_feature
{
    [Route("api/AdhocFeature")]
    [ApiController]
    [Authorize]
    public class AdhocFeatureController : ControllerBase
    {
        private readonly IADParameterEmployeeService _adParameterEmployeeService;
        private readonly IApproveFinantialDataService _approveFinancialDataService;

        public AdhocFeatureController(IADParameterEmployeeService adParameterEmployeeService,
            IApproveFinantialDataService approveFinancialDataService)
        {
            _adParameterEmployeeService = adParameterEmployeeService;
            _approveFinancialDataService = approveFinancialDataService;
        }
        [HttpGet("getAllFinancialData")]
        public ActionResult GetAllFinancialData()
        {
            List<ApproveFinantialData> items = new List<ApproveFinantialData>();
            try
            {
                items = _approveFinancialDataService.Get();
            }
            catch (Exception ex)
            {
                return StatusCode(StatusCodes.Status500InternalServerError, ex.Message);
            }

            return Ok(items);
        }

        [HttpPost("getAdParamEmpForApproval")]
        public ActionResult GetAdParamEmpForApproval(dynamic data)
        {
            CurrentUser currentUser = CurrentUser.GetCurrentUser(HttpContext.User);
            int payrollTypeId = currentUser.PayrollTypeID.GetValueOrDefault();
            var items = Newtonsoft.Json.JsonConvert.DeserializeObject(Convert.ToString(data));
            int itemId = (int)items["id"].ToObject<int>();
            DateTime fromDate = (DateTime)items["fromDate"].ToObject<DateTime>();
            DateTime toDate = (DateTime)items["toDate"].ToObject<DateTime>();
            List<ADParameterEmployee> adParameterEmployees = new List<ADParameterEmployee>();
            try
            {
                adParameterEmployees = _adParameterEmployeeService.GetForApproval(itemId, fromDate, toDate, payrollTypeId);
            }
            catch (Exception ex)
            {
                return StatusCode(StatusCodes.Status500InternalServerError, ex.Message);
            }

            return Ok(adParameterEmployees);
        }

		[HttpPost("getAllFinancialDataByType")]
		public ActionResult GetAllFinancialDataByType(dynamic data)
		{
			var items = Newtonsoft.Json.JsonConvert.DeserializeObject(Convert.ToString(data));
			int selectedDataType = (int)items["selectedDataType"].ToObject<int>();
			ApproveFinantialDataService service = new ApproveFinantialDataService();
			List<ApproveFinantialData> finantialData = new List<ApproveFinantialData>();
			try
			{
				finantialData = service.GetByType(selectedDataType);
			}
			catch (Exception ex)
			{
				return StatusCode(StatusCodes.Status500InternalServerError, ex.Message);
			}

			return Ok(finantialData);
		}

		[HttpGet("getByAllowForApproval/{allowDeductId}")]
        public ActionResult GetByAllowForApproval(int allowDeductId)
        {
            CurrentUser currentUser = CurrentUser.GetCurrentUser(HttpContext.User);
            int payrollId = currentUser.PayrollTypeID.GetValueOrDefault();
            List<ADParameterEmployee> items = new List<ADParameterEmployee>();
            try
            {
                items = _adParameterEmployeeService.GetByAllowForApproval(allowDeductId, payrollId);
            }
            catch (Exception ex)
            {
                return StatusCode(StatusCodes.Status500InternalServerError, ex.Message);
            }

            return Ok(items);
        }
        
        [HttpPost("saveFinancialDataList")]
        public ActionResult SaveFinancialDataList(List<ApproveFinantialData> items)
        {
            try
            {
               _approveFinancialDataService.Save(items);
            }
            catch (Exception ex)
            {
                return StatusCode(StatusCodes.Status500InternalServerError, ex.Message);
            }

            return Ok();
        }
        
        [HttpPost("deleteFinancialDataList")]
        public ActionResult DeleteFinancialDataList(List<ApproveFinantialData> items)
        {
            try
            {
                _approveFinancialDataService.Delete(items);
            }
            catch (Exception ex)
            {
                return StatusCode(StatusCodes.Status500InternalServerError, ex.Message);
            }

            return Ok();
        }


    }
}