#!/usr/bin/env python
# -*- coding: utf-8 -*-
#*******************************************************************************#
#    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 Street, Fifth Floor, Boston, MA 02110-1301 USA.
#*******************************************************************************#

#  El idioma nativo del programador es el español, pero los comentarios  
#  estarán también en inglés 

import wx
import gettext
import os

if os.path.abspath(__file__).startswith('/opt'):
    gettext.install("leds", "/opt/extras.ubuntu.com/leds/share/locale")
else:
    gettext.install("leds")

class Info:
	# si hay futuros colaboradores, el separador será \n 
	# if there are future collaborators, the separator is \n 
	collaborators = "Abel Sendon\n"
	version = "2.120229"
	

class Led:
	color = ""
	def __init__(self, c):
		self.color = c
		self.voltage = 0  # led's voltage | voltaje del led
		self.power = 0    # power supplied | corriente sumistrada
		# consumo en amperes, salvo que se cambie la formula que calcula 
		# la resistencia, éste debería quedar en 0.05 (no es excesivo para el cálculo).
		# consumption in amps, unless you change the formula that calculates
		# resistance, it should be in 0.05 (not excessive for the calculation)
		self.consumption = 0.05   
		
		if c == "red":
			self.voltage = 1.8
		elif c == "green":
			self.voltage = 2.6  
		elif c == "blue":
			self.voltage = 3.2  
		elif c == "infrared":
			self.voltage = 1.9  
		elif c == "amber":
			self.voltage = 2.2  
		elif c == "white":
			self.voltage = 3.5  
		elif c == "yellow":
			self.voltage = 2.1  
		elif c == "ultraviolet":
			self.voltage = 3.1
		else: 
			self.voltage = 2.5
			 
	def setPower(self, p):
		self.power = p
		
	def calcResistance(self):
		r = ((self.power - self.voltage) / (self.power * self.consumption)) * 100 
		return r
	
	# Para las futuras veriones, leds en paralelo
	# For future versions, leds in parallel
	#def calcResistance(self, led1, led2):
	#   volts = led1 + led2 
	#	r = ((self.power - volts) / (self.power * self.consumption)) * 100 
	#	return r
		
	def info(self): 
	# Info adicional 
		msg = ""
		if self.color == "blue":
			msg = _("Information of blue led") + " \
			    \n==============================================\
				\n" + _("Possible compounds") + "  -  " + _("Wavelength") + " \
				\n-------------------------------------------------------------------------------------\
				\n" + _("Zinc selenide(ZnSe)") + "  -  " + _(" ? ") + " \
				\n-------------------------------------------------------------------------------------\
				\n" + _("Gallium nitride and Indio(InGaN)") + "  -  " + _("450 nm") + " \
				\n-------------------------------------------------------------------------------------\
				\n" + _("Silicon carbide(SiC)") + "  -  " + _("480 nm") + " \
				\n==============================================="
				
		elif self.color == "red": 
				msg = _("Information of red led") + " \
			    \n==============================================\
				\n" + _("Possible compounds") + "  -  " + _("Wavelength") + " \
				\n-------------------------------------------------------------------------------------\
				\n" + _("Gallium arsenide and aluminum(AlGaAs)") + "  -  " + _("890 nm") + " \
				\n-------------------------------------------------------------------------------------\
				\n" + _("Gallium arsenide phosphide(GaAsP)") + "  -  " + _("630 nm") + " \
				\n-------------------------------------------------------------------------------------\
				\n==============================================="
				
		elif self.color == "green":
				msg = _("Information of green led") + " \
			    \n==============================================\
				\n" + _("Possible compounds") + "  -  " + _("Wavelength") + " \
				\n-------------------------------------------------------------------------------------\
				\n" + _("Gallium phosphide (GaP)") + "  -  " + _("555 nm") + " \
				\n-------------------------------------------------------------------------------------\
				\n" + _("Gallium nitride (GaN)") + "  -  " + _("525 nm") + " \
				\n-------------------------------------------------------------------------------------\
				\n==============================================="
				
		elif self.color == "amber" or self.color == "yellow":
				msg = _("Information of amber led") + " \
			    \n==============================================\
				\n" + _("Possible compounds") + "  -  " + _("Wavelength") + " \
				\n-------------------------------------------------------------------------------------\
				\n" + _("Gallium arsenide phosphide (GaAsp)") + "  -  " + _("630 nm") + " \
				\n-------------------------------------------------------------------------------------\
				\n==============================================="
				
		elif self.color == "infrared":
				msg = _("Information of infrared led") + " \
			    \n==============================================\
				\n" + _("Possible compounds") + "  -  " + _("Wavelength") + " \
				\n-------------------------------------------------------------------------------------\
				\n" + _("Gallium arsenide (Gas)") + "  -  " + _("940 nm") + " \
				\n-------------------------------------------------------------------------------------\
				\n==============================================="
						
		elif self.color == "ultraviolet":
				msg = _("Information of ultraviolet led") + " \
			    \n==============================================\
				\n" + _("Possible compounds") + "  -  " + _("Wavelength") + " \
				\n-------------------------------------------------------------------------------------\
				\n" + _("Diamond(C)") + "  -  " + _(" ? ") + " \
				\n-------------------------------------------------------------------------------------\
				\n==============================================="
				
		elif self.color == "white":
				msg = _("Information of white led") + " \
			    \n==============================================\
				\n" + _("Possible compounds") + "  -  " + _("Wavelength") + " \
				\n-------------------------------------------------------------------------------------\
				\n" + _("Zinc selenide (ZnSe)") + "  -  " + _(" ? ") + " \
				\n-------------------------------------------------------------------------------------\
				\n==============================================="
		else:
			msg = "..."
		
		return msg
		
		

class FrmMain(wx.Frame):
    def __init__(self, *args, **kwds):
        kwds["style"] = wx.ICONIZE|wx.CAPTION|wx.MINIMIZE|wx.CLOSE_BOX|wx.MINIMIZE_BOX|wx.SYSTEM_MENU|wx.CLIP_CHILDREN
        wx.Frame.__init__(self, *args, **kwds)
        self.label_1 = wx.StaticText(self, -1, "  " + _("Color "))
        # Este es un bug, el combobox no selecciona ningún elemento cuando se usa con gettext
        # por el momento quedaría así
        # This is a bug, the combobox does not select anything when used with gettext
        # for now, would look like
        #self.cmbColor = wx.ComboBox(self, -1, choices=[_("red"), _("green"), _("blue"), _("amber"), _("yellow"), _("infrared"), _("ultraviolet"), _("white")], style=wx.CB_DROPDOWN)
        self.cmbColor = wx.ComboBox(self, -1, choices=["red", "green", "blue", "amber", "yellow", "infrared", "ultraviolet", "white"], style=wx.CB_DROPDOWN)
        self.label_2 = wx.StaticText(self, -1, "  " + _("Voltage"))
        self.txtVolts = wx.TextCtrl(self, -1, "")
        self.txtDisplay = wx.TextCtrl(self, -1, "", style=wx.TE_MULTILINE|wx.HSCROLL|wx.TE_READONLY)
        self.btnInfo = wx.Button(self, -1, _("Information"))
        self.btnCalc = wx.Button(self, -1, _("Calculate"))
        self.btnAbout = wx.Button(self, -1, _("About LEDs"))
        self.btnQuit = wx.Button(self, wx.ID_EXIT, "")

        self.__set_properties()
        self.__do_layout()

        self.Bind(wx.EVT_TEXT_ENTER, self.btnCalc_click, self.txtVolts)
        self.Bind(wx.EVT_BUTTON, self.btnInfo_click, self.btnInfo)
        self.Bind(wx.EVT_BUTTON, self.btnCalc_click, self.btnCalc)
        self.Bind(wx.EVT_BUTTON, self.btnAbout_click, self.btnAbout)
        self.Bind(wx.EVT_BUTTON, self.btnQuit_click, self.btnQuit)


    def __set_properties(self):
        self.SetTitle(_("LEDs"))
        _icon = wx.EmptyIcon()
        _icon.CopyFromBitmap(wx.Bitmap("/opt/extras.ubuntu.com/leds/icons/leds-32x32.png", wx.BITMAP_TYPE_ANY))
        self.SetIcon(_icon)
        self.SetSize((640, 480))
        self.label_1.SetMinSize((60, 15))
        self.cmbColor.SetMinSize((480, 25))
        self.cmbColor.SetSelection(-1)
        self.label_2.SetMinSize((60, 15))
        self.txtVolts.SetMinSize((480, 25))
        self.txtDisplay.SetMinSize((630, 290))

    def __do_layout(self):
        sizer_1 = wx.BoxSizer(wx.VERTICAL)
        sizer_4 = wx.BoxSizer(wx.HORIZONTAL)
        sizer_3 = wx.BoxSizer(wx.HORIZONTAL)
        sizer_3.Add(self.label_1, 0, 0, 0)
        sizer_3.Add(self.cmbColor, 0, 0, 0)
        sizer_1.Add(sizer_3, 1, wx.EXPAND, 0)
        sizer_4.Add(self.label_2, 0, 0, 0)
        sizer_4.Add(self.txtVolts, 0, 0, 0)
        sizer_1.Add(sizer_4, 1, wx.EXPAND, 0)
        sizer_1.Add(self.txtDisplay, 0, wx.ALL, 4)
        sizer_1.Add(self.btnInfo, 0, wx.EXPAND, 0)
        sizer_1.Add(self.btnCalc, 0, wx.EXPAND, 0)
        sizer_1.Add(self.btnAbout, 0, wx.EXPAND, 0)
        sizer_1.Add(self.btnQuit, 0, wx.EXPAND, 0)
        self.SetSizer(sizer_1)
        self.Layout()

    def btnCalc_click(self, event): 
    	self.txtDisplay.Clear()
    	color = self.cmbColor.GetValue()
    	if color == "":
    		self.txtDisplay.Clear()
    		self.txtDisplay.AppendText(_("Must select a led color"))
    		return
    	led = Led(color)
    	if self.txtVolts.GetValue() == "":
    		self.txtDisplay.AppendText(_("Must specify a voltage (1.8V or more)"))
    		return
    	led.setPower(float(self.txtVolts.GetValue()))
    	if led.calcResistance() < 0:
    		self.txtDisplay.AppendText(_("Don't need resistance"))
    		return
    	self.txtDisplay.AppendText(_("Resistance") + ": " + str(led.calcResistance()) + " ohms")
    	

    def btnInfo_click(self, event): 
    	self.txtDisplay.Clear()
    	color = self.cmbColor.GetValue()
    	if color == "":
    		self.txtDisplay.Clear()
    		self.txtDisplay.AppendText(_("Must select a led color"))
    		return
    	led = Led(color)
        self.txtDisplay.AppendText(led.info())


    def btnAbout_click(self, event): 
        d = dlgAbout(None, -1, "")
        d.Show()

    def btnQuit_click(self, event):
        exit()


class dlgAbout(wx.Dialog, Info):
    def __init__(self, *args, **kwds):
        kwds["style"] = wx.DEFAULT_DIALOG_STYLE
        wx.Dialog.__init__(self, *args, **kwds)
        self.text_ctrl_1 = wx.TextCtrl(self, -1, _("LEDs version " + self.version + " - Calculate the led's resistence \n2012(C) - Abel Sendon <abelnicolas1976@gmail.com> \n\nThis 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. \n\nThis 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. \n\nYou should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., \n51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. \n"), style=wx.TE_MULTILINE)
        self.btnCredits = wx.Button(self, -1, _("Credits"))
        self.button_2 = wx.Button(self, wx.ID_OK, "")
        self.__set_properties()
        self.__do_layout()
        self.Bind(wx.EVT_BUTTON, self.show_credits, self.btnCredits)
        
    def __set_properties(self):
        self.SetTitle(_("About"))
        self.text_ctrl_1.SetMinSize((400, 250))
        self.btnCredits.SetMinSize((200, 45))
        self.button_2.SetMinSize((200, 45))

    def show_credits(self,none):
      	self.text_ctrl_1.Clear()
      	self.text_ctrl_1.AppendText(self.collaborators)
       	
    def __do_layout(self):
        sizer_2 = wx.BoxSizer(wx.VERTICAL)
        sizer_5 = wx.BoxSizer(wx.HORIZONTAL)
        sizer_2.Add(self.text_ctrl_1, 0, wx.EXPAND, 0)
        sizer_5.Add(self.btnCredits, 0, 0, 0)
        sizer_5.Add(self.button_2, 0, 0, 0)
        sizer_2.Add(sizer_5, 1, wx.EXPAND, 0)
        self.SetSizer(sizer_2)
        sizer_2.Fit(self)
        self.Layout()


if __name__ == "__main__":
    #import gettext
    #gettext.install("leds")
    app = wx.PySimpleApp(0)
    wx.InitAllImageHandlers()
    Frm_Main = FrmMain(None, -1, "")
    app.SetTopWindow(Frm_Main)
    Frm_Main.Show()
    app.MainLoop()
