from PIL import Image, ImageDraw, ImageFont
import unicodedata
import shutil

# Templates directory
dir = "templates/"

def normalize_name(name):
    # Normalize the name and replace spaces with underscores
    normalized_name = unicodedata.normalize('NFKD', name).encode('ASCII', 'ignore').decode('utf-8')
    return normalized_name.replace(" ", "_").lower()

def normalize_phone_number(phone_number):
    # Replace '+' with '00' and remove spaces
    normalized_number = phone_number.replace('+', '00').replace(' ', '')

    return normalized_number

def add_text_to_image(image_path, text, position, font_path, font_size, text_color, output_filename):
    # Open the existing image
    image = Image.open(image_path).convert("RGBA")

    # Create a drawing object
    draw = ImageDraw.Draw(image)

    # Load the custom font
    font = ImageFont.truetype(font_path, font_size)

    # Add text to the image with the specified color
    draw.text(position, text, font=font, fill=text_color)

    # Save the result
    image.save(output_filename, "PNG")

def generate_email_signature_template(name, phone, email, normalized_name):
    # Read the template from the existing HTML file
    with open("email_signature_template.html", "r", encoding="utf-8") as template_file:
        template = template_file.read()

    # Replace placeholders with actual data
    template = template.replace('@@name@@', name)
    template = template.replace('@@title@@', title)
    template = template.replace('@@normalized_name@@', normalized_name)
    template = template.replace('@@tel_normal@@', phone)
    template = template.replace('@@email@@', email)
    template = template.replace('@@tel_normalized@@', normalize_phone_number(phone))

    # Save the result to a file
    with open(dir + f"{normalized_name}_template.html", "w", encoding="utf-8") as file:
        file.write(template)

# Example usage
name = input("Enter name: ")
title = input("Enter title: ")
phone = input("Enter phone number: ")
email = input("Enter email: ")

# Normalize the name
normalized_name = normalize_name(name)

# Define color
color = "#828282"

# Assuming you have name_and_title.png, phone.png, and email.png files
add_text_to_image("name_and_title.png", f"{name}", (50, 40), "Saira-Bold.ttf", 37, color, dir + f"{normalized_name}_name_and_title.png")

# Using a different font for the title
add_text_to_image(dir + f"{normalized_name}_name_and_title.png", title, (50, 110), "Saira-Light.ttf", 30, color, dir + f"{normalized_name}_name_and_title.png")

add_text_to_image("phone.png", phone, (115, 25), "Saira-Regular.ttf", 25, color, dir + f"{normalized_name}_tel.png")
add_text_to_image("email.png", email, (115, 15), "Saira-Regular.ttf", 25, color, dir + f"{normalized_name}_email.png")

# Generate and save the email signature template
generate_email_signature_template(name, phone, email, normalized_name)
