using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using Kreta.Core.MessageBroker.Contract.MobileNotification;
using Kreta.Core.MessageBroker.Contract.MobileNotification.Enum;
using Kreta.DataAccessManual;
using Kreta.Job.Tasks.Entities;
using Kreta.MessageBroker.Client.MobileNotification;
using Kreta.Resources;
using Serilog;
using Serilog.Core.Enrichers;

namespace Kreta.Job.Tasks.Helpers.Notification
{
    internal static class RendszeruzenetNotificationHelper
    {
        internal static void SendRendszeruzenetNotification(string connectionString)
        {
            var logger = Log.ForContext(new PropertyEnricher[] { new PropertyEnricher("LoggerId", Guid.NewGuid()) });
            try
            {
                Dal.ServiceSystemConnection.Run(connectionString, h =>
            {
                var feljegyzesDal = h.Feljegyzes();
                var feljegyzesek = feljegyzesDal.GetAllSchemaRendszeruzenetFeljegyzesNotification();
                List<IdWithData> feljegyzesList = new List<IdWithData>();
                foreach (DataRow row in feljegyzesek.Tables[0].Rows)
                {
                    if (!int.TryParse(row["ID"] == DBNull.Value ? string.Empty : row["ID"].ToString(), out int feljegyzesId))
                    { continue; }
                    feljegyzesList.Add(new IdWithData(feljegyzesId, row));
                }

                if (feljegyzesList.Count > 0)
                {
                    feljegyzesDal.SetFeljegyzesAsKikuldott(feljegyzesList.Select(x => x.Id).Distinct());

                    var mobileNotificationMessages = new List<MobileNotificationMessage>();

                    feljegyzesList.ForEach(item => ProcessRendszeruzenetNotification(item.Data, item.Id, mobileNotificationMessages));

                    if (mobileNotificationMessages.Count > 0)
                    {
                        MobileNotificationMessageHelper.PostStudentNotification(mobileNotificationMessages.ToArray());
                    }
                }
            });
            }
            catch (Exception ex)
            {
                logger.Fatal(ex, ex.GetType().FullName);
            }
        }

        internal static void ProcessRendszeruzenetNotification(DataRow row, int feljegyzesId, List<MobileNotificationMessage> mobileNotificationMessages)
        {
            if (!int.TryParse(row["TanuloId"] == DBNull.Value ? string.Empty : row["TanuloId"].ToString(), out int tanuloId)) { return; }

            Guid.TryParse(row["IdpUniqueId"] == DBNull.Value ? string.Empty : row["IdpUniqueId"].ToString(), out Guid idpUniqueId);
            var instituteCode = row["IntezmenyAzonosito"] == DBNull.Value ? string.Empty : row["IntezmenyAzonosito"].ToString();
            var message = row["UzenetTargy"] == DBNull.Value ? string.Empty : row["UzenetTargy"].ToString();

            if (string.IsNullOrWhiteSpace(message))
            { return; }

            mobileNotificationMessages.Add(MobileNotificationMessageHelper.CreateMessage(
                instituteCode,
                tanuloId,
                MobileNotificationMessageType.Note,
                feljegyzesId,
                message,
                NotificationResource.RendszeruzenetTitle));
        }
    }
}