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 WorkFlowConfigurationRepository
    {
        useractivitylogModel userlogmodel;
        CommonRepository oCommonRepository = new CommonRepository();
        public object add(object data)
        {
            using (IDbConnection dbConnection = ConnectionBase.Connection)
            {
                dbConnection.Open();
                string input = JsonConvert.SerializeObject(data);
                //bool _roleexists = CheckExists(mastername, input);
                // string result ="";
                //if (_roleexists == false)
                //{
                string query = @"public.spworkflowdefinition";
                var param = new DynamicParameters();
                param.Add("inputjsonstr", dbType: DbType.String, value: input, direction: ParameterDirection.Input);
                param.Add("mode_p", dbType: DbType.String, value: "addstage", direction: ParameterDirection.Input);
                var result = dbConnection.Query<string>(query, param, commandType: CommandType.StoredProcedure);
                if (result.Count() > 0 && result.ToList()[0] != null)
                //}
                //code to add user activity log
                {
                    userlogmodel = new useractivitylogModel();
                    userlogmodel.useraction = "Add workflowdefinition";
                    userlogmodel.description = "Added workflowdefinition";
                    oCommonRepository.useractivityAdd(userlogmodel);
                }
                return result;
            }
        }
        //code to edit specific data
        public object Edit(string inputstr, string mode)
        {

            using (IDbConnection dbConnection = ConnectionBase.Connection)
            {
                dbConnection.Open();

                string query = @"public.spworkflowdefinition";
                var param = new DynamicParameters();
                param.Add("inputjsonstr", dbType: DbType.String, value: inputstr, direction: ParameterDirection.Input);
                param.Add("mode_p", dbType: DbType.String, value: mode, direction: ParameterDirection.Input);
                return dbConnection.Query<string>(query, param, commandType: CommandType.StoredProcedure);
            }
        }
        //To activate or deactivate the selected record
        public string Delete(object data)
        {
            using (IDbConnection dbConnection = ConnectionBase.Connection)
            {
                dbConnection.Open();
                string input = JsonConvert.SerializeObject(data);
                string query = @"public.spworkflowdefinition";
                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);
                var result = dbConnection.Query<string>(query, param, commandType: CommandType.StoredProcedure).First();
                if (result.Count() > 0 && result.ToList()[0] != null)
                {
                    userlogmodel = new useractivitylogModel();
                    userlogmodel.useraction = "Delete workflowdefinition";
                    userlogmodel.description = "Deleted  workflowdefinition. Deleted Id is " + result.ToString();
                    oCommonRepository.useractivityAdd(userlogmodel);
                }
                return result;
            }
        }

        // Get the workflow definition details based on workflow id
        public object GetById(string inputjsonval, string mode)
        {
            using (IDbConnection dbConnection = ConnectionBase.Connection)
            {
                dbConnection.Open();
                var param = new DynamicParameters();
                string query = @"public.spworkflowdefinitionselect";
                param.Add("inputjsonstr", inputjsonval);
                param.Add("mode_p", dbType: DbType.String, value: mode, direction: ParameterDirection.Input);
                return dbConnection.Query<string>(query, param, commandType: CommandType.StoredProcedure);

            }
        }

        // Get the flowchart for given workflow definition id
        public object GetFlowChart(string inputjsonval)
        {
            using (IDbConnection dbConnection = ConnectionBase.Connection)
            {
                dbConnection.Open();
                var param = new DynamicParameters();
                string query = @"public.spworkflowdefinitionselect";
                param.Add("inputjsonstr", inputjsonval);
                param.Add("mode_p", dbType: DbType.String, value: "getflowchart", direction: ParameterDirection.Input);
                return dbConnection.Query<string>(query, param, commandType: CommandType.StoredProcedure);

            }
        }
        //code to edit specific data
        public object UpdateInOutFile(string inputstr, string mode)
        {

            using (IDbConnection dbConnection = ConnectionBase.Connection)
            {
                dbConnection.Open();

                string query = @"public.spinoutfiles";
                var param = new DynamicParameters();
                param.Add("inputjsonstr", dbType: DbType.String, value: inputstr, direction: ParameterDirection.Input);
                param.Add("mode_p", dbType: DbType.String, value: mode, direction: ParameterDirection.Input);
                return dbConnection.Query<string>(query, param, commandType: CommandType.StoredProcedure);
            }
        }

        // Get the inputoutputfile details based on workflow id
        public object IOGetByWfdId(string inputjsonval, string mode)
        {
            using (IDbConnection dbConnection = ConnectionBase.Connection)
            {
                dbConnection.Open();
                var param = new DynamicParameters();
                string query = @"public.spinoutfilesselect";
                param.Add("inputjsonstr", inputjsonval);
                param.Add("mode_p", dbType: DbType.String, value: mode, direction: ParameterDirection.Input);
                return dbConnection.Query<string>(query, param, commandType: CommandType.StoredProcedure);
            }
        }
    }
}
