import html


def format_mail(template: str, event: dict, ishtml: bool) -> str:
    """
    Formats the email template according to lambda triggering event.

    :param template: Email template.
    :param event: Event which triggered Lambda. Contains form fields.
    :param ishtml: True if template is HTML. Linebreaks are changed accordingly.
    :return: Formatted email template.
    """
    header = "Someone filled the contact form"
    subtext = ""
    # uuid.uuid4().hex
    unsubscribe_key = "f4bd5dd85908487b904ea189fb81e753"  # Not actually applicable for Admin email ID
    keys = ['firstName', 'lastName', 'email', 'subject', 'message']
    for key in keys:
        if ishtml:
            value = html.escape(event[key]).replace('\n', '<br/>')
            subtext += "{}: {}<br>".format(key, value)
        else:
            subtext += "{}: {}\n".format(key, event[key]).replace('\n', '\r\n')
    template = template.replace('{{header}}', header)
    template = template.replace('{{subtext}}', subtext)
    template = template.replace('{{unsubscribe-key}}', unsubscribe_key)
    return template