using Dapper;
using Newtonsoft.Json;
using System;
using System.Data;
using System.Linq;
using System.Xml.Linq;
using WK_KLI_LAPS_Service.Models.Master;

namespace WK_KLI_LAPS_Service.App_Data
{
    public class MasterRepository
    {
        useractivitylogModel userlogmodel;
        CommonRepository oCommonRepository = new CommonRepository();
        /// Get the list of all master data present in the db
        public object GetAll(string mastername)
        {
            using (IDbConnection dbConnection = ConnectionBase.Connection)
            {
                dbConnection.Open();
                var param = new DynamicParameters();
                string query = @"public.sp" + mastername + "masterselect";
                param.Add("inputjsonstr", null);
                param.Add("mode_p", dbType: DbType.String, value: "getall", direction: ParameterDirection.Input);
                return dbConnection.Query<string>(query, param, commandType: CommandType.StoredProcedure);
            }
        }
        /// Get the list of all master data present in the db
        public object GetActive(string mastername)
        {
            using (IDbConnection dbConnection = ConnectionBase.Connection)
            {
                dbConnection.Open();
                var param = new DynamicParameters();
                string query = @"public.sp" + mastername + "masterselect";
                param.Add("inputjsonstr", null);
                param.Add("mode_p", dbType: DbType.String, value: "getactive", direction: ParameterDirection.Input);
                return dbConnection.Query<string>(query, param, commandType: CommandType.StoredProcedure);

            }
        }
        public object GetAllWithExclude(string mastername)
        {
            using (IDbConnection dbConnection = ConnectionBase.Connection)
            {
                dbConnection.Open();
                var param = new DynamicParameters();
                string query = @"public.sp" + mastername + "masterselect";
                param.Add("inputjsonstr", null);
                param.Add("mode_p", dbType: DbType.String, value: "getallwithexclude", direction: ParameterDirection.Input);
                return dbConnection.Query<string>(query, param, commandType: CommandType.StoredProcedure);
            }
        }

        /// Get the list of all master data present in the db
        public object GetById(string mastername, string inputjsonval)
        {
            using (IDbConnection dbConnection = ConnectionBase.Connection)
            {
                dbConnection.Open();
                var param = new DynamicParameters();
                string query = @"public.sp" + mastername + "masterselect";
                param.Add("inputjsonstr", inputjsonval);
                param.Add("mode_p", dbType: DbType.String, value: "getbyid", direction: ParameterDirection.Input);
                return dbConnection.Query<string>(query, param, commandType: CommandType.StoredProcedure);

            }
        }
        /// To insert the newly added role information
        public int add(object data, string mastername)
        {
            using (IDbConnection dbConnection = ConnectionBase.Connection)
            {
                dbConnection.Open();
                string input = JsonConvert.SerializeObject(data);
                bool _exists = CheckExists(mastername, input);
                int result = 0;
                if (_exists == false)
                {

                    string query = @"public.sp" + mastername + "master";
                    var param = new DynamicParameters();
                    param.Add("inputjsonstr", dbType: DbType.String, value: input, direction: ParameterDirection.Input);
                    param.Add("mode_p", dbType: DbType.String, value: "insert", direction: ParameterDirection.Input);
                    result = dbConnection.Query<int>(query, param, commandType: CommandType.StoredProcedure).First();

                }
                //code to add user activity log
                if (result != 0)
                {
                    userlogmodel = new useractivitylogModel();
                    userlogmodel.useraction = "Add " + mastername;
                    userlogmodel.description = "Added " + mastername;
                    oCommonRepository.useractivityAdd(userlogmodel);
                }
                return result;
            }
        }
        public int addjobandjobcofig(object data, string mastername)
        {
            using (IDbConnection dbConnection = ConnectionBase.Connection)
            {
                dbConnection.Open();
                string input = JsonConvert.SerializeObject(data);
                //bool _exists = CheckExists(mastername, input);
                int result = 0;
                //if (_exists == false)
                //{

                    string query = @"public.sp" + mastername + "master";
                    var param = new DynamicParameters();
                    param.Add("inputjsonstr", dbType: DbType.String, value: input, direction: ParameterDirection.Input);
                    param.Add("mode_p", dbType: DbType.String, value: "insertjobandjobconfig", direction: ParameterDirection.Input);
                    result = dbConnection.Query<int>(query, param, commandType: CommandType.StoredProcedure).First();

                //}
                //code to add user activity log
                if (result != 0)
                {
                    userlogmodel = new useractivitylogModel();
                    userlogmodel.useraction = "Add " + mastername;
                    userlogmodel.description = "Added " + mastername;
                    oCommonRepository.useractivityAdd(userlogmodel);
                }
                return result;
            }
        }

        //code to edit specific data
        public int Edit(object data, string mastername)
        {

            using (IDbConnection dbConnection = ConnectionBase.Connection)
            {
                dbConnection.Open();
                string input = JsonConvert.SerializeObject(data);
                bool _exists = CheckExists(mastername, input);
                int result = 0;
                if (_exists == false)
                {

                    string query = @"public.sp" + mastername + "master";
                    var param = new DynamicParameters();
                    param.Add("inputjsonstr", dbType: DbType.String, value: input, direction: ParameterDirection.Input);
                    param.Add("mode_p", dbType: DbType.String, value: "update", direction: ParameterDirection.Input);
                    result = dbConnection.Query<int>(query, param, commandType: CommandType.StoredProcedure).First();
                }
                //code to add user activity log
                if (result != 0)
                {
                    userlogmodel = new useractivitylogModel();
                    userlogmodel.useraction = "Edit " + mastername;
                    userlogmodel.description = "Edited " + mastername + ".Edited Id is " + result.ToString();
                    oCommonRepository.useractivityAdd(userlogmodel);
                }
                return result;


            }
        }
        //To activate or deactivate the selected record
        public int Delete(object data, string mastername)
        {
            using (IDbConnection dbConnection = ConnectionBase.Connection)
            {

                dbConnection.Open();
                int result = 0;
                string input = JsonConvert.SerializeObject(data);
                string query = @"public.sp" + mastername + "master";
                var param = new DynamicParameters();
                param.Add("inputjsonstr", dbType: DbType.String, value: input, direction: ParameterDirection.Input);
                param.Add("mode_p", dbType: DbType.String, value: "activeordeactive", direction: ParameterDirection.Input);
                result = dbConnection.Query<int>(query, param, commandType: CommandType.StoredProcedure).First();

                if (result != 0)
                {
                    userlogmodel = new useractivitylogModel();
                    userlogmodel.useraction = "Delete " + mastername;
                    userlogmodel.description = "Deleted " + mastername + ".Deleted Id is " + result.ToString();
                    oCommonRepository.useractivityAdd(userlogmodel);
                }
                return result;
            }
        }
        public int Master(string spname, string mode_p, string inputval)
        {
            try
            {
                using (IDbConnection dbConnection = ConnectionBase.Connection)
                {
                    dbConnection.Open();
                    string query = spname;
                    var param = new DynamicParameters();
                    param.Add("mode_p", dbType: DbType.String, value: mode_p, direction: ParameterDirection.Input);
                    if (inputval != "")
                    {
                        param.Add("inputjsonstr", dbType: DbType.String, value: inputval, direction: ParameterDirection.Input);
                    }
                    return dbConnection.Query<int>(query, param, commandType: CommandType.StoredProcedure).First();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        //Check the given name is exists or not
        public bool CheckExists(string mastername, string inputjsonval, bool ismaster = true)
        {
            bool result = false;
            using (IDbConnection dbConnection = ConnectionBase.Connection)
            {
                string query = "";
                dbConnection.Open();
                var param = new DynamicParameters();
                if (ismaster)
                {
                    query = @"public.sp" + mastername + "masterselect";
                }
                else
                {
                    query = @"public.sp" + mastername + "select";
                }
                param.Add("inputjsonstr", inputjsonval);
                param.Add("mode_p", dbType: DbType.String, value: "checkexistence", direction: ParameterDirection.Input);
                var _list = dbConnection.Query<string>(query, param, commandType: CommandType.StoredProcedure).ToList();
                if (_list.Count() > 0 && _list.ToList()[0] != null)
                    result = true;
                return result;
            }
        }
        public string GetScalarValue(string mastername, string inputjsonval)
        {
            using (IDbConnection dbConnection = ConnectionBase.Connection)
            {
                dbConnection.Open();
                var param = new DynamicParameters();
                string query = @"public.sp" + mastername + "masterselect";
                param.Add("inputjsonstr", inputjsonval);
                param.Add("mode_p", dbType: DbType.String, value: "getbyid", direction: ParameterDirection.Input);
                return dbConnection.ExecuteScalar<string>(query, param, commandType: CommandType.StoredProcedure);
            }
        }
        public object GetToExport(string mastername)
        {
            using (IDbConnection dbConnection = ConnectionBase.Connection)
            {
                dbConnection.Open();
                var param = new DynamicParameters();
                string query = @"public.sp" + mastername + "masterselect";
                param.Add("inputjsonstr", null);
                param.Add("mode_p", dbType: DbType.String, value: "exporttoexcel", direction: ParameterDirection.Input);
                return dbConnection.Query<string>(query, param, commandType: CommandType.StoredProcedure);

            }
        }
        public int DataToImport(object data, string mastername)
        {
            using (IDbConnection dbConnection = ConnectionBase.Connection)
            {

                dbConnection.Open();
                int result = 0;
                string input = JsonConvert.SerializeObject(data);
                string query = @"public.sp" + mastername + "master";
                var param = new DynamicParameters();
                param.Add("inputjsonstr", dbType: DbType.String, value: input, direction: ParameterDirection.Input);
                param.Add("mode_p", dbType: DbType.String, value: "datatoimport", direction: ParameterDirection.Input);
                result = dbConnection.Query<int>(query, param, commandType: CommandType.StoredProcedure).First();

                if (result != 0)
                {
                    userlogmodel = new useractivitylogModel();
                    userlogmodel.useraction = "datatoimport " + mastername;
                    userlogmodel.description = "datatoimport " + mastername + ".datatoimport Id is " + result.ToString();
                    oCommonRepository.useractivityAdd(userlogmodel);
                }
                return result;
            }
        }
        /// To insert the newly added role information
        public int addlog(object data, string mastername)
        {
            using (IDbConnection dbConnection = ConnectionBase.Connection)
            {
                dbConnection.Open();
                string input = JsonConvert.SerializeObject(data);
                int result = 0;
                string query = @"public.sp" + mastername;
                var param = new DynamicParameters();
                param.Add("inputjsonstr", dbType: DbType.String, value: input, direction: ParameterDirection.Input);
                param.Add("mode_p", dbType: DbType.String, value: "insert", direction: ParameterDirection.Input);
                result = dbConnection.Query<int>(query, param, commandType: CommandType.StoredProcedure).First();
                return result;
            }
        }
        //ccwms
        public object PostCCJobMaster(string spname, Int64 userid, Int64 projecdefid)
        {
            try
            {
                using (IDbConnection dbConnection = ConnectionBase.Connection)
                {
                    dbConnection.Open();
                    //int result = 0;
                    string query = spname;
                    var param = new DynamicParameters();
                    param.Add("p_userid", dbType: DbType.Int32, value: userid, direction: ParameterDirection.Input);
                    param.Add("p_projectdefid", dbType: DbType.Int32, value: projecdefid, direction: ParameterDirection.Input);
                    return dbConnection.Query<string>(query, param, commandType: CommandType.StoredProcedure).First();
                    //code to add user activity log
                    //if (result != 0)
                    //{
                    //    return oCommonRepository.CCExecuteSP_ReturnList("cc_sp_getuserjob", userid, projecdefid);
                    //}
                    //return result;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public object Master_StateCode(string spname, string inputval, string mode_p)
        {

            try
            {
                using (IDbConnection dbConnection = ConnectionBase.Connection)
                {
                    dbConnection.Open();
                    //int result = 0;
                    string query = spname;
                    var param = new DynamicParameters();
                    param.Add("mode_p", dbType: DbType.String, value: mode_p, direction: ParameterDirection.Input);
                    if(inputval != "")
                    {
                        param.Add("inputjsonstr", dbType: DbType.String, value: inputval, direction: ParameterDirection.Input);
                    }
                   
                    return dbConnection.Query<string>(query, param, commandType: CommandType.StoredProcedure).First();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public object Master_Frequency(string spname, string inputval, string mode_p)
        {

            try
            {
                using (IDbConnection dbConnection = ConnectionBase.Connection)
                {
                    dbConnection.Open();
                    //int result = 0;
                    string query = spname;
                    var param = new DynamicParameters();
                    param.Add("mode_p", dbType: DbType.String, value: mode_p, direction: ParameterDirection.Input);
                    if (inputval != "")
                    {
                        param.Add("inputjsonstr", dbType: DbType.String, value: inputval, direction: ParameterDirection.Input);
                    }

                    return dbConnection.Query<string>(query, param, commandType: CommandType.StoredProcedure).First();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public object Master_select(string spname, string inputval, string mode_p)
        {

            try
            {
                using (IDbConnection dbConnection = ConnectionBase.Connection)
                {
                    dbConnection.Open();
                    //int result = 0;
                    string query = spname;
                    var param = new DynamicParameters();
                    param.Add("mode_p", dbType: DbType.String, value: mode_p, direction: ParameterDirection.Input);
                    if (inputval != "")
                    {
                        param.Add("inputjsonstr", dbType: DbType.String, value: inputval, direction: ParameterDirection.Input);
                    }

                    return dbConnection.Query<string>(query, param, commandType: CommandType.StoredProcedure).First();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public object ExcecuteSP(string spname, string inputval, string mode_p)
        {
            try
            {
                using (IDbConnection dbConnection = ConnectionBase.ConnectionAI)
                {
                    dbConnection.Open();
                    //int result = 0;
                    string query = spname;
                    var param = new DynamicParameters();
                    param.Add("mode_p", dbType: DbType.String, value: mode_p, direction: ParameterDirection.Input);
                    if (inputval != "")
                    {
                        param.Add("inputjsonstr", dbType: DbType.String, value: inputval, direction: ParameterDirection.Input);
                    }
                    return dbConnection.Query<string>(query, param, commandType: CommandType.StoredProcedure).First();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public object AI_ExecuteSP_ReturnList(string spname, string inputjsonstring, string mode)
        {
            using (IDbConnection dbConnection = ConnectionBase.ConnectionAI)
            {
                dbConnection.Open();
                var param = new DynamicParameters();
                string query = spname;
                param.Add("inputjsonstr", inputjsonstring);
                param.Add("mode_p", dbType: DbType.String, value: mode, direction: ParameterDirection.Input);
                return dbConnection.Query<string>(query, param, commandType: CommandType.StoredProcedure);
            }
        }
    }

    
}
