from config import Config

from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from smtplib import SMTP_SSL
import ssl

class SendMail(object):
    def __init__(self):
        # SMTP credentials
        self.__server = Config.MAIL_SERVER
        self.__port = Config.MAIL_PORT
        self.__user = Config.MAIL_USER
        self.__password = Config.MAIL_PASSWORD
        self.__default_sender = Config.MAIL_SENDER
        self.__default_recipients = Config.MAIL_RECIPIENTS

        # SSL context
        self.__default_ssl_context = ssl.create_default_context()

        # Setup SMTP client
        self.__smtp_client = SMTP_SSL(host=self.__server,port=self.__port,)
        self.__smtp_client.login(self.__user,self.__password)

        # Set default message
        self.set_message_subject()
        self.set_message()
    
    def set_message(self, msg: str = '<h1>Test email message.</h1>'):
        self.__message_root = MIMEMultipart()
        self.__message_alt = MIMEMultipart('alternative')

        self.__message_root['Subject'] = self.__message_subject
        self.__message_root['From'] = self.__default_sender
        self.__message_root['To'] = ', '.join(self.__default_recipients)

        self.__message_root.attach(self.__message_alt)
        self.__message_alt.attach(MIMEText(msg,'html'))

        return True

    def set_message_subject(self, subject: str = 'allpawcare.com test email'):
        self.__message_subject = subject

        return True

    def send(self):
        self.__smtp_client.sendmail(self.__default_sender,self.__default_recipients,
                self.__message_root.as_string())

        return True

    def send_meet_and_greet_request(self, client_name: str = 'Andrew',
                    client_pets: str = 'Nabooru', visit_type: str = 'Test',
                    meeting_date: str = 'Test',
                    meeting_time: str = '10:00',
                    contact_type: str = 'Test',
                    client_contact: str = 'Test',
                    client_notes: str = 'This is just a test.'):
        self.set_message_subject('New meet and greet request')
        self.set_message('''
            <html>
                <h1>A new meet and greet request</h1>

                <h2>
                    {name} has requested a meet and greet.
                </h2>

                <ul>
                    <li>
                        <em>Meeting info:</em> {date} {time}
                    </li>

                    <li>
                        <em>Visit type:</em> {vtype}
                    </li>

                    <li>
                        <em>Pet(s):</em> {pets}
                    </li>

                    <li>
                        <em>{ctype}:</em> {contact}
                    </li>

                    <li>
                        <em>Client notes:</em> {notes}
                    </li>
                </ul>
            </html>
        '''.format(name = client_name,
                pets = client_pets, time = meeting_time,
                date = meeting_date, vtype = visit_type,
                ctype = contact_type, contact = client_contact,
                notes = client_notes))

        self.send()

        return True

