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;
using WK_KLI_LAPS_Service.Models.Master;

namespace WK_KLI_LAPS_Service.Controllers.Configuration.Job
{
    public interface IJobConfigurationService
    {
        object GetAll();
        object GetProjectCode();
        object GetProductType(string projectid);
        object GetJobNo(string projectid, string projectdefinitionid);
        int Add(jobModel data);
        int Addjobandjobcofig(jobModel data);
        int Edit(jobModel data);
        object GetByJobId(jobModel data);
        object GetFlowChart(jobModel data);

        object AddJobWorkflow(List<jobworkflowdefinitionModel> data);
        object UpdateJobWorkflow(List<jobworkflowdefinitionModel> data);
        object GetAllByUserId(userModel data);
        object getAllByUserId_witharchive(userModel data);
        object GetJobWorkFlowDefCount(jobworkflowdefinitionModel data);
        object GetLotMastersByJobId(lotmasterModel data);
        object GetJobProductType(string userid);
        object GetTatFlowByProjectId(Int64 projectdefinitionid);
        object GetTatFlowByJobId(Int64 jobId);
        object getReadyYTS(jobworkflowdefinitionModel data);
        object getJobByProjectDefinitionid(Int64 projectdefinitionid);
        object GetByJobIdFromJobMaster(jobModel data);
        //ccwms
        int DeleteJob(Int64 jobid);
        object GetCCJobByUserId(Int64 userid, Int64 projecdefid);
        object PostCCJobMaster(Int64 userid, Int64 projecdefid);
    }
    public class JobConfigurationService : IJobConfigurationService
    {
        CommonRepository oCommonRepository = new CommonRepository();
        MasterRepository oMasterRepository = new MasterRepository();

        //code to retrieve list of all records
        public object GetAll()
        {
            //return oCommonRepository.ExecuteSP_ReturnList("spcustomercontactselect", "{}", "getall");
            return oMasterRepository.GetAll("job");
        }

        public object GetProjectCode()
        {

            return oCommonRepository.ExecuteSP_ReturnList("spjobmasterselect", "{}", "getProjectCode");
        }


        public object GetProductType(string projectid)
        {
            string jsonData = @"{'getid': " + projectid + "}";
            string details = JObject.Parse(jsonData).ToString();
            return oCommonRepository.ExecuteSP_ReturnList("spjobmasterselect", details, "getProductType");
        }
        public int DeleteJob(Int64 jobid)
        {
            delete data = new delete();
            data.jobid = jobid;
            string input = JsonConvert.SerializeObject(data);
            int retval = oMasterRepository.Master("spdelete", "job", input);
            return retval;
        }
        public object getAllByUserId_witharchive(userModel data)
        {
            string input = JsonConvert.SerializeObject(data);
            return oCommonRepository.ExecuteSP_ReturnList("spjobmasterselect", input, "getAllByUserId_witharchive");
        }
        class delete
        {
            public Int64 jobid { get; set; }
        }
        public object GetJobNo(string projectid, string projectdefinitionid)
        {
            string jsonData = @"{'projectid': " + projectid + ",'projectdefinitionid': " + projectdefinitionid + "}";
            string details = JObject.Parse(jsonData).ToString();
            return oCommonRepository.ExecuteSP_ReturnList("spjobmasterselect", details, "getJobNo");
        }


        //Code to add data
        public int Add(jobModel data)
        {
            if (string.IsNullOrEmpty(data.jobno))
                throw new Exception("Value should not be empty");
            //int retval = oCommonRepository.ExecuteSP_ReturnInt("spjobmasterselect", Newtonsoft.Json.JsonConvert.SerializeObject(data), "insert", "Inserting Customer contact");
            int retval = oMasterRepository.add(data, "job");
            return retval;

        }

        //Code to add job and jobcofiguration data
        public int Addjobandjobcofig(jobModel data)
        {
            int retval = oMasterRepository.addjobandjobcofig(data, "job");
            return retval;

        }

        //code to update data
        public int Edit(jobModel data)
        {
            if (string.IsNullOrEmpty(data.jobno))
                throw new Exception("Value should not be empty");
            //int retval = oCommonRepository.ExecuteSP_ReturnInt("spjobmasterselect", data, "update", "Updating Customer contact");
            int retval = oMasterRepository.Edit(data, "job");
            return retval;
        }

        ////code to activate or deactivate record
        //public int Delete(customercontactModel data)
        //{
        //    int retval = oCommonRepository.ExecuteSP_ReturnInt("spjobmasterselect", data, "activeordeactive", "Activating or deactivating Customer contact");
        //    return retval;
        //}
        //code to get job workflow definition details by job id

        public object GetByJobId(jobModel data)
        {
            string input = JsonConvert.SerializeObject(data);
            return oCommonRepository.ExecuteSP_ReturnList("spjobworkflowdefinitionselect", input, "getbyjobid");
        }

        //code to get flowchart for the given job id
        public object GetFlowChart(jobModel data)
        {
            string input = JsonConvert.SerializeObject(data);
            return oCommonRepository.ExecuteSP_ReturnList("spjobworkflowdefinitionselect", input, "getflowchart");
        }


        //code to add the project workflow definition 
        public object AddJobWorkflow(List<jobworkflowdefinitionModel> data)
        {
            string input = JsonConvert.SerializeObject(data);
            return oCommonRepository.ExecuteSP_ReturnList("spjobworkflowdefinition", input, "insert");
        }

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

        public object GetAllByUserId(userModel data)
        {
            string input = JsonConvert.SerializeObject(data);
            return oCommonRepository.ExecuteSP_ReturnList("spjobmasterselect", input, "getAllByUserId");
        }
        public object GetJobWorkFlowDefCount(jobworkflowdefinitionModel data)
        {
            string input = JsonConvert.SerializeObject(data);
            return oCommonRepository.ExecuteSP_ReturnList("spjobworkflowdefinitionselect", input, "getjobworkflowdefcount");
        }

        public object GetLotMastersByJobId(lotmasterModel data)
        {
            string input = JsonConvert.SerializeObject(data);
            return oCommonRepository.ExecuteSP_ReturnList("spjobworkflowdefinitionselect", input, "getjobworkflowdefcount");
        }
        public object GetJobProductType(string userid)
        {
            string jsonData = @"{'userid': " + userid + "}";
            string details = JObject.Parse(jsonData).ToString();
            //return oCommonRepository.ExecuteSP_ReturnList("spjobmasterselect", details, "getJobProductType");
            return oCommonRepository.ExecuteSP_ReturnList("spjobmasterselect", details, "getJobProductbyUser");
        }
        public object GetTatFlowByProjectId(Int64 projectdefinitionid)
        {
            string jsonData = @"{'projectdefinitionid': " + projectdefinitionid + "}";
            string details = JObject.Parse(jsonData).ToString();
            return oCommonRepository.ExecuteSP_ReturnList("spjobworkflowdefinitionselect", details, "getbyprojectdefinitionid");
        }

        public object GetTatFlowByJobId(Int64 jobId)
        {
            string jsonData = @"{'jobid': " + jobId + "}";
            string details = JObject.Parse(jsonData).ToString();
            return oCommonRepository.ExecuteSP_ReturnList("spjobworkflowdefinitionselect", details, "gettatbyjobid");
        }

        public object getReadyYTS(jobworkflowdefinitionModel data)
        {
            string input = JsonConvert.SerializeObject(data);
            return oCommonRepository.ExecuteSP_ReturnList("spjobworkflowdefinitionselect", input, "getreadyyts");
        }
        public object getJobByProjectDefinitionid(object data, string mode)
        {
            var input = JsonConvert.SerializeObject(data);
            return oCommonRepository.ExecuteSP_ReturnList("spjobmasterselect", input, mode);
        }

        public object getJobByProjectDefinitionid(Int64 projectdefinitionid)
        {
            string jsonData = @"{'projectdefinitionid': " + projectdefinitionid + "}";
            string details = JObject.Parse(jsonData).ToString();
            return oCommonRepository.ExecuteSP_ReturnList("spjobmasterselect", details, "getJobByProjectDefinitionid");
        }
        public object GetByJobIdFromJobMaster(jobModel data)
        {
            string input = JsonConvert.SerializeObject(data);
            return oCommonRepository.ExecuteSP_ReturnList("spjobmasterselect", input, "getbyjobid");
        }

        //ccwms
        public object GetCCJobByUserId(Int64 userid, Int64 projecdefid)
        {
            //string jsonData = @"{'p_userid': " + userid + ",'p_projectdefid': " + projecdefid + "}";
            //string details = JObject.Parse(jsonData).ToString();
            return oCommonRepository.CCExecuteSP_ReturnList("cc_sp_getuserjob", userid, projecdefid);
        }
        public object PostCCJobMaster(Int64 userid, Int64 projecdefid)
        {
            return oMasterRepository.PostCCJobMaster("cc_sp_createjob", userid, projecdefid);
        }
    }
}
