using System.Data;
using System.Threading.Tasks;
using IWS5.Core.Helpers;
using IWS5.Core.Models;
using IWS5.Core.Models.TriggerAction;
using IWS5.Server.Utils;
using Microsoft.Data.SqlClient;
using Newtonsoft.Json;
using PDServer.Helpers.TriggerProcessors;

namespace IWS5.Server.Helpers.TriggerProcessors;

public class EmailTriggerProcessor(IMessageProcessor messageProcessor) : ITriggerProcessor
{
    public async Task<bool> ProcessTrigger(SqlConnection sqlConnection, string triggerAction, int workItemId)
    {
        return await ProcessTrigger(sqlConnection, triggerAction, workItemId, string.Empty, null, null, string.Empty, null);
    }
    
    public async Task<bool> ProcessTrigger(SqlConnection sqlConnection, string triggerAction, int workItemId, string sourceUser = "", int? sourceQueueId = null, int? eventId = null, string lastAction = "", int? knownQueueId = null)
    {
        var mailTriggerActionData = JsonConvert.DeserializeObject<EMailTriggerAction>(triggerAction);
        var mailTask = new MailTask
        {
            Enabled = true,
            ServerId = mailTriggerActionData.ServerId,
            From = mailTriggerActionData.From,
            CC = mailTriggerActionData.CC,
            BCC = mailTriggerActionData.BCC,
            ReplyTo = mailTriggerActionData.ReplyTo,
            Subject = mailTriggerActionData.Subject,
            Recipients = mailTriggerActionData.Recipients,
            Message = mailTriggerActionData.Message,
            Priority = mailTriggerActionData.Priority
        };

        if (sqlConnection.State != ConnectionState.Open)
            sqlConnection.Open();

        mailTask.Subject = await messageProcessor.ProcessMessage(new MessageModel { 
            Message = mailTask.Subject,
            WorkItemId = workItemId,
            SourceUser = sourceUser,
            SourceQueueId = sourceQueueId ?? 0,
            EventId = eventId ?? 0,
            LastAction = lastAction,
            KnownQueueId = knownQueueId
        });
        mailTask.Message = await messageProcessor.ProcessMessage(new MessageModel
        {
            Message = mailTask.Message,
            WorkItemId = workItemId,
            SourceUser = sourceUser,
            SourceQueueId = sourceQueueId ?? 0,
            EventId = eventId ?? 0,
            LastAction = lastAction,
            KnownQueueId = knownQueueId
        });

        const string sqlStr = "SELECT ConnectionString, UserName, Password, DefaultFrom FROM ATConnection WHERE Id = @serverId";

        using var dap3 = new SqlDataAdapter();
        dap3.SelectCommand = new SqlCommand(sqlStr, sqlConnection);
        dap3.SelectCommand.Parameters.AddWithValue("@serverId", mailTask.ServerId);

        var ds3 = new DataSet();
        dap3.Fill(ds3);

        if (ds3.Tables[0].Rows.Count == 0)
        {
            return false;
        }

        string host = "";
        string userName = "";
        string password = "";

        foreach (DataRow tr in ds3.Tables[0].Rows)
        {
            host = tr[0].ToString();
            userName = tr[1].ToString();
            password = tr[2].ToString();
        }

        return MailSender.SendEmail(mailTask, host, userName, password);
    }
}