using Microsoft.Extensions.Configuration;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
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.Project
{
    public interface IProjectConfigurationService
    {
        object GetAll();
        int Add(projectdefinitionModel data);
        object GetByUserId(object data, string mode);
        object GetActiveProjectDefinition();
        int Edit(projectdefinitionModel data);
        int Delete(projectdefinitionModel data);
        object GetProjectDefWorkflow();
        object GetProjDefStageFollowup(object data);
        object GetFlowChart(projectdefinitionModel data);
        object GetByIdWithMasterValue(projectdefinitionModel data);
        object GetByIdProjectDefWorkflow(projectdefinitionModel data);
        object GetCurrentFlowProject(projectdefinitionModel data);
        object GetCurrentFlowWorkflow(workflowdefinitionModel data);
        object GetStageFollowup();
        object GetProjectFollowup();
        object GetStageFollowupActive();
        object GetActiveProjectFollowup();
        int DeleteStageFollowup(stagefollowupModel data);
        int AddStageFollowup(stagefollowupModel data);
        int EditStageFollowup(stagefollowupModel data);
        int DeleteProjectFollowup(projectfollowupModel data);
        int AddProjectFollowup(projectfollowupModel data);
        int EditProjectFollowup(projectfollowupModel data);

        object AddProjectWorkflow(List<projectworkflowdefinitionModel> data);
        object UpdateProjectWorkflow(List<projectworkflowdefinitionModel> data);
        object GetTAT();
        object getAllWOWFByUserId(object data, string mode);
        object GetByIdProjectDefWorkflowwithOutline(projectdefinitionModel data);
        object GetAllForSupplement(object data, string mode);
    }

    public class ProjectConfigurationService : IProjectConfigurationService
    {
        CommonRepository oCommonRepository = new CommonRepository();
        //Code to add new  project definition details
        public int Add(projectdefinitionModel data)
        {
            if (string.IsNullOrEmpty(data.project_subtitle))
                throw new Exception("Value should not be empty");
            //string input = JsonConvert.SerializeObject(data);
            bool exists = false;
            exists = oCommonRepository.CheckExists("spprojectdefinitionselect", "checkexistence", data);
            int result = 0;
            if (exists == false)
            {
                result = oCommonRepository.ExecuteSP_ReturnInt("spprojectdefinition", data, "insert", "Add new project definiition");
            }
            return result;
        }

        // Get the projection definition details based on user mapped
        public object GetByUserId(object data, string mode)
        {
            // var user = new { userid = data };
            var input = JsonConvert.SerializeObject(data);
            //return oProjectConfigurationRepository.GetByUserId(input, mode);
            return oCommonRepository.ExecuteSP_ReturnList("spprojectdefinitionselect", input, mode);
        }

        public object GetAll()
        {
            return oCommonRepository.ExecuteSP_ReturnList("spprojectdefinitionselect", "{}", "getAll_Projectdefinition");
        }
        public object GetActiveProjectDefinition()
        {
            return oCommonRepository.ExecuteSP_ReturnList("spprojectdefinitionselect", null, "getall");
        }

        public int Edit(projectdefinitionModel data)
        {
            if (string.IsNullOrEmpty(data.project_subtitle))
                throw new Exception("Value should not be empty");
            //string input = JsonConvert.SerializeObject(data);
            bool exists = false;
            exists = oCommonRepository.CheckExists("spprojectdefinitionselect", "checkexistence", data);
            int result = 0;
            if (exists == false)
            {
                result = oCommonRepository.ExecuteSP_ReturnInt("spprojectdefinition", data, "update", "edit project definiition");
            }
            return result;
        }
        //code to activate or deactivate record
        public int Delete(projectdefinitionModel data)
        {
            int retval = oCommonRepository.ExecuteSP_ReturnInt("spprojectdefinition", data, "activeordeactive", "activeordeactive Project definition");
            return retval;
        }

        public object GetProjectDefWorkflow()
        {
            return oCommonRepository.ExecuteSP_ReturnList("spprojectworkflowdefinitionselect", "{}", "ProjectDefWorkflow");
        }

        public object GetProjDefStageFollowup(object data)
        {

            string input = JsonConvert.SerializeObject(data);
            dynamic jobj = JObject.Parse(input);
            string mode = jobj.mode.Value;
            return oCommonRepository.ExecuteSP_ReturnList("spprojectworkflowdefinitionselect", input, mode);
        }
        //code to get flowchart for the given workflow definition
        public object GetFlowChart(projectdefinitionModel data)
        {
            string input = JsonConvert.SerializeObject(data);
            return oCommonRepository.ExecuteSP_ReturnList("spprojectworkflowdefinitionselect", input, "getflowchart");
        }
        //Code to get specific project definition record with master values
        public object GetByIdWithMasterValue(projectdefinitionModel data)
        {
            string input = JsonConvert.SerializeObject(data);
            return oCommonRepository.ExecuteSP_ReturnList("spprojectdefinitionselect", input, "getbyidwithmastervalue");
        }
        //code to get projectworkflowdefinition by projectdefinitionid
        public object GetByIdProjectDefWorkflow(projectdefinitionModel data)
        {
            string input = JsonConvert.SerializeObject(data);
            return oCommonRepository.ExecuteSP_ReturnList("spprojectworkflowdefinitionselect", input, "getbyid");
        }
        //code to get current flow by projectdefinitionid
        public object GetCurrentFlowProject(projectdefinitionModel data)
        {
            string input = JsonConvert.SerializeObject(data);
            return oCommonRepository.ExecuteSP_ReturnList("spprojectworkflowdefinitionselect", input, "getcurrentflowproject");
        }
        //code to get current flow by workflowid
        public object GetCurrentFlowWorkflow(workflowdefinitionModel data)
        {
            string input = JsonConvert.SerializeObject(data);
            return oCommonRepository.ExecuteSP_ReturnList("spprojectworkflowdefinitionselect", input, "getcurrentflowworkflow");
        }

        public int AddStageFollowup(stagefollowupModel data)
        {
            if (string.IsNullOrEmpty(data.remaindertype))
                throw new Exception("Value should not be empty");
            string input = JsonConvert.SerializeObject(data);

            bool exists = false;
            exists = oCommonRepository.CheckExists("spprojectdefinitionselect", "checkexistence", data);
            int result = 0;
            if (exists == false)
            {
                result = oCommonRepository.ExecuteSP_ReturnInt("spstagefollowup", data, "insert", "Add new stage followup");
            }



            return result;
        }

        public int EditStageFollowup(stagefollowupModel data)
        {
            if (string.IsNullOrEmpty(data.remaindertype))
                throw new Exception("Value should not be empty");
            //string input = JsonConvert.SerializeObject(data);
            bool exists = false;
            exists = oCommonRepository.CheckExists("spprojectdefinitionselect", "checkexistence", data);
            int result = 0;
            if (exists == false)
            {
                result = oCommonRepository.ExecuteSP_ReturnInt("spstagefollowup", data, "update", "edit new stage followup");
            }
            return result;
        } 
        public int AddProjectFollowup(projectfollowupModel data)
        {
            if (string.IsNullOrEmpty(data.remaindertype))
                throw new Exception("Value should not be empty");
            string input = JsonConvert.SerializeObject(data);

            bool exists = false;
            exists = oCommonRepository.CheckExists("spprojectfollowupselect", "checkexistence", data);
            int result = 0;
            if (exists == false)
            {
                result = oCommonRepository.ExecuteSP_ReturnInt("spprojectfollowup", data, "insert", "Add new stage followup");
            }



            return result;
        }

        public int EditProjectFollowup(projectfollowupModel data)
        {
            if (string.IsNullOrEmpty(data.remaindertype))
                throw new Exception("Value should not be empty");
            //string input = JsonConvert.SerializeObject(data);
            bool exists = false;
            exists = oCommonRepository.CheckExists("spprojectfollowupselect", "checkexistence", data);
            int result = 0;
            if (exists == false)
            {
                result = oCommonRepository.ExecuteSP_ReturnInt("spprojectfollowup", data, "update", "edit new stage followup");
            }
            return result;
        }

        public object GetStageFollowup()
        {
            return oCommonRepository.ExecuteSP_ReturnList("spstagefollowupselect", "{}", "getall");
        } public object GetProjectFollowup()
        {
            return oCommonRepository.ExecuteSP_ReturnList("spprojectfollowupselect", "{}", "getall");
        }

        public object GetStageFollowupActive()
        {
            return oCommonRepository.ExecuteSP_ReturnList("spstagefollowupselect", "{}", "getactive");
        }  
        public object GetActiveProjectFollowup()
        {
            return oCommonRepository.ExecuteSP_ReturnList("spprojectfollowupselect", "{}", "getactive");
        }

        public int DeleteStageFollowup(stagefollowupModel data)
        {
            int retval = oCommonRepository.ExecuteSP_ReturnInt("spstagefollowup", data, "activeordeactive", "activeordeactive stage followup");
            return retval;
        } 
        public int DeleteProjectFollowup(projectfollowupModel data)
        {
            int retval = oCommonRepository.ExecuteSP_ReturnInt("spprojectfollowup", data, "activeordeactive", "activeordeactive project followup");
            return retval;
        }
        //code to add the project workflow definition 
        public object AddProjectWorkflow(List<projectworkflowdefinitionModel> data)
        {
            string input = JsonConvert.SerializeObject(data);
            return oCommonRepository.ExecuteSP_ReturnList("spprojectworkflowdefinition", input, "insert");
        }

        //code to update the project workflow definition 
        public object UpdateProjectWorkflow(List<projectworkflowdefinitionModel> data)
        {
            string input = JsonConvert.SerializeObject(data);
            return oCommonRepository.ExecuteSP_ReturnList("spprojectworkflowdefinition", input, "update");
        }

        public object GetTAT()
        {
            string defaulttathour = Startup.Configuration.GetValue<string>("AppSettings:defaulttathour");
            string defaulttattype = Startup.Configuration.GetValue<string>("AppSettings:defaulttattype");
            var tatdetails = new
            {
                tathours = defaulttathour,
                tattype = defaulttattype
            };
            var json = System.Text.Json.JsonSerializer.Serialize(tatdetails);
            return json;
        }
        // Get the projection definition details based on user mapped
        public object getAllWOWFByUserId(object data, string mode)
        {
            // var user = new { userid = data };
            var input = JsonConvert.SerializeObject(data);
            //return oProjectConfigurationRepository.GetByUserId(input, mode);
            return oCommonRepository.ExecuteSP_ReturnList("spprojectdefinitionselect", input, mode);
        }

        //code to get GetByIdProjectDefWorkflowwithOutline by projectdefinitionid
        public object GetByIdProjectDefWorkflowwithOutline(projectdefinitionModel data)
        {
            string input = JsonConvert.SerializeObject(data);
            return oCommonRepository.ExecuteSP_ReturnList("spprojectworkflowdefinitionselect", input, "getbyidwithoutlineconfig");
        }
        // Get the projection definition details based on user mapped
        public object GetAllForSupplement(object data, string mode)
        {
            // var user = new { userid = data };
            var input = JsonConvert.SerializeObject(data);
            //return oProjectConfigurationRepository.GetByUserId(input, mode);
            return oCommonRepository.ExecuteSP_ReturnList("spprojectdefinitionselect", input, mode);
        }
    }
}
