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 windowDidLoad(self):
        
    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 objectChooseRadio_(self, sender):
        theCell=sender.selectedCell()
        theTag=theCell.tag()
        if theTag==0:
            self.ObjectLocalCheckbox.setHidden_(False)
        elif theTag==1:
            self.ObjectLocalCheckbox.setHidden_(True)
            self.printToOutput("Hit Go to find which tags in your local folder are not being preloaded into your meshes \n\n(You must have created at least one preloaded data tag by holding the spacebar while starting your mesh.)")

    def objectGoButton_(self, sender):        
        self.progressSpinner.startAnimation_(sender)
        if self.newLocal.didError==False:
            if self.ObjectRadio.cellAtRow_column_(1,0).state()==1:
                unusedTagList=self.mTags.findUnusedTags(self.newLocal, True)
                tagString= NSMutableAttributedString.alloc().init()
                for tmpTag in unusedTagList:
                    tagString.appendAttributedString_(self.createColoredText(tmpTag.tagHeader.getGroup_Tag() + " (" + tmpTag.tagHeader.getSubgroup_Tag() + ") " + tmpTag.tagHeader.getName() + "\n", tmpTag.tagHeader.getTagType()))
                self.theOutput.textStorage().setAttributedString_(tagString)
            elif self.ObjectRadio.cellAtRow_column_(0,0).state()==1:
                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 unitChooseRadio_(self, sender):
        theCell=sender.selectedCell()
        theTag=theCell.tag()
        if theTag==0:        
            tagType="unit"
        elif theTag==1: 
            tagType="arti"
        elif theTag==2: 
            tagType="scen"
        tmpList=self.newLocal.getTagList(tagType, True)
        self.extractList=[]
        for eTag in tmpList:
            self.extractList.append(eTag.getTagName())
        self.extractList.sort()
        self.UnitPopup.removeAllItems()
        self.UnitPopup.addItemsWithTitles_(self.extractList)
        
    def unitGoButton_(self, sender):
        self.progressSpinner.startAnimation_(sender)
        tagString= NSMutableAttributedString.alloc().init()
        tagString.appendAttributedString_(self.createColoredText("Gathering Related Tags\n", 3))
        self.theOutput.textStorage().setAttributedString_(tagString)
        if self.UnitRadio.cellAtRow_column_(0,0).state()==1:
            tagType="unit"
        elif self.UnitRadio.cellAtRow_column_(1,0).state()==1:
            tagType="arti"
        elif self.UnitRadio.cellAtRow_column_(2,0).state()==1:
            tagType="scen"
        tmpList=self.newLocal.getTagList(tagType, False)
        for tTag in tmpList:
            if tTag.getTagName()==self.extractList[self.UnitPopup.indexOfSelectedItem()]:
                break
        self.printToOutput("Exporting " + tagType + ": " + tTag.getTagName(), 3)
        self.shouldAppend=True
        #tagString.appendAttributedString_(self.createColoredText())
        #self.theOutput.textStorage().setAttributedString_(tagString)
        #self.theOutput.display()
        self.mTags.ExportLinkedTags(self.newLocal, tTag)
        
        self.printToOutput("Exporting Successful", 3)
        #tagString.appendAttributedString_(self.createColoredText())
        #self.theOutput.textStorage().setAttributedString_(tagString)
        self.shouldAppend=False
        self.progressSpinner.stopAnimation_(sender)
            
    
    def awakeFromNib(self):
        self.progressBar.setUsesThreadedAnimation_(True)
        self.progressBar.startAnimation_(self.LoadWindow)
        self.LoadWindow.makeKeyAndOrderFront_(self.LoadWindow)
        
        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(self.printToDialog)
        self.shouldAppend=False
        
        self.LoadWindow.orderOut_(self.LoadWindow)
        self.progressBar.stopAnimation_(self.LoadWindow)
        self.MainChaosWindow.makeKeyAndOrderFront_(self.MainChaosWindow)
        
    def printToDialog(self, theString):
        self.LoaderText.setStringValue_(theString)
        self.LoaderText.display()
    
    def printToOutput(self, theString, theColor=0):
        if self.shouldAppend:
            self.theOutput.textStorage().appendAttributedString_(self.createColoredText("\n" + theString,theColor))
        else:
            self.theOutput.textStorage().setAttributedString_(self.createColoredText(theString,theColor))
        self.theOutput.display()
    
    
    def refreshTags(self, printFunction):
        self.mTags=mythTags()
        self.newLocal.loadTags(printFunction)
        
        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)
        
        if self.UnitRadio.cellAtRow_column_(0,0).state()==1:
            tagType="unit"
        elif self.UnitRadio.cellAtRow_column_(1,0).state()==1:
            tagType="arti"
        elif self.UnitRadio.cellAtRow_column_(2,0).state()==1:
            tagType="scen"
        tmpList=self.newLocal.getTagList(tagType, False)
        self.extractList=[]
        for eTag in tmpList:
            self.extractList.append(eTag.getTagName())
        self.extractList.sort()
        self.UnitPopup.removeAllItems()
        self.UnitPopup.addItemsWithTitles_(self.extractList)
    
    def progressSpinner_(self, sender):
        self.progressSpinner.startAnimation_(sender)
        self.printToOutput("Updating tag list please wait...")
        self.shouldAppend=True
        self.refreshTags(self.printToOutput)
        self.printToOutput("Tag list updated.")
        self.progressSpinner.stopAnimation_(sender)
        self.shouldAppend=False
        
    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()
