#!/usr/bin/env python2


# Copyright 2016 Luke Phillips (lukerazor@hotmail.com)
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
#along with this program; if not, write to the Free Software
#Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

# Extension dirs
# linux:~/.config/inkscape/extensions
# windows: D:\Program Files\Inkscape\share\extensions

import inkex
import simplestyle
import simplepath
from inkex import *
import csv
import fnmatch
import re
import os
import os.path
import lxml
from lxml import etree
from copy import deepcopy
import sys
import copy

CSNS = ""

NSS[u'cs'] = u'http://www.razorfoss.org/tuckboxextension/'
			
class PaperBase():
	def __init__(self, width, height):		
		self.Width = width
		self.Height = height	

class A5(PaperBase):
	def __init__(self):
		PaperBase.__init__(self, 150, 100)
		 
class InsertPaperTemplateEffect(inkex.Effect):
	def __init__(self):
		inkex.Effect.__init__(self)
		self.OptionParser.add_option('-p', '--papertype', action = 'store', type = 'string', dest = 'PaperType')
		self.OptionParser.add_option('-s', '--show_type', action = 'store', type = 'string', dest = 'ShowType')
		
		### colour ###
		self.StrokeWidthFloat = 0.5
		self.StrokeWidth = self.MMtoUU(self.StrokeWidthFloat)
		
		self.FontSize = "11px"
		
	def effect(self):
		self.Group = inkex.etree.SubElement(self.current_layer, inkex.addNS('g','svg'), {} )
		
		paperTypes = {}
		paperTypes["A5"] = (148, 210, "#ffeeaa") # yellow
		paperTypes["A4"] = (210, 297, "#ffccaa") # orange
		paperTypes["A3"] = (297, 420, "#afdde9") # blue
		paperTypes["A2"] = (420, 594, "#ccaaff") # purple
		paperTypes["A1"] = (594, 841, "#afe9c6") # green
		paperTypes["A0"] = (841, 1189, "#ffd5d5") # red
		
		paperTypes["POKER"] = (63.5, 88, "#ffffff") # white
		paperTypes["BRIDGE"] = (56, 88, "#ffffff") # white
		paperTypes["MINI_US"] = (41, 63, "#ffffff") # white
		paperTypes["MINI_EU"] = (44, 68, "#ffffff") # white
		paperTypes["TAROT"] = (70, 120, "#ffffff") # white
		

		if self.options.PaperType in paperTypes:
			self.CreateTemplate(self.options.PaperType, *(paperTypes[self.options.PaperType]))
		else:
			raise Exception("Paper type '{0}' is undefined".format(self.options.PaperType))		
	
	def CreateTemplate(self, label, width, height, colour):
		# TODO Read mouse position
		x = 0
		y = 0
		self._CreateRectangleInMillimetres(width-self.StrokeWidthFloat, height-self.StrokeWidthFloat, x, y, colour)
		if self.options.ShowType == "true":
			self._CreateText(label, x + width/2 , y + height/2)
		
	def _CreateText(self, labelText, x, y):
		style = {'stroke': '#000000', 
			'stroke-width': self.StrokeWidth, 
			'fill' : '#000000', 
			'font-size' : self.FontSize,
			'text-align' : 'center', 
			'text-anchor' : 'middle'
			}
		
		attribs = {'style': simplestyle.formatStyle(style), 'x': self.MMtoUU(x), 'y': self.MMtoUU(y)}
		
		text = inkex.etree.Element(inkex.addNS('text','svg'), attribs)
		text.text = labelText
		
		self.Group.append(text)
	
	def _CreateRectangleInMillimetres(self, width, height, x, y, color):
		style = {'stroke': '#000000', 'stroke-width': self.StrokeWidth, 'fill' : color}                
		attribs = {'style': simplestyle.formatStyle(style), 'height': self.MMtoUU(height), 'width': self.MMtoUU(width), 'x': self.MMtoUU(x), 'y': self.MMtoUU(y)}
		inkex.etree.SubElement(self.Group, inkex.addNS('rect','svg'), attribs )
		
	def MMtoUU(self, mmval):
		if hasattr(self, "xxxunittouu"):
			return str(self.unittouu("{0}mm".format(mmval)))
		else:
			MM_TO_PIXELS = 1
		
			return str(MM_TO_PIXELS * mmval)
	

if __name__ == '__main__':
	creator = InsertPaperTemplateEffect()
	
	creator.affect()
