#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Base shape class for pyprototypr
"""
# lib
import copy
import json
import math
# third party
from reportlab.pdfgen import canvas as reportlab_canvas
from reportlab.lib.units import cm, inch
from reportlab.lib.pagesizes import (
    A6, A5, A4, A3, A2, A1, A0, LETTER, LEGAL, ELEVENSEVENTEEN,
    letter, legal, elevenSeventeen, B6, B5, B4, B3, B2, B0, landscape)
from reportlab.lib.utils import ImageReader
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.lib.enums import TA_LEFT, TA_RIGHT, TA_CENTER, TA_JUSTIFY
from reportlab.lib.colors import (
    aliceblue, antiquewhite, aqua, aquamarine, azure, beige, bisque, black,
    blanchedalmond, blue, blueviolet, brown, burlywood, cadetblue, chartreuse,
    chocolate, coral, cornflowerblue, cornsilk, crimson, cyan, darkblue,
    darkcyan, darkgoldenrod, darkgray, darkgrey, darkgreen, darkkhaki,
    darkmagenta, darkolivegreen, darkorange, darkorchid, darkred, darksalmon,
    darkseagreen, darkslateblue, darkslategray, darkslategrey, darkturquoise,
    darkviolet, deeppink, deepskyblue, dimgray, dimgrey, dodgerblue,
    floralwhite, forestgreen, fuchsia, gainsboro, ghostwhite, gold, goldenrod,
    gray, grey, green, greenyellow, honeydew, hotpink, indianred, indigo,
    ivory, khaki, lavender, lavenderblush, lawngreen, lemonchiffon, lightblue,
    lightcoral, lightcyan, lightgoldenrodyellow, lightgreen, lightgrey,
    lightpink, lightsalmon, lightseagreen, lightskyblue, lightslategray,
    lightslategrey, lightsteelblue, lightyellow, lime, limegreen, linen,
    magenta, maroon, mediumaquamarine, mediumblue, mediumorchid, mediumpurple,
    mediumseagreen, mediumslateblue, mediumspringgreen, mediumturquoise,
    mediumvioletred, midnightblue, mintcream, mistyrose, moccasin,
    navajowhite, navy, oldlace, olive, olivedrab, orange, orangered, orchid,
    palegoldenrod, palegreen, paleturquoise, palevioletred, papayawhip,
    peachpuff, peru, pink, plum, powderblue, purple, red, rosybrown,
    royalblue, saddlebrown, salmon, sandybrown, seagreen, seashell, sienna,
    silver, skyblue, slateblue, slategray, slategrey, snow, springgreen,
    steelblue, tan, teal, thistle, tomato, turquoise, violet, wheat, white,
    whitesmoke, yellow, yellowgreen, fidblue, fidred, fidlightblue,
    cornflower, firebrick)
# local
from pyprototypr.utils import tools

DEBUG = False
UNITS = {
    "cm": cm,
    "inch": inch
}
COLORS = {
    "aliceblue": aliceblue,
    "antiquewhite": antiquewhite,
    "aqua": aqua,
    "aquamarine": aquamarine,
    "azure": azure,
    "beige": beige,
    "bisque": bisque,
    "black": black,
    "blanchedalmond": blanchedalmond,
    "blue": blue,
    "blueviolet": blueviolet,
    "brown": brown,
    "burlywood": burlywood,
    "cadetblue": cadetblue,
    "chartreuse": chartreuse,
    "chocolate": chocolate,
    "coral": coral,
    "cornflower": cornflower,
    "cornflowerblue": cornflowerblue,
    "cornsilk": cornsilk,
    "crimson": crimson,
    "cyan": cyan,
    "darkblue": darkblue,
    "darkcyan": darkcyan,
    "darkgoldenrod": darkgoldenrod,
    "darkgray": darkgray,
    "darkgreen": darkgreen,
    "darkgrey": darkgrey,
    "darkkhaki": darkkhaki,
    "darkmagenta": darkmagenta,
    "darkolivegreen": darkolivegreen,
    "darkorange": darkorange,
    "darkorchid": darkorchid,
    "darkred": darkred,
    "darksalmon": darksalmon,
    "darkseagreen": darkseagreen,
    "darkslateblue": darkslateblue,
    "darkslategray": darkslategray,
    "darkslategrey": darkslategrey,
    "darkturquoise": darkturquoise,
    "darkviolet": darkviolet,
    "deeppink": deeppink,
    "deepskyblue": deepskyblue,
    "dimgray": dimgray,
    "dimgrey": dimgrey,
    "dodgerblue": dodgerblue,
    "fidblue": fidblue,
    "fidlightblue": fidlightblue,
    "fidred": fidred,
    "firebrick": firebrick,
    "floralwhite": floralwhite,
    "forestgreen": forestgreen,
    "fuchsia": fuchsia,
    "gainsboro": gainsboro,
    "ghostwhite": ghostwhite,
    "goldenrod": goldenrod,
    "gold": gold,
    "gray": gray,
    "green": green,
    "greenyellow": greenyellow,
    "grey": grey,
    "honeydew": honeydew,
    "hotpink": hotpink,
    "indianred": indianred,
    "indigo": indigo,
    "ivory": ivory,
    "khaki": khaki,
    "lavenderblush": lavenderblush,
    "lavender": lavender,
    "lawngreen": lawngreen,
    "lemonchiffon": lemonchiffon,
    "lightblue": lightblue,
    "lightcoral": lightcoral,
    "lightcyan": lightcyan,
    "lightgoldenrodyellow": lightgoldenrodyellow,
    "lightgreen": lightgreen,
    "lightgrey": lightgrey,
    "lightpink": lightpink,
    "lightsalmon": lightsalmon,
    "lightseagreen": lightseagreen,
    "lightskyblue": lightskyblue,
    "lightslategray": lightslategray,
    "lightslategrey": lightslategrey,
    "lightsteelblue": lightsteelblue,
    "lightyellow": lightyellow,
    "limegreen": limegreen,
    "lime": lime,
    "linen": linen,
    "magenta": magenta,
    "maroon": maroon,
    "mediumaquamarine": mediumaquamarine,
    "mediumblue": mediumblue,
    "mediumorchid": mediumorchid,
    "mediumpurple": mediumpurple,
    "mediumseagreen": mediumseagreen,
    "mediumslateblue": mediumslateblue,
    "mediumspringgreen": mediumspringgreen,
    "mediumturquoise": mediumturquoise,
    "mediumvioletred": mediumvioletred,
    "midnightblue": midnightblue,
    "mintcream": mintcream,
    "mistyrose": mistyrose,
    "moccasin": moccasin,
    "navajowhite": navajowhite,
    "navy": navy,
    "oldlace": oldlace,
    "olivedrab": olivedrab,
    "olive": olive,
    "orange": orange,
    "orangered": orangered,
    "orchid": orchid,
    "palegoldenrod": palegoldenrod,
    "palegreen": palegreen,
    "paleturquoise": paleturquoise,
    "palevioletred": palevioletred,
    "papayawhip": papayawhip,
    "peachpuff": peachpuff,
    "peru": peru,
    "pink": pink,
    "plum": plum,
    "powderblue": powderblue,
    "purple": purple,
    "red": red,
    "rosybrown": rosybrown,
    "royalblue": royalblue,
    "saddlebrown": saddlebrown,
    "salmon": salmon,
    "sandybrown": sandybrown,
    "seagreen": seagreen,
    "seashell": seashell,
    "sienna": sienna,
    "silver": silver,
    "skyblue": skyblue,
    "slateblue": slateblue,
    "slategray": slategray,
    "slategrey": slategrey,
    "snow": snow,
    "springgreen": springgreen,
    "steelblue": steelblue,
    "tan": tan,
    "teal": teal,
    "thistle": thistle,
    "tomato": tomato,
    "turquoise": turquoise,
    "violet": violet,
    "wheat": wheat,
    "whitesmoke": whitesmoke,
    "white": white,
    "yellowgreen": yellowgreen,
    "yellow": yellow,
}
PAGES = {
    "LETTER": LETTER,
    "landscape": landscape,
    "legal": legal,
    "A1": A1,
    "A0": A0,
    "A3": A3,
    "A2": A2,
    "A5": A5,
    "A4": A4,
    "A6": A6,
    "elevenSeventeen": elevenSeventeen,
    "LEGAL": LEGAL,
    "letter": letter,
    "B4": B4,
    "B5": B5,
    "B6": B6,
    "B0": B0,
    "B2": B2,
    "B3": B3,
    "ELEVENSEVENTEEN": ELEVENSEVENTEEN,
}
WIDTH = 0.1


class BaseCanvas:
    """Wrapper/extended class for a ReportLab canvas."""

    def __init__(self, filename=None, pagesize=None, **kwargs):
        self.canvas = reportlab_canvas.Canvas(
            filename=filename, pagesize=pagesize or A4)
        self.jsonfile = kwargs.get('defaults', None)
        self.defaults = {}
        # override
        if self.jsonfile:
            try:
                with open(self.jsonfile) as data_file:
                    self.defaults = json.load(data_file)
            except IOError: