#Tedr
import sys
from com.festina.gameserver.model.quest import State
from com.festina.gameserver.model.quest import QuestState
from com.festina.gameserver.model.quest.jython import QuestJython as JQuest


ICE_CRYSTAL_ID = 7080
OFFERING_ID = 7523
RESONANCE_AMULET_1_ID = 7284
RESONANCE_AMULET_2_ID = 7315
RESONANCE_AMULET_3_ID = 7346
RESONANCE_AMULET_4_ID = 7377
RESONANCE_AMULET_5_ID = 7408
RESONANCE_AMULET_6_ID = 7439
RESONANCE_AMULET_7_ID = 7470
DIVINE_STONE_OF_WISDOM_ID = 7081
HALISHA_MARK_ID = 7501
ADEN_ID = 57
BOOK_ID = 6622

#Mobs
START_ID = 7191
NPC1_ID = 8588
DONATH_ID = 8626
TABLET_1_ID = 8646
TABLET_2_ID = 8648
TABLET_3_ID = 8652
TABLET_4_ID = 8654
TABLET_5_ID = 8655 
TABLET_6_ID = 8659
DIVINE_STONE_MOB_ID = 8280
ARCHON_NPC_ID = 8280
NPC_TABLET6_ID = 8591

#Mobs kill

FORBIDDEN_KNOWLEDGE_ID = 5215
KILL_TABL3_ID = 5270

FINAL_KILL_ID = 5275



class Quest (JQuest) :

 def __init__(self,id,name,descr): JQuest.__init__(self,id,name,descr)

 def onEvent (self,event,st) :
#START     
    htmltext = event
    if event == "1" :
        if  st.getPlayer().getClassId().getId() == 17 :
           if st.getPlayer().getLevel() >= 76 :
              htmltext = "7191-03.htm"
              st.set("cond","1")
              st.playSound("ItemSound.quest_accept")
              st.setState(STARTED)
           else :
              htmltext = "7191-02.htm"
              st.exitQuest(1)
        else :
           htmltext = "7191-02.htm"
           st.exitQuest(1)
#NPC1          
          
    elif event == "2" :
          htmltext = "8588-05.htm"
          st.playSound("ItemSound.quest_middle")
          st.set("cond","2")
    elif event == "3" :
          st.set("cond","5")
          htmltext = "8588-06.htm"
          st.playSound("ItemSound.quest_middle")
          st.takeItems(OFFERING_ID,1)
          st.giveItems(RESONANCE_AMULET_1_ID,1)
#DONATH          
          
    elif event == "4" :
          htmltext = "8626-05.htm"
          st.set("cond","3")
    elif event == "5" :
          htmltext = "8626-06.htm"
          st.playSound("ItemSound.quest_middle")
          st.set("cond","4")
          st.takeItems(ICE_CRYSTAL_ID,1)
          st.giveItems(OFFERING_ID,1)
          
  #Tablet1        
    elif event == "6" : 
          htmltext = "8646-02.htm"
          st.set("cond","6")
          st.playSound("ItemSound.quest_middle")
          st.takeItems(RESONANCE_AMULET_1_ID,1)
  #Tablet2        
    elif event == "7" : 
          htmltext = "8648-03.htm"
          st.set("cond","8")
          st.playSound("ItemSound.quest_middle")
          st.takeItems(RESONANCE_AMULET_2_ID,1) 
#Tablet3          
    elif event == "8" : 
          htmltext = "8652-02.htm"
          st.set("cond","9")
          st.playSound("Itemsound.quest_before_battle")
          st.getPcSpawn().addSpawn(KILL_TABL3_ID)    
    elif event == "9" : 
          htmltext = "8652-06.htm"
          st.set("cond","10")
          st.playSound("ItemSound.quest_middle")          
          st.takeItems(RESONANCE_AMULET_3_ID,1)
          
 #DIVINE STONE OF WISDOM        
    elif event == "10" : 
          htmltext = "8280-07.htm"
    elif event == "10-1" : 
          htmltext = "8280-02.htm"
          st.playSound("ItemSound.quest_middle")
          st.set("cond","11")          
    elif event == "10-2" : 
          htmltext = "8280-03.htm"
          st.playSound("ItemSound.quest_middle")
          st.set("cond","12")                       
    elif event == "11" : 
          htmltext = "8280-08.htm"
          st.set("cond","13")
          st.playSound("ItemSound.quest_middle")          
          st.takeItems(DIVINE_STONE_OF_WISDOM_ID,1)
          st.giveItems(RESONANCE_AMULET_4_ID,1)
          
      #4 Tablet    
    elif event == "12" : 
          htmltext = "8654-02.htm"
          st.playSound("ItemSound.quest_middle")
          st.set("cond","14")          
          st.takeItems(RESONANCE_AMULET_4_ID,1) 

# ARCHON NPC  
    elif event == "13" : 
          htmltext = "8280-12.htm"
          st.playSound("ItemSound.quest_middle")           
          st.set("cond","15")
 #5 Tablet          
          
    elif event == "14" : 
          htmltext = "8655-03.htm"          
          st.set("cond","17")
          st.playSound("ItemSound.quest_middle")          
          st.takeItems(RESONANCE_AMULET_5_ID,1)
          
  #6 Tablet         
    elif event == "15" : 
          htmltext = "8659-02.htm"
          st.getPcSpawn().removeAllSpawn()          
          st.getPcSpawn().addSpawn(NPC_TABLET6_ID, 124251, 82154, -2813)
          st.getPcSpawn().addSpawn(FINAL_KILL_ID, 124379, 82579, -2730)
          st.playSound("Itemsound.quest_before_battle")
          st.set("cond","18")
#NPC TABLET 6          
    elif event == "16" : 
          htmltext = "8591-10.htm"
          st.giveItems(RESONANCE_AMULET_6_ID,1)
          st.playSound("ItemSound.quest_middle")
          st.set("id","0")
    elif event == "17" : 
          htmltext = "8591-11.htm"
  #6 Tablet
    elif event == "18" :
          st.getPcSpawn().removeAllSpawn()
          st.playSound("ItemSound.quest_middle")          
          htmltext = "8659-06.htm"
          st.takeItems(RESONANCE_AMULET_6_ID,1)
          st.set("cond","19")
   #Finall       
    elif event == "19" :               
          htmltext = "7191-06.htm"          
    elif event == "20" :               
          htmltext = "7191-07.htm"          
          st.addExpAndSp(2299404,0)
          st.giveItems(ADEN_ID,5000000)
          st.giveItems(BOOK_ID,1)
          st.getPlayer().setClassId(98)
          st.getPlayer().setBaseClass(98)
          st.player.broadcastUserInfo()
          st.playSound("ItemSound.quest_fanfare_2")
          st.set("cond","0")
          st.set("onlyone","1")
          st.setState(COMPLETED)
    return htmltext


 def onTalk (Self,npc,st):
   npcId = npc.getNpcId()
   htmltext = "<html><head><body>I have nothing to say you</body></html>"
   id = st.getState()
   if id == CREATED :
     st.set("cond","0")
     st.set("id","0")
     st.set("onlyone","0")     
   if npcId == START_ID and int(st.get("cond"))==0 and int(st.get("onlyone"))==0 :
        htmltext = "7191-01.htm"        
   elif npcId == START_ID and int(st.get("cond"))==1 :
      htmltext = "7191-04.htm"
   elif npcId == START_ID and int(st.get("cond"))==19 :
      htmltext = "7191-05.htm"
   

   elif npcId == NPC1_ID and int(st.get("cond"))==1 :
      htmltext = "8588-01.htm"
   elif npcId == NPC1_ID and int(st.get("cond"))==2 and st.getQuestItemsCount(OFFERING_ID)==0 :
      htmltext = "8588-02.htm"      
   elif npcId == NPC1_ID and int(st.get("cond"))==4 and st.getQuestItemsCount(OFFERING_ID)==1 :
      htmltext = "8588-03.htm"
   elif npcId == NPC1_ID and int(st.get("cond"))==5 and st.getQuestItemsCount(RESONANCE_AMULET_1_ID)==1 :
      htmltext = "8588-04.htm"


   elif npcId == DONATH_ID and int(st.get("cond"))==2 and st.getQuestItemsCount(ICE_CRYSTAL_ID)==0 :
      htmltext = "8626-01.htm"
   elif (npcId == DONATH_ID and int(st.get("cond"))==3) and st.getQuestItemsCount(ICE_CRYSTAL_ID)==0 : 
      htmltext = "8626-02.htm"
   elif npcId == DONATH_ID and int(st.get("cond"))==3 and st.getQuestItemsCount(ICE_CRYSTAL_ID)==1 :
      htmltext = "8626-03.htm"
   elif npcId == DONATH_ID and int(st.get("cond"))==4 and st.getQuestItemsCount(OFFERING_ID)==1 :
      htmltext = "8626-04.htm"
      
      
   elif npcId == TABLET_1_ID and int(st.get("cond"))==5 and st.getQuestItemsCount(RESONANCE_AMULET_1_ID)==1 :
      htmltext = "8646-01.htm"
   elif npcId == TABLET_1_ID and int(st.get("cond"))==6 :
      htmltext = "8646-03.htm"
      
      
   elif npcId == TABLET_2_ID and int(st.get("cond"))==6 and st.getQuestItemsCount(RESONANCE_AMULET_2_ID)==0 :
      htmltext = "8648-01.htm"
   elif npcId == TABLET_2_ID and int(st.get("cond"))==7 and st.getQuestItemsCount(RESONANCE_AMULET_2_ID)==1 :
      htmltext = "8648-02.htm"
   elif npcId == TABLET_2_ID and int(st.get("cond"))==8 :
      htmltext = "8648-04.htm"
      
      
   elif npcId == TABLET_3_ID and int(st.get("cond"))==8 and st.getQuestItemsCount(RESONANCE_AMULET_3_ID)==0 :
      htmltext = "8652-01.htm"
   elif npcId == TABLET_3_ID and int(st.get("cond"))==9 and st.getQuestItemsCount(RESONANCE_AMULET_3_ID)==0 :
      htmltext = "8652-03.htm"
      st.getPcSpawn().removeAllSpawn()
      st.playSound("Itemsound.quest_before_battle")
      st.getPcSpawn().addSpawn(KILL_TABL3_ID)
   elif npcId == TABLET_3_ID and int(st.get("cond"))==9 and st.getQuestItemsCount(RESONANCE_AMULET_3_ID)==1 :
      htmltext = "8652-05.htm"
   elif npcId == TABLET_3_ID and int(st.get("cond"))==10 :
      htmltext = "8652-07.htm"
      
 # DIVINE STONE OF WISDOM  
   elif npcId == DIVINE_STONE_MOB_ID and int(st.get("cond"))==10 :
      htmltext = "8280-01.htm"      
   elif npcId == DIVINE_STONE_MOB_ID and st.getQuestItemsCount(DIVINE_STONE_OF_WISDOM_ID)==0 and int(st.get("cond"))>=11 and int(st.get("cond"))<=12 :
      htmltext = "8280-04.htm"      
   elif npcId == DIVINE_STONE_MOB_ID and st.getQuestItemsCount(DIVINE_STONE_OF_WISDOM_ID)==1 and int(st.get("cond"))>=11 and int(st.get("cond"))<=12 :
      htmltext = "8280-05.htm"
   elif npcId == DIVINE_STONE_MOB_ID and int(st.get("cond"))==13 :
      htmltext = "8280-06.htm"
      
      #Tablet 4
   elif npcId == TABLET_4_ID and int(st.get("cond"))==13 and st.getQuestItemsCount(RESONANCE_AMULET_4_ID)==1 :
      htmltext = "8654-01.htm"
   elif npcId == TABLET_4_ID and int(st.get("cond"))==14 :
      htmltext = "8654-03.htm"
     #ARCHON NPC
   elif npcId == ARCHON_NPC_ID and int(st.get("cond"))==14 :
      htmltext = "8280-10.htm"
   elif npcId == ARCHON_NPC_ID and int(st.get("cond"))==15 :
      htmltext = "8280-12.htm"
      #Tablet 5
   elif npcId == TABLET_5_ID and int(st.get("cond"))==15 :
      htmltext = "8655-01.htm"
   elif npcId == TABLET_5_ID and int(st.get("cond"))==16 and st.getQuestItemsCount(RESONANCE_AMULET_5_ID)==1 :
      htmltext = "8655-02.htm"
   elif npcId == TABLET_5_ID and int(st.get("cond"))==17 :
      htmltext = "8655-04.htm"

#Tablet 6
   elif npcId ==TABLET_6_ID and int(st.get("cond"))==17 :
      htmltext = "8659-01.htm"
   elif npcId ==TABLET_6_ID and int(st.get("cond"))==18 and st.getQuestItemsCount(RESONANCE_AMULET_6_ID)==0 :
      htmltext = "8659-03.htm"
      st.set("cond","17")
   elif npcId ==TABLET_6_ID and int(st.get("cond"))==18 or int(st.get("cond"))==17 and st.getQuestItemsCount(RESONANCE_AMULET_6_ID)==1 :
      htmltext = "8659-05.htm"
   elif npcId ==TABLET_6_ID and int(st.get("cond"))==19 :
      htmltext = "8659-07.htm"           
      
     
   elif npcId == NPC_TABLET6_ID and st.getQuestItemsCount(RESONANCE_AMULET_6_ID)==0 and int(st.get("id"))==0 :
      htmltext = "8591-01.htm"
      st.set("id","1")
   elif npcId == NPC_TABLET6_ID and st.getQuestItemsCount(RESONANCE_AMULET_6_ID)==0 and int(st.get("id"))==1 :
      htmltext = "8591-03.htm"      
   elif npcId == NPC_TABLET6_ID and st.getQuestItemsCount(RESONANCE_AMULET_6_ID)==0 and int(st.get("id"))==10 :
      htmltext = "8591-07.htm"      
   elif npcId == NPC_TABLET6_ID and st.getQuestItemsCount(RESONANCE_AMULET_6_ID)==1 :
      htmltext = "8591-11.htm"
          
      
   return htmltext


 def onKill (self,npc,st):
   npcId = npc.getNpcId()
   if npcId == FORBIDDEN_KNOWLEDGE_ID :
        if int(st.get("cond")) == 6 and st.getQuestItemsCount(RESONANCE_AMULET_2_ID) == 0 :
          if int(st.get("id") > 8) :
            n = ((int(st.get("id"))-8)*10)
            if st.getRandom(9) <= n :
              st.playSound("ItemSound.quest_itemget")
              st.giveItems(RESONANCE_AMULET_2_ID,1)
              st.set("id","0")
              st.set("cond","7")
            else:
              st.set("id",str(int(st.get("id"))+1))
          else:
             st.set("id",str(int(st.get("id"))+1))
   elif npcId == KILL_TABL3_ID :
        if int(st.get("cond")) == 9 and st.getQuestItemsCount(RESONANCE_AMULET_3_ID) == 0 :
          st.giveItems(RESONANCE_AMULET_3_ID,1)
          st.playSound("ItemSound.quest_middle")
   if npcId in [1646, 1647, 1648, 1649, 1650, 1651]  :
      if int(st.get("cond")) == 15  and st.getQuestItemsCount(HALISHA_MARK_ID)<701 :
          st.giveItems(HALISHA_MARK_ID,1)
          if st.getQuestItemsCount(HALISHA_MARK_ID) == 701 :
            st.playSound("Itemsound.quest_before_battle")
            st.getPcSpawn().addSpawn(5247)
            st.takeItems(HALISHA_MARK_ID,701)
          else:
            st.playSound("ItemSound.quest_itemget")
   elif npcId == 5247 :
        if int(st.get("cond")) == 15 and st.getQuestItemsCount(RESONANCE_AMULET_5_ID) == 0 :
           st.set("cond","16")
           st.playSound("ItemSound.quest_middle")
           st.giveItems(RESONANCE_AMULET_5_ID,1)
   elif npcId == FINAL_KILL_ID :
        if int(st.get("cond")) == 18 or int(st.get("cond")) == 17 and int(st.get("id"))<10 :
           st.set("id","10")
           st.playSound("ItemSound.quest_middle")
                   
      
   return

QUEST       = Quest(86,"86_SagaoftheHierophant","Saga of the Hierophant")
CREATED     = State('Start', QUEST)
STARTING    = State('Starting', QUEST)
STARTED     = State('Started', QUEST)
COMPLETED   = State('Completed', QUEST)


QUEST.setInitialState(CREATED)
#Fist NPC
QUEST.addStartNpc(START_ID)
CREATED.addTalkId(START_ID)
STARTING.addTalkId(START_ID)
STARTED.addTalkId(START_ID)
COMPLETED.addTalkId(START_ID)
#Second NPC
STARTED.addTalkId(NPC1_ID)

STARTED.addTalkId(DONATH_ID)
#Tablet 1
STARTED.addTalkId(TABLET_1_ID) 
#Tablet 2
STARTED.addTalkId(TABLET_2_ID)
#Tablet 3
STARTED.addTalkId(TABLET_3_ID)

# DIVINE STONE MOB
STARTED.addTalkId(DIVINE_STONE_MOB_ID)


#Tablet 4
STARTED.addTalkId(TABLET_4_ID)
#Tablet 5
STARTED.addTalkId(TABLET_5_ID)
#Tablet 6
STARTED.addTalkId(TABLET_6_ID)
#NPC TABLET 6
STARTED.addTalkId(NPC_TABLET6_ID)


STARTED.addKillId(FORBIDDEN_KNOWLEDGE_ID)
STARTED.addKillId(KILL_TABL3_ID)

STARTED.addKillId(1646)
STARTED.addKillId(1647)
STARTED.addKillId(1648)
STARTED.addKillId(1649)
STARTED.addKillId(1650)
STARTED.addKillId(1651)

STARTED.addKillId(5247)

STARTED.addKillId(FINAL_KILL_ID)

STARTED.addQuestDrop(FORBIDDEN_KNOWLEDGE_ID,RESONANCE_AMULET_2_ID,1)
STARTED.addQuestDrop(KILL_TABL3_ID,RESONANCE_AMULET_3_ID,1)
STARTED.addQuestDrop(5247,RESONANCE_AMULET_5_ID,1)
STARTED.addQuestDrop(1646,HALISHA_MARK_ID,1)
STARTED.addQuestDrop(1647,HALISHA_MARK_ID,1)
STARTED.addQuestDrop(1648,HALISHA_MARK_ID,1)
STARTED.addQuestDrop(1649,HALISHA_MARK_ID,1)
STARTED.addQuestDrop(1650,HALISHA_MARK_ID,1)
STARTED.addQuestDrop(1651,HALISHA_MARK_ID,1)