using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using WK_KLI_LAPS_COMMONTOOL_Service.App_Data;
using WK_KLI_LAPS_COMMONTOOL_Service.Models;

namespace WK_KLI_LAPS_COMMONTOOL_Service.Controllers.WorkArea
{
    public interface IWorkAreaService
    {
        object getCheckout(object data);
        object getJobDetails(object data);
        object getLotDetailsComments(object data);
        object checkLotTransactionStatus(string transactionId);
        object checkLotStatus(string transactionId);
        object insertLotComments(string comment, string filename, string transid, string doption, string tostage, string jobid, string lotid);
        object metaFieldsUpdate(string transid, string fields);
        object updateLotStatus(string transid, string lotstatus);
        object usrLotTransactionUpdate(string tranid, string userid, string status, string jobworkflowid, string comments, string mode);
        string getFileSaveLcation(int id);
        object CheckAppType(string tranid, string userid);
        object getNextStageDetails(string jobid);
        object getBatchStatus(string batchid);
        string GetServerPath(string id, bool createdir, string mode);
        object getFileTypeOutput(string transactionid);
        object getFileTypeInput(string transactionid);
        object getCurrentStage(string transactionid);
        object PushBack(object data);
        object workflowdecision(object data);
    }
    public class WorkAreaService: IWorkAreaService
    {
        CommonRepository oCommonRepository = new CommonRepository();
        public object PushBack(object data)
        {
            var input = JsonConvert.SerializeObject(data);
            return oCommonRepository.ExecuteSP_ReturnListwithoutmodeText("sppushback", input);
        }

        public object getCheckout(object data)
        {
            var input = JsonConvert.SerializeObject(data);
            return oCommonRepository.ExecuteSP_ReturnListwithoutmode("spusrlottranupdatev2", input);
        }
        public object usrstagemove(object data)
        {
            var input = JsonConvert.SerializeObject(data);
            return oCommonRepository.ExecuteSP_ReturnListwithoutmode("spusrstagemove", input);
        }
        public object getJobDetails(object data)
        {
            //var input = JsonConvert.SerializeObject(data);
            var input = JsonConvert.DeserializeObject<object>(data.ToString());
            var lotDetails = oCommonRepository.ExecuteSP_ReturnList("spgetlotandstagedetails", input.ToString(), "getlot");
            var stageDetails = oCommonRepository.ExecuteSP_ReturnList("spgetlotandstagedetails", input.ToString(), "getstage");
            var commentsDetails = oCommonRepository.ExecuteSP_ReturnList("spgetlotcommentslot", input.ToString(), "null");
            var json = new List<object>();
            json.Add(lotDetails);
            json.Add(stageDetails);
            json.Add(commentsDetails);
            return json;
        }
        public object getLotDetailsComments(object data)
        {
            var input = JsonConvert.SerializeObject(data);
            return oCommonRepository.ExecuteSP_ReturnList("spgetlotcommentslot", input, "null");
        }
        public object getLotDetailsOCR(long jobId)
        {
            // Wrap jobId into an object
            var data = new { jobid = jobId }; // lowercase 'jobid' matches SP

            // Serialize to JSON
            var inputJson = JsonConvert.SerializeObject(data);

            // Call stored procedure
            return oCommonRepository.ExecuteSP_ReturnList("spgettransactionid", inputJson, null);
        }


        public object checkLotTransactionStatus(string transactionId)
        {
            Fields oFieldsmodal = new Fields();
            oFieldsmodal.transid = Convert.ToUInt32(transactionId);
            var input = JsonConvert.SerializeObject(oFieldsmodal);
            return oCommonRepository.ExecuteSP_ReturnList1("sppostupdatelottransaction", input, "gettransactionstatus");
        }
        public object checkLotStatus(string transactionId)
        {
            Fields oFieldsmodal = new Fields();
            oFieldsmodal.transid = Convert.ToUInt32(transactionId);
            var input = JsonConvert.SerializeObject(oFieldsmodal);
            return oCommonRepository.ExecuteSP_ReturnList1("sppostupdatelottransaction", input, "getlotstatus");

        }
        public object insertLotComments(string comment, string filename, string transid, string doption, string tostage, string jobid, string lotid)
        {
            insertcomments oInsertComments = new insertcomments();
            oInsertComments.transid = Convert.ToUInt32(transid);
            oInsertComments.comments = comment;
            oInsertComments.filename = filename;
            oInsertComments.doption = doption;
            oInsertComments.jobid = Convert.ToUInt32(jobid);
            oInsertComments.lotid = Convert.ToUInt32(lotid);
            var input = JsonConvert.SerializeObject(oInsertComments);
            return oCommonRepository.ExecuteSP_ReturnList1("sppostupdatelottransaction", input, "insertlotcomments");

        }
        public object metaFieldsUpdate(string transid, string fields)
        {
            Fields oFieldsmodal = new Fields();
            oFieldsmodal.transid = Convert.ToUInt32(transid);
            oFieldsmodal.fields = fields;
            var input = JsonConvert.SerializeObject(oFieldsmodal);
            return oCommonRepository.ExecuteSP_ReturnList1("sppostupdatelottransaction", input, "metafieldsupdate");
        }
        public object updateLotStatus(string transid, string lotstatus)
        {
            insertcomments oInsertComments = new insertcomments();
            oInsertComments.transid = Convert.ToUInt32(transid);
            oInsertComments.lotstatus = lotstatus;
            var input = JsonConvert.SerializeObject(oInsertComments);
            return oCommonRepository.ExecuteSP_ReturnList1("sppostupdatelottransaction", input, "updatelotstatus");

        }
        public object usrLotTransactionUpdate(string tranid, string userid, string status, string jobworkflowid, string comments, string mode)
        {
            insertcomments oInsertComments = new insertcomments();
            oInsertComments.tranid = Convert.ToUInt32(tranid);
            oInsertComments.userid = Convert.ToUInt32(userid);
            oInsertComments.status = status;
            oInsertComments.jobworkflowid = Convert.ToUInt32(jobworkflowid);
            oInsertComments.comments = comments;
            var input = JsonConvert.SerializeObject(oInsertComments);
            return oCommonRepository.ExecuteSP_ReturnList1("spusrlottransactionupdate", input, mode);
        }
        public string getFileSaveLcation(int id)
        {
            string fileSaveLocation = oCommonRepository.getfilesavelocation(id);
            return fileSaveLocation;
        }
        public object CheckAppType(string tranid, string userid)
        {
            //appdetails obj = null;
            List<appdetails> appdetails1 = new List<appdetails>();
            insertcomments oInsertComments = new insertcomments();
            oInsertComments.tranid = Convert.ToUInt32(tranid);
            oInsertComments.userid = Convert.ToUInt32(userid);
            oInsertComments.status = "R";
            var input = JsonConvert.SerializeObject(oInsertComments);
            return oCommonRepository.ExecuteSP_ReturnList1("spusrlottransactionupdate", input, "apptype");
        }
        public object getNextStageDetails(string jobid)
        {
            JobWorkflow_ oFieldsmodal = new JobWorkflow_();
            oFieldsmodal.jobid = jobid;
            var input = JsonConvert.SerializeObject(oFieldsmodal);
            return oCommonRepository.ExecuteSP_ReturnList1("sppostupdatelottransaction", input, "getnextstagedetails");
        }
        public object getBatchStatus(string batchid)
        {
            Fields oFieldsmodal = new Fields();
            oFieldsmodal.batchid = Convert.ToInt32(batchid);
            var input = JsonConvert.SerializeObject(oFieldsmodal);
            return oCommonRepository.ExecuteSP_ReturnList1("sppostupdatelottransaction", input, "getbatchstatus");
        }

        public string GetServerPath(string id, bool createdir, string mode)
        {
            string fileSaveLocation = oCommonRepository.GetServerPath(id, mode, createdir);
            return fileSaveLocation;
        }
        public object getFileTypeOutput(string transactionid)
        {
            var data = new { transactionid = transactionid };
            var jsonstr = JsonConvert.SerializeObject(data);
            return oCommonRepository.ExecuteSP_ReturnList1("splottransactionselect", jsonstr, "getfiletypeoutput");
        }
        public object getFileTypeInput(string transactionid)
        {
            var data = new { transactionid = transactionid };
            var jsonstr = JsonConvert.SerializeObject(data);
            return oCommonRepository.ExecuteSP_ReturnList1("splottransactionselect", jsonstr, "getfiletypeinput");
        }
        public object getFilerenameInput(string transactionid)
        {
            var data = new { transactionid = transactionid };
            var jsonstr = JsonConvert.SerializeObject(data);
            return oCommonRepository.ExecuteSP_ReturnList1("spgetfilerename", jsonstr, null);
        }
        public object spfiletypevalidation(string transactionid,string jobid)
        {
            var data = new { transactionid = transactionid, jobid=Convert.ToInt32(jobid) };
            var jsonstr = JsonConvert.SerializeObject(data);
            return oCommonRepository.ExecuteSP_ReturnList1("spfiletypevalidation", jsonstr,null);
        }
        public object getCurrentStage(string transactionid)
        {
            var data = new { transactionid = transactionid };
            var jsonstr = JsonConvert.SerializeObject(data);
            return oCommonRepository.ExecuteSP_ReturnList1("splottransactionselect", jsonstr, "getcurrentstage");

        }

        public object workflowdecision(object data)
        {
            var input = data.ToString();
            return oCommonRepository.ExecuteSP_ReturnList1("spworkflowdecision", input, "insert");
        }

    }

}
