using Newtonsoft.Json;
using WK_KLI_LAPS_Service.App_Data;
using WK_KLI_LAPS_Service.Models.Master;

namespace WK_KLI_LAPS_Service.Controllers.Master.Vendor
{

    public interface IVendorService
    {
        object GetAll();
        object GetActiveVendor();
        // int Add(vendorModel data);
        string Edit(vendorModel data);
        int Delete(vendorModel data);
        string Add(vendorModel data);
    }
    public class VendorService : IVendorService
    {
        MasterRepository oMasterRepository = new MasterRepository();
        CommonRepository oCRepository = new CommonRepository();

        //public int Add(vendorModel data)
        //{
        //    int retval = 0;
        //    string returnmsg="";
        //    if (!oCRepository.CheckExists("spvendormasterselect", "checkexistence", data,ref returnmsg))
        //    {
        //       retval = oCRepository.ExecuteSP_ReturnInt("spvendormaster", data, "insert", "vendor");
        //    }
        //    return retval;
        //}

        public string Add(vendorModel data)
        {
            int retval = 0;
            string returnmsg = "";
            string message = "";
            if (!oCRepository.CheckExistsWithMessage("spvendormasterselect", "checkexistence", data, ref returnmsg))
            {
                retval = oCRepository.ExecuteSP_ReturnInt("spvendormaster", data, "insert", "vendor");

                if (retval > 0)
                {
                    message = "0";
                    returnmsg = JsonConvert.SerializeObject(new { message });
                }
            }
            return returnmsg;
        }

        public int Delete(vendorModel data)
        {
            int retval = oMasterRepository.Delete(data, "vendor");
            return retval;
        }

        public string Edit(vendorModel data)
        {
            int retval = 0;
            string returnmsg = "";
            string message = "";

            if (!oCRepository.CheckExistsWithMessage("spvendormasterselect", "checkexistence", data, ref returnmsg))
            {
                retval = oCRepository.ExecuteSP_ReturnInt("spvendormaster", data, "update", "vendor");

                if (retval > 0)
                {
                    message = "0";
                    returnmsg = JsonConvert.SerializeObject(new { message });
                }
            }
            return returnmsg;
        }

        public object GetActiveVendor()
        {
            return oMasterRepository.GetActive("vendor");
        }

        public object GetAll()
        {
            return oMasterRepository.GetAll("vendor");
        }
    }
}
