using System;
using System.Data;
using Kreta.Core;
using Kreta.DataAccess.Interfaces;
using Kreta.DataAccessManual.Interfaces;
using Kreta.DataAccessManual.ParameterClasses;
using Kreta.DataAccessManual.Util;
using Kreta.Enums;
using Kreta.Enums.ManualEnums;
using Kreta.Framework;
using SDA.DataProvider;
using SDA.Kreta.Entities;

namespace Kreta.DataAccessManual
{
    internal class JuttatasDAL : DataAccessBase, IJuttatasDal
    {
        public JuttatasDAL(DalHandler dalHandler)
            : base(dalHandler)
        {

        }

        public IJuttatas Get()
        {
            return Juttatas.GiveAnInstance();
        }

        public IJuttatas Get(int id)
        {
            var entity = Juttatas.GiveAnInstance();
            entity.LoadByID(id);
            return entity;
        }

        public int Insert(IJuttatas dto)
        {
            var entity = dto as Juttatas;
            entity.Insert();

            DalHelper.Commit();
            return entity.ID;
        }

        public void Update(IJuttatas dto)
        {
            var entity = dto as Juttatas;
            entity.Update();
            DalHelper.Commit();
        }

        public void UpdateTanulokSzakkepzoAdatok(int felhasznaloId, string tanulokIdString, SzakkepzoAdatokPCO szakkepzoAdatok)
        {
            using (var command = new SDACommand())
            {
                command.Connection = UserContext.Instance.SDAConnection;
                command.Transaction = UserContext.Instance.SDATransaction;
                command.CommandType = CommandType.StoredProcedure;
                command.CommandText = "uspUpdateTanulokSzakkepzoAdatok";

                command.Parameters.Add("pTanulokIdString", tanulokIdString);
                command.Parameters.Add("pJogviszonyTipus", szakkepzoAdatok.JogviszonyTipus);
                command.Parameters.Add("pFelhasznaloId", felhasznaloId);

                command.ExecuteNonQuery();

                DalHelper.Commit();
            }
        }

        public void UpdateTanulokSzakkepzesiJuttatasok(int tanevId, int felhasznaloId, int juttatasTipusId, int? tanuloId = null, string tanulokIdString = null)
        {
            using (var command = new SDACommand())
            {
                command.Connection = UserContext.Instance.SDAConnection;
                command.Transaction = UserContext.Instance.SDATransaction;
                command.CommandType = CommandType.StoredProcedure;
                command.CommandText = "uspUpdateTanuloSzakkepzesiJuttatasok";

                if (tanuloId.IsEntityId())
                {
                    command.Parameters.Add("pTanuloId", tanuloId.Value);
                }
                if (!string.IsNullOrWhiteSpace(tanulokIdString))
                {
                    command.Parameters.Add("pTanuloIdList", tanulokIdString);
                }

                command.Parameters.Add("pTanevId", tanevId);
                command.Parameters.Add("pMaxIgazolatlanokSzama", Constants.MinMaxValues.MaxIgazolatlanJuttatasokhoz);
                command.Parameters.Add("pMinOsztondijAtlag", Constants.MinMaxValues.MinOsztondijAtlag);
                command.Parameters.Add("pOsztondijAlap", Constants.General.JuttatasAlap);
                command.Parameters.Add("pLekerdezesDatuma", DateTime.Today);
                command.Parameters.Add("pFelhasznaloId", felhasznaloId);
                command.Parameters.Add("pGeneraltHonap", null);
                command.Parameters.Add("pJuttatasTipusId", SDADBType.Int).Value = juttatasTipusId;

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

        public bool IsExitingJogosultJuttatas(int tanuloId)
        {
            using (var command = new SDACommand())
            {
                command.Connection = UserContext.Instance.SDAConnection;
                command.Transaction = UserContext.Instance.SDATransaction;

                command.Parameters.Add("pTanuloId", tanuloId);
                command.CommandText = @"
                    IF EXISTS
                    (
                        SELECT 1 FROM T_JUTTATAS_OSSZES j
                        WHERE
                            j.C_TANULOID = :pTanuloId AND j.C_ISJOGOSULT = 'T' AND j.TOROLT = 'F' AND j.C_GENERALTHONAP IS NOT NULL
                            AND C_ELUTASITASOKA = 0
                    )
                        SELECT 1
                    ELSE
                        SELECT 0
                ";

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

        public void UpdateOsszesTanuloAtlag(int felhasznaloId, AtlagTipusEnum atlagTipus, int tanevId, int kovTanevId)
        {
            using (var command = new SDACommand())
            {
                command.Connection = UserContext.Instance.SDAConnection;
                command.Transaction = UserContext.Instance.SDATransaction;
                command.CommandType = CommandType.StoredProcedure;
                command.CommandText = atlagTipus == AtlagTipusEnum.Felevi ? "uspUpdateSzamitottAtlagFelevi" : "uspUpdateSzamitottAtlagEvvegi";
                command.Parameters.Add("pTanevId", SDADBType.Int).Value = tanevId;
                command.Parameters.Add("pKovTanevId", SDADBType.Int).Value = kovTanevId;
                if (atlagTipus == AtlagTipusEnum.Felevi)
                {
                    command.Parameters.Add("pDate", SDADBType.DateTime).Value = DateTime.Today;
                }
                command.Parameters.Add("pFelhasznaloId", SDADBType.Int).Value = felhasznaloId;

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

        public void UpdateTanuloEpJuttatasok(int tanevId, int felhasznaloId, int tanuloId)
        {
            using (var command = new SDACommand())
            {
                command.Connection = UserContext.Instance.SDAConnection;
                command.Transaction = UserContext.Instance.SDATransaction;
                command.CommandType = CommandType.StoredProcedure;
                command.CommandText = "uspUpdateTanuloEpJuttatas";

                command.Parameters.Add("pTanevId", SDADBType.Int).Value = tanevId;
                command.Parameters.Add("pMinErdemjegy", SDADBType.Double).Value = Constants.MinMaxValues.MinEpjErtekeles;
                command.Parameters.Add("pJuttatasAlap", SDADBType.Int).Value = Constants.General.JuttatasAlap;
                command.Parameters.Add("pTanuloId", SDADBType.Int).Value = tanuloId;
                command.Parameters.Add("pFelhasznaloId", SDADBType.Int).Value = felhasznaloId;

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

    }
}