# -*- coding: utf-8 -*-
 
#    This file is part of CrossMess.
#
#    CrossMess 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.
#
#    CrossMess 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 CrossMess; if not, write to the Free Software Foundation, Inc., 51
#    Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 
 
import wx
import ChatWindow
import AddContact
 
class ContactListPanel(wx.Panel):
	"""
	Contact List Panel
	"""
	def __init__(self, Parent=None):
 
		wx.Panel.__init__(self, Parent)
 
		self.Sizer = wx.BoxSizer(wx.VERTICAL)
 
		self.SetSizer(self.Sizer, True)
 
		self.ContactList = wx.TreeCtrl(self, style=(wx.TR_HIDE_ROOT|wx.TR_HAS_BUTTONS))
 
		self.RootID = self.ContactList.AddRoot("Root")
 
		self.Groups = {}
		self.Contacts = {}
 
		self.BuildImageLists()
 
		self.ContactList.SetImageList(self.ImageList)
 
		self.GetSizer().Add(self.ContactList, 1, border=12, flag=wx.EXPAND|wx.BOTTOM)
 
		self.sizer2 = wx.BoxSizer(wx.HORIZONTAL)
 
		self.GetSizer().Add(self.sizer2, border=12, flag=wx.EXPAND|wx.BOTTOM)
 
		self.sizer3 = wx.BoxSizer(wx.VERTICAL)
 
		self.sizer2.Add(self.sizer3, 1, border=6, flag=wx.RIGHT|wx.EXPAND)
 
		self.Sizer4 = wx.BoxSizer(wx.HORIZONTAL)
 
		self.sizer3.Add(self.Sizer4, border=6, flag=wx.EXPAND|wx.ALIGN_TOP|wx.BOTTOM)
 
		MyNick = wx.StaticText(self,label="Meu apelido:")
 
		self.Sizer4.Add(MyNick, border=6, flag=wx.RIGHT|wx.ALIGN_CENTER_VERTICAL)
 
		self.UserName = wx.TextCtrl(self)
 
		self.Sizer4.Add(self.UserName, 1, flag=wx.EXPAND)
 
		StateChoices = ["Disponível", "Ocupado", "Ausente"]
		StateChoices += ["Volto logo", "Ao telefone"]
		StateChoices += ["Em horário de almoço", "Invisível"]
 
		self.StatusBox = wx.Choice(self, 1, choices=StateChoices)
 
		#self.StatusBox.SetSizeHints(200, -1)
 
		self.sizer3.Add(self.StatusBox, border=6, flag=wx.BOTTOM|wx.EXPAND, proportion=1)
 
		self.AddContact = wx.Button(self, id=wx.ID_ADD, label="Adicionar Contato")
 
		#self.AddContact.SetSizeHints(200, -1)
 
		self.sizer3.Add(self.AddContact, flag=wx.ALIGN_TOP|wx.EXPAND)
 
		img = wx.Image(self.Parent.Theme.GetImage("face"))
 
		#posx = 32 - img.GetWidth() / 2
		#posy = 32 - img.GetHeight() / 2
 
		img = img.Scale(96, 96, wx.IMAGE_QUALITY_HIGH)
 
		self.UserImage = wx.BitmapButton(self, bitmap=wx.BitmapFromImage(img))
 
		self.sizer2.Add(self.UserImage)
 
		self.ContactList.Bind(wx.EVT_LEFT_DCLICK, self.OnDblClick)
		self.AddContact.Bind(wx.EVT_BUTTON, self.OnAddClick)
 
	def BuildImageLists(self):
 
		self.ImageList = wx.ImageList(32, 32, initialCount=2)
		self.ImageListNames = []
 
		self.ImageListAdd("online")
		self.ImageListAdd("offline")
		self.ImageListAdd("blocked")
		self.ImageListAdd("group")
 
	def ImageListAdd(self, ImageName):
 
		imgpath = self.Parent.Theme.GetImage(ImageName)
 
		if imgpath == None:
			return 1
 
		img = wx.Image(imgpath)
 
		bmp = wx.BitmapFromImage(img)
 
		self.ImageList.Add(bmp)
		self.ImageListNames.append(ImageName.lower())
 
	def GetImageID(self, name, default=None):
		"""
		Return a image index by image name
		"""
 
		imgid = default
 
		if self.ImageListNames.index(name.lower()) > 0:
 
			imgid = self.ImageListNames.index(name.lower())
 
		return imgid
 
	def InsertContact(self, UserName, UserString="", UserImage=None, UserGroup=None, UserStatus=None):
 
		imgid = 0
 
		parentid = self.RootID
 
		if UserGroup != None:
			parentid = self.GetGroupIDByName(UserGroup)
 
			if parentid == None:
				parentid = self.RootID
 
		if UserStatus != None:
 
			imgid = self.GetImageID(UserStatus.lower(), 0)
 
		ItemId = self.ContactList.AppendItem(parentid, UserName, imgid, imgid)
 
		self.ContactList.SetItemData(ItemId, wx.TreeItemData(["contact"]))
 
	def InsertGroup(self, GroupName, GroupImage=None):
 
		# TODO: Check if group don't exist
 
		imgid = self.GetImageID("group", 0)
 
		if GroupImage != None:
			bmp = wx.BitmapFromImage(GroupImage).Scale(32, 32)
			imgid = self.ImageList.Add(bmp)
 
		ItemId = self.ContactList.AppendItem(self.RootID, GroupName, imgid, imgid)
 
		self.ContactList.SetItemData(ItemId, wx.TreeItemData(["group"]))
 
		self.Groups[GroupName] = [ItemId, {}]
 
	def GetGroupIDByName(self, GroupName):
 
		for group in self.Groups.keys():
 
			if group.lower() == GroupName.lower():
 
				return self.Groups[group][0]
 
		return None
 
	def OnDblClick(self, event):
 
		hit = self.ContactList.HitTest(event.GetPosition())
 
		itemflag = wx.TREE_HITTEST_ONITEM
		itemflag |=  wx.TREE_HITTEST_ONITEMBUTTON
		itemflag |= wx.TREE_HITTEST_ONITEMICON
		itemflag |= wx.TREE_HITTEST_ONITEMINDENT
		itemflag |= wx.TREE_HITTEST_ONITEMLABEL
		itemflag |= wx.TREE_HITTEST_ONITEMLOWERPART
		itemflag |= wx.TREE_HITTEST_ONITEMUPPERPART
		itemflag |= wx.TREE_HITTEST_ONITEMRIGHT
		itemflag |= wx.TREE_HITTEST_ONITEMSTATEICON
 
		if hit[1] & itemflag:
 
			data = self.ContactList.GetItemData(hit[0]).GetData()
 
			if data != None:
 
				if data[0] == "group":
					self.ContactList.Toggle(hit[0])
 
				elif data[0] == "contact":
					name = self.ContactList.GetItemText(hit[0])
 
					ChatWindow.ChatWindow(self.Parent, name)
 
	def OnAddClick(self, event):
 
		imgpath = self.Parent.Theme.GetImage("add-contact")
 
		w = AddContact.AddContactWindow(self.Parent, imgpath)
 
		w.Bind(w.EVT_ADD_CONTACT, self.OnAddContact)
 
	def OnAddContact(self, event):
 
		print "Adding contact %s" % event.ContactName