import os
import boto3
from botocore.exceptions import ClientError

# Replace sender@example.com with your "From" address.
# This address must be verified with Amazon SES.
SENDER = os.environ.get('SENDER')

# Replace recipient@example.com with a "To" address. If your account
# is still in the sandbox, this address must be verified.
RECIPIENT = os.environ.get('RECIPIENT')

# Specify a configuration set. If you do not want to use a configuration
# set, comment the following variable, and the
# ConfigurationSetName=CONFIGURATION_SET argument below.
CONFIGURATION_SET = "ConfigSet"

# If necessary, replace us-west-2 with the AWS Region you're using for Amazon SES.
AWS_REGION = os.environ.get('AWS_REGION', "us-east-1")

# The subject line for the email.
SUBJECT = "ethermine worker issue! worker name: {}"

# The email body for recipients with non-HTML email clients.
BODY_TEXT = """There was an issue with worker {}. It either timed out or has
            atypically low hashrate. Please restart or look into this worker."""

# The HTML body of the email.
BODY_HTML = """<html>
<head></head>
<body>
  <h1>ethermine worker down<h1>
  <p>{}</p>
</body>
</html>"""

# The character encoding for the email.
CHARSET = "UTF-8"

# Create a new SES resource and specify a region.
client = boto3.client('ses', region_name=AWS_REGION)


def send_email(worker_name):
    # Try to send the email.
    try:
        # Provide the contents of the email.
        response = client.send_email(
            Destination={
                'ToAddresses': [
                    RECIPIENT,
                ],
            },
            Message={
                'Body': {
                    'Html': {
                        'Charset': CHARSET,
                        'Data': BODY_HTML.format(worker_name),
                    },
                    'Text': {
                        'Charset': CHARSET,
                        'Data': BODY_TEXT.format(worker_name),
                    },
                },
                'Subject': {
                    'Charset': CHARSET,
                    'Data': SUBJECT.format(worker_name),
                },
            },
            Source=SENDER,
            # If you are not using a configuration set, comment or delete the
            # following line
            ConfigurationSetName=CONFIGURATION_SET,
        )
    # Display an error if something goes wrong.
    except ClientError as e:
        print(e.response['Error']['Message'])
    else:
        print("Email sent! Message ID:"),
        print(response['ResponseMetadata']['RequestId'])
