#!/usr/bin/env python
# -*- coding: iso-8859-15 -*-

# Description: Abacum, programming language
# Copyright (C) 2007 by Rafael Treviño Menéndez
# Author: Rafael Treviño Menéndez <skasi.7@gmail.com>

# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU Library 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
# Library 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 Street, Fifth Floor, Boston, MA  02110-1301, USA.

import utils
import pygame

SLOT_SIZE = 18
FONT_SIZE = 20

class Object:

	font = None
	redOrbSurface = None
	greenOrbSurface = None

	def __init__ (self, name, pos = (0, 0)):
		if not Object.font:
			Object.font = pygame.font.Font (None, FONT_SIZE)
			Object.redOrbSurface = pygame.image.load ('images/redSlot.png').convert_alpha ()
			Object.greenOrbSurface = pygame.image.load ('images/greenSlot.png').convert_alpha ()

		self.name = name
		self.pos = pos
		self.clickPos = None
		self.surface = None
		self.rect = None
		self.nameSurface = None
		self.slotsIn = []
		self.slotsOut = []
		self.properties = []
	
	def compile (self):
		nameSize = Object.font.size (self.name)
		width = max (SLOT_SIZE * max (len (self.slotsIn), len (self.slotsOut)), nameSize[0] + 4, 2 * SLOT_SIZE + 2 + 2 * (FONT_SIZE + 1))
		height = 2 * SLOT_SIZE + 2 + (len (self.properties) + 1) * (FONT_SIZE + 1)
		
		self.nameSurface = utils.textRender (self.name, Object.font, (216, 133, 0), (228, 226, 19), pygame.Rect (0, 0, width - 4, FONT_SIZE))

		self.surface = pygame.Surface ((width, height))
		self.rect = self.surface.get_rect ()
		self.rect.topleft = self.pos
		self.render ()
	
	def render (self):
		utils.gradient (self.surface, (100, 100, 100), (200, 200, 200), pygame.Rect (0, 0, self.rect.width / 2, self.rect.height))
		utils.gradient (self.surface, (200, 200, 200), (100, 100, 100), pygame.Rect (self.rect.width / 2, 0, self.rect.width - self.rect.width / 2, self.rect.height))

		pygame.draw.line (self.surface, (220, 220, 220), (0, SLOT_SIZE), (self.rect.width, SLOT_SIZE))
		pygame.draw.line (self.surface, (220, 220, 220), (0, self.rect.height - SLOT_SIZE), (self.rect.width, self.rect.height - SLOT_SIZE))

		self.surface.blit (self.nameSurface, (2, SLOT_SIZE + 2))

		for name, options, a, b, c in self.properties:
			# Blue one
			# self.surface.blit (utils.textRender (name, Object.font, (0, 18, 200), (0, 200, 220), pygame.Rect (0, 0, self.rect.width - 4, FONT_SIZE)), (2, SLOT_SIZE + FONT_SIZE + 3))
			# Purple one
			self.surface.blit (utils.textRender (name, Object.font, (140, 0, 200), (220, 0, 204), pygame.Rect (0, 0, self.rect.width - 4, FONT_SIZE)), (2, SLOT_SIZE + FONT_SIZE + 3))

		for i, slot in enumerate (self.slotsIn):
			self.surface.blit (Object.greenOrbSurface, (1 + i * SLOT_SIZE, 1))

	def mouse (self, type, pos):
		if type == pygame.MOUSEMOTION:
			if self.clickPos:
				self.pos = utils.relativePos (pos, self.clickPos)
				self.rect.topleft = self.pos
		elif type == pygame.MOUSEBUTTONDOWN:
			self.clickPos = utils.relativePos (pos, self.rect.topleft)
			self.render ()
		elif type == pygame.MOUSEBUTTONUP:
			self.clickPos = None
			self.render ()
	
	def addSlotIn (self, name):
		self.slotsIn.append ((name, None, None))
	
	def addProperty (self, name, options):
		# name, options, selectedOptionSurface, namesurface, optionsNameSurface
		self.properties.append ((name, options, None, None, None))
	
	def addSlotOut (self, name):
		# name, on / off, namesurface
		self.slotsOut.append ((name, None, None))
	
	def update (self):
		return self.surface, self.pos
