using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using WK_KLI_LAPS_Service.App_Data;
using WK_KLI_LAPS_Service.Models;
using WK_KLI_LAPS_Service.Models.Configuration;
using WK_KLI_LAPS_Service.Models.Master;

namespace WK_KLI_LAPS_Service.Controllers.Master.LotBatch
{
    public interface ILotBatchService
    {
        object GetAll();
        object GetActiveLot();
        object Add(string inputstr, string mode);
        int Edit(lotBatchModel data);
        int Delete(lotBatchModel data);
        int PostBatch(object inputstr, string mode);
        object GetByIssupplement(Int32 issupplement);
        object GetCommentsByTranId(lotcomment data);
        object GetChapterDetailsbyJobid(Int32 jobid);
        object Getjobstatecount(Int32 jobid);
        object GetSupplement(Int32 jobid);
        object GetisProcessing(Int32 jobid);
        object GetisProcessing2(Int32 jobid);
        object finalsubmission(Int32 jobid);
        object Getsuppview(Int64 jobid, Int64 batchid);
        object GetSupplementDetailsBylotid(Int32 lotid);
        object UpdateIsLocked(lottraninfo lotTransactionData);

    }

    public class LotBatchService : ILotBatchService
    {
        MasterRepository oMasterRepository = new MasterRepository();
        CommonRepository oCommonRepository = new CommonRepository();
        //code to retrieve list of all records
        public object GetAll()
        {
            return oMasterRepository.GetAll("lot");
        }

        //code to retrieve list of active records
        public object GetActiveLot()
        {
            return oMasterRepository.GetActive("lot");
        }

        //Code to add data
        public object Add(string inputstr, string mode)
        {

            return oCommonRepository.ExecuteSP_ReturnList("splotmaster", inputstr, mode);
        }

        //code to update data
        public int Edit(lotBatchModel data)
        {
            if (string.IsNullOrEmpty(data.title))
                throw new Exception("Value should not be empty");
            int retval = oMasterRepository.Edit(data, "lot");
            return retval;
        }

        //code to activate or deactivate record
        public int Delete(lotBatchModel data)
        {
            int retval = oMasterRepository.Delete(data, "lot");
            return retval;
        }
        //Code to add data
        public int PostBatch(object inputstr, string mode)
        {
            return oCommonRepository.ExecuteSP_ReturnInt("splotbatch", inputstr, mode, "LotBatch");
        }
        public object GetByIssupplement(Int32 issupplement)
        {
            string input = JsonConvert.SerializeObject(issupplement);
            return oCommonRepository.ExecuteSP_ReturnList("splotbatchselect", input, "getallBySupplement");
        }

        public object GetCommentsByTranId(lotcomment data)
        {
            string input = JsonConvert.SerializeObject(data);
            return oCommonRepository.ExecuteSP_ReturnList("splotbatchselect", input, "getcommentsbytranid");
        }
        public object GetChapterDetailsbyJobid(Int32 jobid)
        {
            var data = new { jobid = jobid };
            string input = JsonConvert.SerializeObject(data);
            return oCommonRepository.ExecuteSP_ReturnList("splottransactionselect", input, "getchapdetailsbyjobid");
        }
        public object Getjobstatecount(Int32 jobid)
        {
            var data = new { jobid = jobid };
            string input = JsonConvert.SerializeObject(data);
            return oCommonRepository.ExecuteSP_ReturnList("splotbatchselect", input, "jobstatecount");
        }

        public object finalsubmission(Int32 jobid)
        {
            var data = new { jobid = jobid };
            string input = JsonConvert.SerializeObject(data);
            return oCommonRepository.ExecuteSP_ReturnList("splotbatchselect", input, "finalsubmission");
        }
        public object GetSupplement(Int32 jobid)
        {
            var data = new { jobid = jobid };
            string input = JsonConvert.SerializeObject(data);
            return oCommonRepository.ExecuteSP_ReturnList("splotbatchselect", input, "getallBySupplement");
        }
        public object GetisProcessing(Int32 jobid)
        {
            var data = new { jobid = jobid };
            string input = JsonConvert.SerializeObject(data);
            return oCommonRepository.ExecuteSP_ReturnList("splotbatchselect", input, "isProcessing");
        }
        public object GetisProcessing2(Int32 jobid)
        {
            var data = new { jobid = jobid };
            string input = JsonConvert.SerializeObject(data);
            return oCommonRepository.ExecuteSP_ReturnList("splotbatchselect", input, "isProcessing2");
        }
        public object Getsuppview(Int64 jobid, Int64 batchid)
        {
            var data = new { jobid = jobid, batchid = batchid };
            string input = JsonConvert.SerializeObject(data);
            return oCommonRepository.ExecuteSP_ReturnList("spgetsupplementdetails", input, "getsupplementdetails");
        }
        public object GetSupplementDetailsBylotid(Int32 lotid)
        {
            var data = new { lotid = lotid };
            string input = JsonConvert.SerializeObject(data);
            return CommonRepository.ExecuteSP_ReturnList2("splotbatchdetailsselect", input, "getsupplementdetails");
        }
        public object UpdateIsLocked(lottraninfo lotTransactionData)
        {
            //var data = new { transactionid = transactionid };
            var jsonstr = JsonConvert.SerializeObject(lotTransactionData);
            return CommonRepository.ExecuteSP_ReturnList2("splottransaction", jsonstr, "updateislocked");
        }
    }
}
