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

namespace WK_KLI_LAPS_Service.Controllers.Configuration.UserManagement.ProjectMapping
{

    public interface IUserProjectMappingService
    {
        object GetAll();
        object GetActiveuUerProjectMapping();
        object Add(string inputstr, string mode);
        int Edit(userprojectmappingModel data);
        int Delete(userprojectmappingModel data);
        object GetById(int data, string mode);
        object UpdateUserProjectOnEmpty(string inputstr, string mode);
    }

    public class UserProjectMappingService : IUserProjectMappingService
    {
        MasterRepository oMasterRepository = new MasterRepository();
        CommonRepository oCommonRepository = new CommonRepository();

        //code to retrieve list of all records
        public object GetAll()
        {

            return oMasterRepository.GetAll("userprojectmapping");
        }

        //code to retrieve list of active records
        public object GetActiveuUerProjectMapping()
        {

            return oMasterRepository.GetActive("userprojectmapping");
        }

        public object Add(string inputstr, string mode)
        {
            string query = @"public.spuserprojectmapping";
            return oCommonRepository.ExecuteSP_ReturnList(query, inputstr, mode);
        }

        //code to update data
        public int Edit(userprojectmappingModel data)
        {
            if (data.userid < 0)
                throw new Exception("Value should not be empty");
            int retval = oMasterRepository.Edit(data, "userprojectmapping");
            return retval;
        }

        //code to activate or deactivate record
        public int Delete(userprojectmappingModel data)
        {
            int retval = oMasterRepository.Delete(data, "userprojectmapping");
            return retval;
        }

        //method to retrieve the workflow definition details based on workflow id
        public object GetById(int data, string mode)
        {
            //return oUserManagementRepository.GetById(data, mode);
            var userprojectmapping = new { project_definitionid = data };
            var jsonstr = JsonConvert.SerializeObject(userprojectmapping);
            string query = @"public.spuserprojectmappingselect";
            return oCommonRepository.ExecuteSP_ReturnList(query, jsonstr, mode);
        }

        public object UpdateUserProjectOnEmpty(string inputstr, string mode)
        {
            return oCommonRepository.ExecuteSP_ReturnList("spuserprojectmapping", inputstr, mode);
        }
    }
}
