using Npgsql;
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using LAPS_WATCHER.Configure;

namespace LAPS_IOService.Models
{
    class DB_Library
    {
        public static DataTable GetDataTable(string Qry, List<NpgsqlParameter> paramet = null)
        {
            NpgsqlConnection postgrescon = new NpgsqlConnection(CustomConfiguration.AppSettings["DBInfo:" + eGlob.Environment]);
            DataTable dtable = new DataTable();
            try
            {
                NpgsqlCommand cmd = new NpgsqlCommand(Qry, postgrescon);
                if (paramet != null)
                {
                    foreach (NpgsqlParameter sqlpara in paramet)
                        cmd.Parameters.Add(sqlpara);
                    cmd.CommandType = CommandType.StoredProcedure;
                }
                else
                {
                    cmd.CommandType = CommandType.Text;
                }
                NpgsqlDataAdapter adp = new NpgsqlDataAdapter(cmd);
                adp.Fill(dtable);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.StackTrace);
            }
            return dtable;
        }
        public static void InsertDataNonQuery(string Qry, List<NpgsqlParameter> paramet = null)
        {
            NpgsqlConnection postgrescon = new NpgsqlConnection(CustomConfiguration.AppSettings["DBInfo:" + eGlob.Environment]);
            try
            {
                postgrescon.Open();
                NpgsqlCommand cmd = new NpgsqlCommand(Qry, postgrescon);
                if (paramet != null)
                {
                    foreach (NpgsqlParameter sqlpara in paramet)
                        cmd.Parameters.Add(sqlpara);
                    cmd.CommandType = CommandType.StoredProcedure;
                }
                else
                {
                    cmd.CommandType = CommandType.Text;
                }
                cmd.ExecuteNonQuery();
                postgrescon.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.StackTrace);
                postgrescon.Close();
            }
        }
        public static object InsertDataScalar(string Qry, List<NpgsqlParameter> paramet = null)
        {
            NpgsqlConnection postgrescon = new NpgsqlConnection(CustomConfiguration.AppSettings["DBInfo:" + eGlob.Environment]);
            try
            {
                postgrescon.Open();
                NpgsqlCommand cmd = new NpgsqlCommand(Qry, postgrescon);
                if (paramet != null)
                {
                    foreach (NpgsqlParameter sqlpara in paramet)
                        cmd.Parameters.Add(sqlpara);
                    cmd.CommandType = CommandType.StoredProcedure;
                }
                else
                {
                    cmd.CommandType = CommandType.Text;
                }
                var obj = cmd.ExecuteScalar();
                postgrescon.Close();
                return obj;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.StackTrace);
                postgrescon.Close();
                return "";
            }
        }
        public static object UpdateDataScalar(string Qry, List<NpgsqlParameter> paramet = null)
        {
            NpgsqlConnection postgrescon = new NpgsqlConnection(CustomConfiguration.AppSettings["DBInfo:" + eGlob.Environment]);
            try
            {
                postgrescon.Open();
                NpgsqlCommand cmd = new NpgsqlCommand(Qry, postgrescon);
                if (paramet != null)
                {
                    foreach (NpgsqlParameter sqlpara in paramet)
                        cmd.Parameters.Add(sqlpara);
                    cmd.CommandType = CommandType.StoredProcedure;
                }
                else
                {
                    cmd.CommandType = CommandType.Text;
                }
                var obj = cmd.ExecuteNonQuery();
                postgrescon.Close();
                return obj;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.StackTrace);
                postgrescon.Close();
                return "";
            }
        }
        public static string GetsCallerValue(string Qry, List<NpgsqlParameter> paramet = null)
        {
            NpgsqlConnection postgrescon = new NpgsqlConnection(CustomConfiguration.AppSettings["DBInfo:" + eGlob.Environment]);
            try
            {
                postgrescon.Open();
                NpgsqlCommand cmd = new NpgsqlCommand(Qry, postgrescon);
                if (paramet != null)
                {
                    foreach (NpgsqlParameter sqlpara in paramet)
                        cmd.Parameters.Add(sqlpara);
                    cmd.CommandType = CommandType.StoredProcedure;
                }
                else
                {
                    cmd.CommandType = CommandType.Text;
                }
                string USRole = (string)cmd.ExecuteScalar();
                postgrescon.Close();
                return USRole;
                
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.StackTrace);
                postgrescon.Close();
            }
            return null;
        }
    }
    public class modcommon
    {
        public static string ns(object val)
        {
            try
            {
                return val.ToString();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.StackTrace);
                return string.Empty;
            }
        }
        public static int nz(object val)
        {
            try
            {
                return int.Parse(val.ToString());
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.StackTrace);
                return 0;
            }
        }
        public static Int16 n_16(object val)
        {
            try
            {
                return Int16.Parse(val.ToString());
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.StackTrace);
                return 0;
            }
        }
        public static Int32 n_32(object val)
        {
            try
            {
                return Int32.Parse(val.ToString());
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.StackTrace);
                return 0;
            }
        }
        public static Int64 n_64(object val)
        {
            try
            {
                return Int64.Parse(val.ToString());
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.StackTrace);
                return 0;
            }
        }
    }
}