using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Tesses.YouTubeDownloader;
using Tesses.YouTubeDownloader.MailKit;

namespace tytdemail.Model;

public class SMTPConfig
{
    public string? Username {get;set;}
    public string? Password {get;set;}

    public string? SmtpServer {get;set;}

    public int SmtpPort {get;set;}=587;

    public string? FromEmail {get;set;}

    public string? ToEmail {get;set;}

    public MailKit.Security.SecureSocketOptions SecureOption {get;set;}=MailKit.Security.SecureSocketOptions.StartTls;

    public bool IgnoreCertError {get;set;}

    public static IDownloader? Create(IConfiguration configuration)
    {
        SMTPConfig? config;
        try{
         config=configuration.GetValue<SMTPConfig>("SMTP");
        }catch(Exception ex){
            _=ex;
            return null;
        }
        string? email=config.ToEmail;
        if(string.IsNullOrWhiteSpace(email)) email="tytd@tesses.net";
        if(!string.IsNullOrWhiteSpace(config.SmtpServer) && !string.IsNullOrWhiteSpace(config.Username) && !string.IsNullOrWhiteSpace(config.Password) && !string.IsNullOrWhiteSpace(config.FromEmail))
        {
            SMTPDownloader downloader = new SMTPDownloader(config.SmtpServer,config.FromEmail,email,config.Username,config.Password,config.SmtpPort,config.SecureOption);
            return downloader;
        }
        return null;
    }
}