using Dapper;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Data;
using System.Linq;
using System.Threading;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace WK_KLI_LAPS_Service.App_Data
{
    public class DashboardRepository
    {
        public object getJobStatusReport(string spname, string data)
        {
            var result = JsonConvert.DeserializeObject<jobStatus>(data);
            using (IDbConnection dbConnection = ConnectionBase.Connection)
            {
                dbConnection.Open();
                var param = new DynamicParameters();
                string query = spname;
                param.Add("p_jobid", dbType: DbType.Int64, value: result.p_jobid, direction: ParameterDirection.Input);
                // param.Add("p_jobid", result.p_jobid);
                param.Add("p_jobstatus", dbType: DbType.Boolean, value: result.p_jobstatus, direction: ParameterDirection.Input);
                if (result.p_userid != 0)
                {
                    param.Add("p_userid", dbType: DbType.Int64, value: result.p_userid, direction: ParameterDirection.Input);
                }
                if (result.p_pagenumber != 0)
                {
                    //param.Add("p_pagenumber", dbType: DbType.Int64, value: result.p_pagenumber, direction: ParameterDirection.Input);
                    param.Add("p_pagenumber", result.p_pagenumber);
                   
                }
                if (result.p_pagesize != 0)
                {
                    // param.Add("p_pagesize", dbType: DbType.Int64, value: result.p_pagesize, direction: ParameterDirection.Input);
                    param.Add("p_pagesize", result.p_pagesize);
                }
                return dbConnection.Query<string>(query, param, commandType: CommandType.StoredProcedure);
            }
        }

        public object getLoadType(string spname, string inputjsonstring, string mode)
        {
            //var result = JsonConvert.DeserializeObject<jobStatus>(inputjsonstring);
            object ViewBag = new { };
            using (IDbConnection dbConnection = ConnectionBase.Connection)
            {
                dbConnection.Open();

                var param1 = new DynamicParameters();
                var param2 = new DynamicParameters();

                param1.Add("inputjsonstr", inputjsonstring);
                param1.Add("mode_p", dbType: DbType.String, value: "isvendor", direction: ParameterDirection.Input);
                var _list = dbConnection.Query<string>(spname, param1, commandType: CommandType.StoredProcedure).ToList();

                bool isVendorConfirm = false;
                if (_list.Count() > 0 && _list.ToList()[0] != null)
                {
                    var TempData = _list[0].ToString();
                    TempData = TempData.Replace("[", "");
                    TempData = TempData.Replace("]", "");
                    //var getVal = JsonConvert.DeserializeObject<isVendor>(TempData).ToString();
                    JObject obj = JObject.Parse(TempData);
                    string msg = (string)obj["isvendor"];
                    if (msg == null)
                    {
                        msg = "0";
                    }
                    if (msg.ToString() == "1")
                    {
                        isVendorConfirm = true;

                        ViewBag = new
                        {
                            isPM = "isPMVisible",
                            isCol = "7",
                            isCol1 = "5",
                            loadtype = "vendor"
                        };
                    }
                }

                if (isVendorConfirm == false)
                {
                    param2.Add("inputjsonstr", inputjsonstring);
                    param2.Add("mode_p", dbType: DbType.String, value: "getuseraspm", direction: ParameterDirection.Input);
                    var getResult2 = dbConnection.Query<string>(spname, param2, commandType: CommandType.StoredProcedure);

                    if (getResult2.Count() > 0 && getResult2.ToList()[0] != null)
                    {
                        ViewBag = new
                        {
                            isPM = "isPMVisible",
                            isCol = "7",
                            isCol1 = "5",
                            loadtype = "dashboard"
                        };
                    }
                    else
                    {
                        ViewBag = new
                        {
                            isPM = "isPMHide",
                            isCol = "12",
                            isCol1 = "0",
                            loadtype = "user"
                        };
                    }
                }

                return ViewBag;
            }
        }

        public object getProjectJobDetails(string spname, string data)
        {
            var result = JsonConvert.DeserializeObject<jobStatus>(data);
            using (IDbConnection dbConnection = ConnectionBase.Connection)
            {
                dbConnection.Open();
                var param = new DynamicParameters();
                string query = spname;
                param.Add("p_userid", result.p_userid);
               // param.Add("p_jobid", result.p_jobid);
               param.Add("p_projectdefinitionid", result.p_projectdefinitionid);
                Thread.Sleep(1000);
                return dbConnection.Query<string>(query, param, commandType: CommandType.StoredProcedure);
            }

        }
    }
}

public class jobStatus
{
    public int p_jobid { get; set; }
    public bool p_jobstatus { get; set; }
    public int p_userid { get; set; }
    public int p_projectdefinitionid { get; set; }
    public int p_pagesize { get; set; }
    public int p_pagenumber { get; set; }
    public string mode { get; set; }
}
