#!/usr/bin/env python
"""
Footprint PCB layouting program
Copyright (C) 2012 Danny Milosavljevic
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 3 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, see <http://www.gnu.org/licenses/>.
"""

from __future__ import division
import canvas
from footprints.fp import elementP, padP, elementLineP, elementArcP, pinP, attributeP, markP, getListItems, getListSize

def tomm(valueMils):
	return(valueMils*0.00254)

class Item(canvas.Item):
	def __init__(self, name, rect, fp = [], layer = 0):
		canvas.Item.__init__(self, name, rect, layer)
		self.shape = [s for s in self.prepareShape(fp)]
	def prepareShape(self, fp):
		if len(fp) < 1:
			return []
		Element0 = fp[0]
		assert(elementP(Element0[0]))
		Element0DirectAttrs = Element0[1]
		Element0Children = Element0[2]
		elementFlags, description, pcbName, value, markX, markY, textX, textY, textDirection, textScale, textFlags = getListItems(Element0DirectAttrs, [0,1,2,3,4,5,6,7,8,9,10,11])
		# Element
		#    Pad
		#    ElementLine
		#    ElementArc
		#    Pin
		#    Attribute
		#    Mark
		for child in Element0Children:
			if elementLineP(child):
				x0, y0, x1, y1, thickness = map(tomm, getListItems(child, [0,1,2,3,4]))
				yield ("line", x0, y0, x1, y1, thickness)
			elif padP(child):
				x0, y0, x1, y1, thickness = map(tomm, getListItems(child, [0,1,2,3,4]))
				rcount = getListSize() - 5
				clearance = 0
				if rcount == 5:
					clearance, mask = map(tomm, getListItems(child, [5,6])
					name, number, flags = getListItems(child, [7,8,9])
				elif rcount == 3:
					name, number, nflags = getListItems(child, [5,6,7])
				elif rcount == 2:
					name, nflags = getListItems(child, [5,6])
				yield ("rectangle", x0 - (thickness + clearance)/2, y0 - (thickness + clearance)/2, x1 + (thickness + clearance)/2, y1 + (thickness + clearance)/2, True)
				yield ("rectangle", x0 - thickness/2, y0 - thickness/2, x1 + thickness/2, y1 + thickness/2, True)
				# TODO maybe mask rectangle as well. x0 - mask/2, .....
			elif pinP(child):
				x, y, thickness = map(tomm, getListItems(child, [0,1,2])
				rcount = getListSize() - 3
				drill = 0
				if rcount == 6:
					clearance, mask, drill = map(tomm, getListItems(child, [3,4,5]))
					name, number, flags = getListItems(child, [6,7,8])
				elif rcount == 4:
					drill, name, number, flags = getListItems(child, [3,4,5,6])
					drill = tomm(drill)
				elif rcount == 3:
					drill, name, flags = getListItems(child, [3,4,5])
					drill = tomm(drill)
				elif rcount == 2:
					name, flags = getListItems(child, [3,4])
				yield ("circle", x, y, thickness + clearance/2, True) # outer
				# TODO mask
				yield ("circle", x, y, thickness, True) # inner
				if drill > 0:
					yield ("circle", x, y, drill, True)
			elif elementArcP(child): # kinda like Pin, but also can do ellipses.
				x, y, width, height, thickness = map(tomm, getListItems(child, [0,1,2,3,6]))
				startAngle, deltaAngle = getListItems(child, [4,5]) # 0..360, the latter can be negative for the other direction.
				bFilled = False # FIXME
				yield ("arc", x - width, y - height, x + width, y + height, thickness, startAngle % 360, (startAngle + deltaAngle) % 360, bFilled)
			#elif attributeP(child):
			#elif markP(child):
		return fp # FIXME coordinate transformations
	def repaint(self, area):
		return(self.shape)
