using System.Collections.Generic;
using Kreta.BusinessLogic.Classes.MobileApi.Naplo.V2.Co;
using Kreta.BusinessLogic.Classes.MobileApi.Naplo.V2.Co.Get.Orarend;
using Kreta.BusinessLogic.HelperClasses;
using Kreta.BusinessLogic.Helpers;
using Kreta.Core.ConnectionType;
using Kreta.DataAccessManual.Interfaces;
using Kreta.DataAccessManual.Util;
using Kreta.Enums.ManualEnums;

namespace Kreta.BusinessLogic.Classes.MobileApi.Naplo.V2.SubqueriesRepo.Get
{
    public static class OrarendGetSubqueries
    {
        public static ResponseWrapperCo<List<OraGetResponseCo>> OraLista(int teacherId, IDalHandler h, int intezmenyId, int tanevId, OraGetRequestCo request, string intezmenyAzonosito)
        {
            var returnObject = new ResponseWrapperCo<List<OraGetResponseCo>>(request.Hash);
            //if (returnObject.IsDatabaseHashOk) return returnObject;

            var oraCoList = new List<OraGetResponseCo>();

            var eventList = new OrarendHelper(new DalHandlerConnectionType(new MobileConnectionType(teacherId, intezmenyId, intezmenyAzonosito, tanevId), h))
                .GetOrarendElemekMobilNaplo(request.Datum, request.Datum.AddDays(1), teacherId);

            foreach (var tanarOra in eventList)
            {
                if (tanarOra.allDay)
                    continue; //Különleges napok átugrása
                var oraResponseCo = new OraGetResponseCo
                {
                    OrarendiOraId = tanarOra.oraType == CalendarOraTypeEnum.OrarendiOra || tanarOra.oraType == CalendarOraTypeEnum.TanoranKivuliFoglalkozas ? tanarOra.id : (int?)null,
                    TanitasiOraId = tanarOra.oraType != CalendarOraTypeEnum.OrarendiOra && tanarOra.oraType != CalendarOraTypeEnum.TanoranKivuliFoglalkozas ? tanarOra.id : (int?)null,
                    Allapot = tanarOra.OraAllapota,
                    Kezdete = tanarOra.start.Value,
                    Vege = tanarOra.end.Value,
                    Oraszam = tanarOra.hanyadikora,
                    IsElmaradt = tanarOra.isElmaradt,
                    Tema = tanarOra.Tema,
                    TantargyId = tanarOra.TantargyId,
                    TantargyNev = tanarOra.TantargyTeljesNev,
                    TantargyKategoria = tanarOra.TantargyKategoria,
                    HelyettesitoId = tanarOra.helyettesitoId,
                    OsztalyCsoportId = tanarOra.TanevRendOsztalyCsoportId,
                    OsztalyCsoportNev = tanarOra.OsztalyCsoport,
                    TeremNev = tanarOra.Terem,
                    OraTulajdonosTanar = new TanarSimplifiedGetResponseCo { Id = tanarOra.TanarId, Nev = tanarOra.TanarNev },
                    EvesOraszam = tanarOra.EvesOraSorszam
                };

                var dktFeladatHelper = new DktFeladatHelper(new DalHandlerConnectionType(new MobileConnectionType(teacherId, intezmenyId, intezmenyAzonosito, tanevId), h));
                if (oraResponseCo.Allapot == TanoraAllapotaEnum.Naplozott)
                {
                    var haziFeladatCo = dktFeladatHelper.GetTanitasiOraHaziFeladatDetail(oraResponseCo.TanitasiOraId, null, false);
                    SetHaziFeladatData(oraResponseCo, haziFeladatCo);
                }
                else if (oraResponseCo.Allapot == TanoraAllapotaEnum.Nem_naplozott)
                {
                    var haziFeladatCo = dktFeladatHelper.GetOrarendiOraHaziFeladatDetail(oraResponseCo.OrarendiOraId, request.Datum, false, null, null);
                    SetHaziFeladatData(oraResponseCo, haziFeladatCo);
                }

                if (tanarOra.hanyadikora == -1 || tanarOra.oraType == CalendarOraTypeEnum.TanoranKivuliFoglalkozas || tanarOra.oraType == CalendarOraTypeEnum.TanoranKivuliNaplozottFoglalkozas)
                {
                    oraResponseCo.Oraszam = null;
                }

                if (tanarOra.oraType != CalendarOraTypeEnum.TanevRendjeEsemeny)
                {
                    var helper = new TanoraHelper(new MobileConnectionType(teacherId, intezmenyId, intezmenyAzonosito, tanevId));

                    oraResponseCo.EvesOraszam = helper.GetOraEvesSorszamaSP(oraResponseCo.TantargyId, oraResponseCo.OsztalyCsoportId, oraResponseCo.Kezdete, oraResponseCo.OraTulajdonosTanar.Id);
                }

                oraCoList.Add(oraResponseCo);
            }

            returnObject.FillData(oraCoList);
            return returnObject;
        }

        private static OraGetResponseCo SetHaziFeladatData(OraGetResponseCo oraResponseCo, TanarHaziFeladatDetailCO haziFeladatCo)
        {
            if (haziFeladatCo != null && haziFeladatCo.Id.HasValue && haziFeladatCo.Id > 0)
            {
                oraResponseCo.HazifeladatId = haziFeladatCo.Id;
                oraResponseCo.HazifeladatSzovege = haziFeladatCo.Szoveg?.Replace("<a", "<a target=\"_blank\"");
                oraResponseCo.HazifeladatHatarido = haziFeladatCo.Hatarido;
            }

            return oraResponseCo;
        }
    }
}