using AyCode.Interfaces.Messages;
using SendGrid;
using SendGrid.Helpers.Mail;
using AyCode.Models.Enums;
using AyCode.Entities.Messages;
using AyCode.Models.Messages;
using TIAM.Database.DataLayers.Users;


namespace TIAMWebApp.Server.Services
{
    public class NoticeSenderService : IMessageSenderService
    {

        private readonly IConfiguration _configuration;
        public UserDal _userDal;

        public NoticeSenderService(IConfiguration configuration, UserDal userDal)
        {

            _configuration = configuration;
            _userDal = userDal;
        }
        public async Task<string> SendMessageAsync<TNotice>(TNotice message, int messageType) where TNotice : class, INoticeBase
        {
            string result = "";
            switch (messageType)
            {
                case (int)MessageTypesEnum.email:
                    if (message is EmailMessage emailMessage)
                    {
                        Console.WriteLine($"EmailMessage!!!");
                        // Access DerivedClass properties
                        var _subject = emailMessage.Subject;
                        result = await SendMailWithSendgrid(emailMessage);
                    }
                    else
                    {
                        // Access BaseClass properties
                        result = "Invalid message";
                    }

                    break;
                case (int)MessageTypesEnum.sms:
                    //await SendSmsWithTwilio(message.Message);
                    break;
                case (int)MessageTypesEnum.push:
                    //await SendPushWithFirebase(message.Message);
                    break;
                case (int)MessageTypesEnum.chat:
                //await SendChatWithSignalR(message.Message);
                default:
                    
                    break;
            }

            return result;

        }

        public async Task<string> SendMailWithSendgrid(EmailMessage message)
        {
            Console.WriteLine($"Sender: {message.SenderId}");
            Console.WriteLine($"Message: {message.Message}");
            //resolve user!!!
            var senderUser = _userDal.Context.Users.FirstOrDefault(x => x.Id == message.SenderId);
            var receiverUser = _userDal.Context.Users.FirstOrDefault(x => x.Id == message.ReceiverId);
            string apiKey = _configuration["SendGrid:Key"];
            var _client = new SendGridClient(apiKey);
            var _from = new EmailAddress("", "");
            if (message.SenderId == Guid.Empty)
            {
                _from = new EmailAddress("noreply@tiam.com", "TourIAm mailservice");
            }
            else
            {
                _from = new EmailAddress(senderUser.EmailAddress, senderUser.EmailAddress);
            }
            var _subject = message.Subject;
            var _to = new EmailAddress(receiverUser.EmailAddress, receiverUser.EmailAddress);
            var _plainTextContent = message.Message;
            var _htmlContent = message.HtmlContent;
            var _msg = MailHelper.CreateSingleEmail(_from, _to, message.Subject, _plainTextContent, _htmlContent);
            var response = await _client.SendEmailAsync(_msg).ConfigureAwait(false);
            return response.StatusCode.ToString();
        }

    }
}
