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


namespace WK_KLI_LAPS_Service.App_Data
{
    public class CheckListDetailsRepository
    {
        useractivitylogModel userlogmodel;
        CommonRepository oCommonRepository = new CommonRepository();
        /// 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 _roleexists = CheckExists(mastername, input);
                int result = 0;
                if (_roleexists == false)
                {
                    string query = @"public.spchecklistdetails";
                    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;
            }
        }

        //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 _roleexists = CheckExists(mastername, input);
                int result = 0;
                if (_roleexists == false)
                {
                    string query = @"public.spchecklistdetails";
                    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.spchecklistdetails";
                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;
            }
        }

        //Check the given name is exists or not
        public bool CheckExists(string mastername, string inputjsonval)
        {
            bool result = false;
            using (IDbConnection dbConnection = ConnectionBase.Connection)
            {

                dbConnection.Open();
                var param = new DynamicParameters();
                string query = @"public.spchecklistdetailsselect";
                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 object GetAll(string mastername, int checklistid)
        {
            using (IDbConnection dbConnection = ConnectionBase.Connection)
            {
                dbConnection.Open();
                string input = JsonConvert.SerializeObject(new { checklistid });
                var param = new DynamicParameters();
                string query = @"public.spchecklistdetailsselect";
                param.Add("inputjsonstr", input);
                param.Add("mode_p", dbType: DbType.String, value: "getbychecklistid", direction: ParameterDirection.Input);
                return dbConnection.Query<string>(query, param, commandType: CommandType.StoredProcedure);
            }
        }

        public object GetActive(string mastername)
        {
            using (IDbConnection dbConnection = ConnectionBase.Connection)
            {
                dbConnection.Open();
                var param = new DynamicParameters();
                string query = @"public.spchecklistdetailsselect";
                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);

            }
        }
    }
}
