#!/usr/bin/env python3

from __future__ import print_function
from wand.image import Image
from wand.drawing import Drawing
from wand.color import Color
from wand.display import display

class CardColor:
    def __init__(self, name, asset, hex_val):
        self.name = name
        self.hex_val = hex_val
        self.asset = asset


def main():
    colors = []
    colors.append(CardColor('red', 'red', '#ff2929'))
    colors.append(CardColor('green', 'green', '#00800e'))
    colors.append(CardColor('blue', 'blue', '#0062f8'))
    colors.append(CardColor('yellow', 'yellow', '#e2f800'))

    special_cards = []
    special_cards.append(CardColor('W', 'white', '#ffffff'))
    special_cards.append(CardColor('N', 'white', '#ffffff'))

    for color in colors:
        for i in range(1, 14):
            draw_card_face(color, i)

    for sign in special_cards:
            draw_card_face(sign, sign.name)          

def draw_card_face(color, symbol):
    with Image(filename='assets/wozard-card.png') as img:
        draw_symbol(img, symbol, color.hex_val, 120, 190)
        draw_symbol(img, symbol, color.hex_val, int(img.width - 120), 190)
        img.flip() # Make mirrored text
        draw_symbol(img, symbol, color.hex_val, 120, 190)
        draw_symbol(img, symbol, color.hex_val, int(img.width - 120), 190)
        draw_rune(img, color)
        draw_mask(img)
        if (symbol == "W"):
            img.negate(channel="rgb")
        draw_card_texture_overlay(img)
        draw_card_trim(img)
        save_img(img, color.asset, symbol)
        if (symbol == "W"):
            save_img(img, color.asset, 40)
        if (symbol == "N"):
            save_img(img, color.asset, 0)

def draw_symbol(img, symbol, hex_color, x, y):
    with Drawing() as draw:
        draw.font = 'DejaVu-Sans-Mono'
        draw.font_size = 180
        draw.font_weight = 1800
        draw.stroke_color = Color('black')
        draw.stroke_width = 5
        draw.text_alignment = 'center'
        draw.fill_color=hex_color
        draw.text(x, y, str(symbol))
        draw(img)
    return img

def draw_rune(img, color):
    with Image(filename='assets/{}.png'.format(color.asset)) as rune:
        rune.resize(220,244)
        rune.opaque_paint(target=Color("white"), fill=Color(color.hex_val), fuzz=0.66*img.quantum_range)
        img.composite(
                     rune, 
                     left=int((img.width - rune.width) / 2), 
                     top=int((img.height - rune.height) / 2))
        
    return img

def draw_card_trim(img):
    with Image(filename='assets/card-trim.png') as trim:
        img.composite(trim)
        
    return img

def draw_card_texture_overlay(img):
    with Image(filename='assets/card-texture-3.png') as texture:
        img.composite(texture)
        
    return img

def draw_mask(img):
    # Mask with a pink outline. This pink outline is this replaced 
    # with a transparency layer before being returned
    with Image(width=img.width, height=img.height, background=Color("#c300ff")) as mask:
        with Drawing() as ctx:
            ctx.fill_color = Color("black")
            ctx.rectangle(left=0,
                        top=0,
                        width=mask.width,
                        height=mask.height,
                        radius=mask.width * 0.075)
            ctx(mask)
    
        img.composite_channel('all_channels', mask, 'screen')
        img.opaque_paint(target=Color("#c300ff"), fill=Color("transparent"), fuzz=0.4*img.quantum_range)
    return img


def save_img(img, color, symbol):
    print('Created card {} {}'.format(color, symbol))
    sizes = ['50', '25']
    img.save(filename='output/card-{}-{}.png'.format(color, symbol))
    for size in sizes:
        img.resize(int(img.width / 2), int(img.height / 2))
        img.save(filename='output/card-{}-{}-{}.png'.format(color, symbol, size))
   


main()
