import objc
from Foundation import *
from AppKit import *
from PyObjCTools import NibClassBuilder, AppHelper
from mythTags import *


NibClassBuilder.extractClasses("MainMenu")


# class defined in MainMenu.nib
class MonsterController(NibClassBuilder.AutoBaseClass):
	# the actual base class is NSObject
	# The following outlets are added to the class:
	# MonsterRadio
	# MonsterTextField
	# MonsterLocalCheckbox
	# ProjectilePopup
	# ProjectileRadio
	# ObjectLocalCheckbox
	# ObjectRadio
	# theOutput
	# ProjectileLocalCheckbox
	# progressSpinner
	
	def monsterChooseRadio_(self, sender):
		theCell=sender.selectedCell()
		theTag=theCell.tag()
		if theTag==0:
			self.MonsterPopup.setHidden_(True)
			self.MonsterLocalCheckbox.setHidden_(False)
		else: 
			self.MonsterPopup.setHidden_(False)
			self.MonsterLocalCheckbox.setHidden_(True)
			
	def monsterGoButton_(self, sender):
		if self.newLocal.didError==False:
			if self.MonsterLocalCheckbox.state()==1:
				localOnly=True
			else:
				localOnly=False
			if self.MonsterRadio.cellAtRow_column_(0,0).state()==1:
				stringList=self.mTags.findAllDeadMonsters(self.newLocal, localOnly)
				tagString= NSMutableAttributedString.alloc().init()
				for tmpData in stringList:
					tagString.appendAttributedString_(self.createColoredText(tmpData[0], tmpData[1]))
				self.theOutput.textStorage().setAttributedString_(tagString)
			elif self.MonsterRadio.cellAtRow_column_(1,0).state()==1:
				stringList=self.mTags.findMonsByName(self.monsList[self.MonsterPopup.indexOfSelectedItem()],self.newLocal)
				tagString= NSMutableAttributedString.alloc().init()
				for tmpData in stringList:
					tagString.appendAttributedString_(self.createColoredText(tmpData[0], tmpData[1]))
				self.theOutput.textStorage().setAttributedString_(tagString)
			elif self.MonsterRadio.cellAtRow_column_(2,0).state()==1:
				stringList=self.mTags.findMonsByTag(str(self.MonsterTextField.stringValue()).rstrip(),self.newLocal)
				tagString= NSMutableAttributedString.alloc().init()
				for tmpData in stringList:
					tagString.appendAttributedString_(self.createColoredText(tmpData[0], tmpData[1]))
				self.theOutput.textStorage().setAttributedString_(tagString)
			else:
				self.theOutput.textStorage().setAttributedString_(self.createColoredText("You broke something noob!~", 0))
		else:
			self.theOutput.textStorage().setAttributedString_(self.createColoredText(self.newLocal.ErrorMessage,0))
	
	
	def projectileChooseRadio_(self, sender):
		theCell=sender.selectedCell()
		theTag=theCell.tag()
		if theTag==0:
			self.ProjectilePopup.setHidden_(False)
			self.ProjectilePopup.removeAllItems()
			self.ProjectilePopup.addItemsWithTitles_(self.PDList)
		elif theTag==1: 
			self.ProjectilePopup.setHidden_(False)
			self.ProjectilePopup.removeAllItems()
			self.ProjectilePopup.addItemsWithTitles_(self.InertiaList)
		elif theTag==2: 
			self.ProjectilePopup.setHidden_(False)
			self.ProjectilePopup.removeAllItems()
			self.ProjectilePopup.addItemsWithTitles_(self.ClassList)
		elif theTag==3: 
			self.ProjectilePopup.setHidden_(True)
		elif theTag==4: 
			self.ProjectilePopup.setHidden_(True)

	def projectileGoButton_(self, sender):
		self.progressSpinner.startAnimation_(sender)
		if self.newLocal.didError==False:
			if self.ProjectileLocalCheckbox.state()==1:
				localOnly=True
			else:
				localOnly=False
			if self.ProjectileRadio.cellAtRow_column_(0,0).state()==1:
				stringList=self.mTags.listByDamageType(self.ProjectilePopup.indexOfSelectedItem(), self.PDList, self.newLocal,localOnly)
				tagString= NSMutableAttributedString.alloc().init()
				for tmpData in stringList:
					tagString.appendAttributedString_(self.createColoredText(tmpData[0], tmpData[1]))
				self.theOutput.textStorage().setAttributedString_(tagString)
			elif self.ProjectileRadio.cellAtRow_column_(1,0).state()==1:
				stringList=self.mTags.listByInertiaType(self.ProjectilePopup.indexOfSelectedItem(), self.InertiaList, self.newLocal,localOnly)
				tagString= NSMutableAttributedString.alloc().init()
				for tmpData in stringList:
					tagString.appendAttributedString_(self.createColoredText(tmpData[0], tmpData[1]))
				self.theOutput.textStorage().setAttributedString_(tagString)
			elif self.ProjectileRadio.cellAtRow_column_(2,0).state()==1:
				stringList=self.mTags.listByClassType(self.ProjectilePopup.indexOfSelectedItem(), self.ClassList, self.newLocal,localOnly)
				tagString= NSMutableAttributedString.alloc().init()
				for tmpData in stringList:
					tagString.appendAttributedString_(self.createColoredText(tmpData[0], tmpData[1]))
				self.theOutput.textStorage().setAttributedString_(tagString)
			elif self.ProjectileRadio.cellAtRow_column_(3,0).state()==1:
				stringList=self.mTags.PrintPRGRandPROJ(self.newLocal,localOnly)
				tagString= NSMutableAttributedString.alloc().init()
				for tmpData in stringList:
					tagString.appendAttributedString_(self.createColoredText(tmpData[0], tmpData[1]))
				self.theOutput.textStorage().setAttributedString_(tagString)
			elif self.ProjectileRadio.cellAtRow_column_(4,0).state()==1:
				stringList=self.mTags.listUnusedProjectiles(self.newLocal,localOnly)
				tagString= NSMutableAttributedString.alloc().init()
				for tmpData in stringList:
					tagString.appendAttributedString_(self.createColoredText(tmpData[0], tmpData[1]))
				self.theOutput.textStorage().setAttributedString_(tagString)
		else:
			self.theOutput.textStorage().setAttributedString_(self.createColoredText(self.newLocal.ErrorMessage,0))
		self.progressSpinner.stopAnimation_(sender)
		
		

	def objectGoButton_(self, sender):
		self.progressSpinner.startAnimation_(sender)
		if self.newLocal.didError==False:
			if self.ObjectLocalCheckbox.state()==1:
				localOnly=True
			else:
				localOnly=False
			if self.ObjectRadio.cellAtRow_column_(0,0).state()==1:
				stringList=self.mTags.findDuplicateObjects(self.newLocal,localOnly)
				tagString= NSMutableAttributedString.alloc().init()
				for tmpData in stringList:
					tagString.appendAttributedString_(self.createColoredText(tmpData[0], tmpData[1]))
				self.theOutput.textStorage().setAttributedString_(tagString)
		else:
			self.theOutput.textStorage().setAttributedString_(self.createColoredText(self.newLocal.ErrorMessage,0))
		self.progressSpinner.stopAnimation_(sender)
		
		
	def unitGoButton_(self, sender):
		self.progressSpinner.startAnimation_(sender)
		tmpList=self.newLocal.getTagList('unit', False)
		for unit in tmpList:
			if unit.getUNITName()==self.unitList[self.UnitPopup.indexOfSelectedItem()]:
				break
				
		tagString= NSMutableAttributedString.alloc().init()
		tagString.appendAttributedString_(self.createColoredText("Exporting " + unit.tagHeader.name + "\n", 3))
		self.theOutput.textStorage().setAttributedString_(tagString)
		
		self.mTags.ExportLinkedTags(self.newLocal, unit)
		
		tagString.appendAttributedString_(self.createColoredText("Exporting Successful\n", 3))
		self.theOutput.textStorage().appendAttributedString_(tagString)
		self.progressSpinner.stopAnimation_(sender)
			
	
	def awakeFromNib(self):
		self.PDList=["None", "Explosive", "Magic", "Holding", "Healing", "Kenetic", "Slashing", "Stoning", "Slash Claws", "Explosive Electricity", "Fire", "Gas", "Charm"]
		self.InertiaList=["None", "Unwieldy", "Small", "Large"]
		self.ClassList=["None", "Unknown", "Head", "Arm", "Leg", "Torso", "Bloody Chunk", "Lethal Object", "Nonlethal Object", "Explosive Object", "Potentially Explosive Object", "Item"]

		self.ProjectilePopup.removeAllItems()
		self.ProjectilePopup.addItemsWithTitles_(self.PDList)
		
		self.newLocal=localInfo("../../../")
		self.refreshTags()
	
	
	
	def refreshTags(self):
		self.mTags=mythTags()
		self.newLocal.loadTags()
		
		tmpList=self.newLocal.getTagList('mons', False)
		self.monsList=[]
		for mons in tmpList:
			self.monsList.append(mons.getmName())
		self.monsList.sort()
		self.MonsterPopup.removeAllItems()
		self.MonsterPopup.addItemsWithTitles_(self.monsList)
		
		tmpList=self.newLocal.getTagList('unit', False)
		self.unitList=[]
		for unit in tmpList:
			self.unitList.append(unit.getUNITName())
		self.unitList.sort()
		self.UnitPopup.removeAllItems()
		self.UnitPopup.addItemsWithTitles_(self.unitList)
	
	def progressSpinner_(self, sender):
		self.progressSpinner.startAnimation_(sender)
		self.theOutput.textStorage().setAttributedString_(self.createColoredText("Updating tag list please wait...",0))
		self.theOutput.display()
		
		self.refreshTags()
		
		self.theOutput.textStorage().appendAttributedString_(self.createColoredText("\nTag list updated.",0))
		self.progressSpinner.stopAnimation_(sender)
		
	def createColoredText(self, theText, theTagType):
		if theTagType==1:
			myDictionary = NSDictionary.dictionaryWithObject_forKey_(NSColor.colorWithCalibratedRed_green_blue_alpha_(0.0,0.5,0.0,1.0), NSForegroundColorAttributeName)
		elif theTagType==2:
			myDictionary = NSDictionary.dictionaryWithObject_forKey_(NSColor.redColor(), NSForegroundColorAttributeName)
		else:
			myDictionary = NSDictionary.dictionaryWithObject_forKey_(NSColor.blackColor(), NSForegroundColorAttributeName)
		attString=NSAttributedString.alloc().initWithString_attributes_(theText, myDictionary)
		return attString		
		

if __name__ == "__main__":
    AppHelper.runEventLoop()
