using System;
using System.Collections.Generic;
using System.Data;
using Kreta.Core;
using Kreta.DataAccessManual.Util;
using Kreta.Enums;
using Kreta.Framework;
using Kreta.Framework.Util;
using SDA.DataProvider;

namespace Kreta.DataAccessManual
{
    internal class AdminDAL : DataAccessBase, IAdminDAL
    {
        public AdminDAL(DalHandler handler, GridParameters parameters)
            : base(handler, parameters)
        {
        }

        public AdminDAL(DalHandler handler) : base(handler)
        {
        }

        public DataSet GetAdminDashboardAdminisztracio(int tanevId)
        {
            using (var command = new SDACommand())
            {
                command.Connection = UserContext.Instance.SDAConnection;
                command.Transaction = UserContext.Instance.SDATransaction;
                command.CommandType = CommandType.StoredProcedure;
                command.CommandText = "uspGetAdminDashboardAdminisztracio";

                command.Parameters.Add("pTanevId", tanevId);
                command.Parameters.Add("pNaFeladatellatasihelyTipus", (int)OktatasiNevelesiFeladatEnum.na);
                command.Parameters.Add("pNaEvfolyamTipusa", (int)EvfolyamTipusEnum.na);
                command.Parameters.Add("pNaCsoportTipusa", (int)CsoportTipusEnum.na);
                command.Parameters.Add("pNaNemTipusa", (int)NemEnum.NA);
                command.Parameters.Add("pNaAlkalmazottMunkakor", (int)MunkakorTipusEnum.na);
                command.Parameters.Add("pNaAlkalmazottMunkaviszony", (int)MunkaviszonyTipusEnum.na);
                command.Parameters.Add("pNaBesorolasiFokozat", (int)BesorolasiFokozatTipusEnum.na);
                command.Parameters.Add("pNaTargyKategoria", (int)TargyKategoriaTipusEnum.na);
                command.Parameters.Add("pNincsAnyanyelv", (int)AnyanyelvEnum.na);
                command.Parameters.Add("pNincsAllampolgarsag", (int)AllampolgarsagEnum.na);
                command.Parameters.Add("pTanarAlkalmazottMunkakor", (int)AlkalmazottMunkaKorTipusEnum.tanar);

                var ds = new DataSet();
                using (var adapter = new SDADataAdapter())
                {
                    adapter.SelectCommand = command;
                    adapter.Fill(ds);
                }
                return ds;
            }
        }

        public DataSet GetAdminDashboardNaplo(int tanevId)
        {
            using (var command = new SDACommand())
            {
                command.Connection = UserContext.Instance.SDAConnection;
                command.Transaction = UserContext.Instance.SDATransaction;
                command.CommandType = CommandType.StoredProcedure;
                command.CommandText = "sp_GetAdminDashboardNaplo";

                command.Parameters.Add("pTanevId", tanevId);
                command.Parameters.Add("pTanarAlkalmazottMunkakor", (int)AlkalmazottMunkaKorTipusEnum.tanar);

                var ds = new DataSet();
                using (var adapter = new SDADataAdapter())
                {
                    adapter.SelectCommand = command;
                    adapter.Fill(ds);
                }
                return ds;
            }
        }

        public DataSet GetAdminDashboardStatisztika(int tanevId, DateTime datum)
        {
            using (var command = new SDACommand())
            {
                command.Connection = UserContext.Instance.SDAConnection;
                command.Transaction = UserContext.Instance.SDATransaction;
                command.CommandType = CommandType.StoredProcedure;
                command.CommandText = "uspGetAdminDashboardStatisztika";

                command.Parameters.Add("pTanevId", tanevId);
                command.Parameters.Add("pDatum", datum);
                command.Parameters.Add("pTanarAlkalmazottMunkakor", (int)AlkalmazottMunkaKorTipusEnum.tanar);

                var ds = new DataSet();
                using (var adapter = new SDADataAdapter())
                {
                    adapter.SelectCommand = command;
                    adapter.Fill(ds);
                }
                return ds;
            }
        }

        public DataSet GetAdminDashboardAdattisztitasAlkalmazott(int intezmenyId, int tanevId, bool isCount)
        {
            using (var command = new SDACommand())
            {
                command.Connection = UserContext.Instance.SDAConnection;
                command.Transaction = UserContext.Instance.SDATransaction;
                command.CommandType = CommandType.StoredProcedure;
                command.CommandText = "uspAdattisztitasAlkalmazott";

                command.Parameters.Add("pIntezmenyId", intezmenyId);
                command.Parameters.Add("pTanevId", tanevId);
                command.Parameters.Add("pIsCount", isCount);

                var ds = new DataSet();
                using (var adapter = new SDADataAdapter())
                {
                    adapter.SelectCommand = command;
                    adapter.Fill(ds);
                }
                return ds;
            }
        }

        public DataSet GetAktivTanevValtasaModel(int tanevId, int kovTanevId, int intezmenyId, DateTime? szakgyakKezdoDatum, DateTime? szakgyakVegDatum)
        {
            using (var command = new SDACommand())
            {
                /*TODO:Tobb Osztaly*//*OM-1612*/
                command.Connection = UserContext.Instance.SDAConnection;
                command.Transaction = UserContext.Instance.SDATransaction;
                command.CommandType = CommandType.StoredProcedure;
                command.CommandText = "uspGetAktivTanevValtasaModel";

                command.Parameters.Add("pTanevId", tanevId);
                command.Parameters.Add("pIntezmenyId", intezmenyId);
                command.Parameters.Add("pKovTanevId", kovTanevId);
                command.Parameters.Add("pSzakgyakKezdoDatum", szakgyakKezdoDatum);
                command.Parameters.Add("pSzakgyakVegDatum", szakgyakVegDatum);
                command.Parameters.Add("pOsszefuggoSzakGyakTipus", (int)CsoportTipusEnum.OsszefuggoSzakmaiGyakorlatiCsoport);
                command.Parameters.Add("pOktNevKatTipus", (int)OktNevelesiKategoriaEnum.NevelesOktatas);

                var ds = new DataSet();
                using (var adapter = new SDADataAdapter())
                {
                    adapter.SelectCommand = command;
                    adapter.Fill(ds);
                }
                return ds;
            }
        }

        public void AktivTanevValtasa(int aktTanevId, int kovTanevId, int intezmenyId, int felhasznaloId)
        {
            using (SDACommand command = new SDACommand())
            {
                command.Connection = UserContext.Instance.SDAConnection;
                command.Transaction = UserContext.Instance.SDATransaction;
                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.Add("pintezmenyId", intezmenyId);
                command.Parameters.Add("paktTanevId", aktTanevId);
                command.Parameters.Add("pkovTanevId", kovTanevId);
                command.Parameters.Add("pfelhasznaloId", felhasznaloId);

                command.CommandText = "sp_AktivTanevValtasa";

                command.ExecuteNonQuery();
                DalHelper.Commit();
            }
        }

        public void ElozoTanevesViewokTorlese(int intezmenyId, string intezmenyAzonosito, int kovTanevId)
        {
            using (var command = new SDACommand())
            {
                command.Connection = UserContext.Instance.SDAConnection;
                command.Transaction = UserContext.Instance.SDATransaction;
                command.CommandType = CommandType.StoredProcedure;

                command.Parameters.Add("pintezmenyId", intezmenyId);
                command.Parameters.Add("pintezmenyAzonosito", intezmenyAzonosito);
                command.Parameters.Add("pkovTanevId", kovTanevId);

                command.CommandText = "sp_ElozoTanevesViewokTorlese";

                command.ExecuteNonQuery();
                DalHelper.Commit();
            }
        }

        public bool MustRefreshRendszerErtesitesek()
        {
            using (var command = new SDACommand())
            {
                command.Connection = UserContext.Instance.SDAConnection;
                command.Transaction = UserContext.Instance.SDATransaction;
                command.CommandType = CommandType.StoredProcedure;
                command.CommandText = "uspMustRefreshRendszerErtesitesek";

                int cnt = Convert.ToInt32(command.ExecuteScalar());
                return cnt > 0;
            }
        }

        public DateTime GetLastRendszerErtesitesek()
        {
            using (var command = new SDACommand())
            {
                command.Connection = UserContext.Instance.SDAConnection;
                command.Transaction = UserContext.Instance.SDATransaction;
                command.CommandType = CommandType.StoredProcedure;
                command.CommandText = "uspGetLastRendszerErtesitesek";

                return Convert.ToDateTime(command.ExecuteScalar());
            }
        }

        public int GetKretaVerzio(int intezmenyId, int tanevId)
        {
            using (var command = new SDACommand())
            {
                command.Connection = UserContext.Instance.SDAConnection;
                command.Transaction = UserContext.Instance.SDATransaction;
                command.CommandType = CommandType.StoredProcedure;
                command.CommandText = "uspGetKretaVerzio";

                command.Parameters.Add("pIntezmenyId", intezmenyId);
                command.Parameters.Add("pTanevId", tanevId);

                return Convert.ToInt32(command.ExecuteScalar());
            }
        }

        public DataSet GetKIRAdattisztitas(string intezmenyAzonosito)
        {
            using (var command = new SDACommand())
            {
                command.Connection = UserContext.Instance.SDAConnection;
                command.Transaction = UserContext.Instance.SDATransaction;
                command.CommandType = CommandType.StoredProcedure;
                command.CommandText = "uspGetKIRAdattisztitas";

                command.Parameters.Add("pIntezmenyAzonosito", intezmenyAzonosito);

                var ds = new DataSet();
                using (var adapter = new SDADataAdapter())
                {
                    adapter.SelectCommand = command;
                    adapter.Fill(ds);
                }
                return ds;
            }
        }

        public DataSet GetNemValidOktAzon(string intezmenyAzonosito, int? tanevId)
        {
            List<CommandParameter> parameters = new List<CommandParameter>
            {
                new CommandParameter(nameof(intezmenyAzonosito), intezmenyAzonosito)
            };

            var commandText = $@"
                SELECT
                    C_OKTATASIAZONOSITO AS OktAzonosito
                FROM T_FELHASZNALO_OSSZES fh
                INNER JOIN T_INTEZMENY_OSSZES i ON i.ID = fh.C_INTEZMENYID AND i.TOROLT = 'F'
                INNER JOIN T_TANEV_OSSZES t ON fh.C_TANEVID = t.ID AND t.TOROLT = 'F'
                INNER JOIN dev.KK_hibas_OktasiAzonositok temp ON fh.C_OKTATASIAZONOSITO = temp.OktAzon
                WHERE
                    i.C_AZONOSITO = @{nameof(intezmenyAzonosito)}
                AND fh.TOROLT = 'F'
                ";

            if (tanevId.IsEntityId())
            {
                parameters.Add(new CommandParameter(nameof(tanevId), tanevId));

                var whereClause = $"AND t.ID = @{nameof(tanevId)}";

                commandText += whereClause;
            }

            DataSet ds = GetData(commandText, parameters);

            return ds;
        }

        public DataSet GetAdminEmail(string intezmenyAzonosito)
        {
            using (var command = new SDACommand())
            {
                command.Connection = UserContext.Instance.SDAConnection;
                command.Transaction = UserContext.Instance.SDATransaction;
                command.CommandType = CommandType.StoredProcedure;
                command.CommandText = "uspGetAdminEmail";

                command.Parameters.Add("pIntezmenyAzonosito", intezmenyAzonosito);

                var ds = new DataSet();
                using (var adapter = new SDADataAdapter())
                {
                    adapter.SelectCommand = command;
                    adapter.Fill(ds);
                }
                return ds;
            }
        }
    }
}