-----------------------------------------------------------------------
-- the table where all the functions/methods go to
-----------------------------------------------------------------------
TriggerControl = {
   quest,
   name,
   locations,
   iactors,
   checklist
}
-----------------------------------------------------------------------

-----------------------------------------------------------------------
-- init function
-----------------------------------------------------------------------
function TriggerControl:init()
   local questName = "TriggerControl"
   -- the quest object stores the C++ object pointer (Quest object)
   self.quest = QuestMgr:createQuest(questName)
   self.name  = questName
   
   -- the locations associated with this quest
   self.locations = {
      "bazar",
      "desert_beach",
      "desert_crossing",
      "desert_plain",
      "desert_rock",
      "giza",
      "home",
      "nile",
      "oasis",
      "palace_backdoor",
      "palace_cellar",
      "palace_courtyard",
      "palace_garden",
      "palace_prison",
      "palace_temple",
      "palace_throneroom",
      "ship_wreck",
      "sphinx_inside",
      "sphinx_outside",
      "underworld"
   }
   -- associate the locations with the quest
   for key, value in ipairs(self.locations) do
      self.quest:addLocation(value)
   end

   -- the interactor objects associated with this quest
   self.iactors = {
      "iactor_bazar_canal_to_bazar_burger",
      "iactor_bazar_b_to_bazar_plaza",
      "iactor_palace_gate_street_to_bazar_plaza"
   }

   -- the checklist of little tasks associated with this quest
   self.checklist = {
      "DinarInShadyShop"
   }

   -- associate the triggers with the quest
   self.quest:addTrigger("trig_bazar_stall_to_bazar_burger", "bazar")
   self.quest:addTrigger("trig_home_shadowzone_a", "home")
   self.quest:addTrigger("trig_home_shadowzone_b", "home")
   --self.quest:addTrigger("trig_sphinx_inside_shadowzone_a", "sphinx_inside")

   -- associate the interactors with the quest
   for key, value in ipairs(self.iactors) do
      self.quest:addInteractor(value)
   end
   -- associate the list items with the quest
   for key, value in ipairs(self.checklist) do
      self.quest:addTaskToCheckList(value)
   end

   -- subscribe the lua functions to call
   self.quest:subscribeLocationEntered(self.name .. ":onLocationEntered")
   self.quest:subscribeLocationLeft(self.name .. ":onLocationLeft")
   self.quest:subscribeInteractorEvent(self.name .. ":onInteractorEvent")
   self.quest:subscribeInteractorEntered(self.name .. ":onMouseEnteredInteractor")
   self.quest:subscribeInteractorLeft(self.name .. ":onMouseLeftInteractor")
   self.quest:subscribeLocationFadingInFinished(self.name .. ":onLocationFadingInFinished")
   self.quest:subscribeTriggerEvent(self.name .. ":onTriggerEvent")   
   self.quest:subscribeTriggerFaceEvent(self.name .. ":onTriggerFaceEvent")   
   self.quest:subscribeCheckListChanged(self.name .. ":updateFromCheckList")   
   
   -- set the initial quest state
   self.quest:setState("Initialized")
end
-----------------------------------------------------------------------

--------------------------------------------------------------------------
-- sets the quest active and updates the iactor configurations
--------------------------------------------------------------------------
function TriggerControl:activate()
   _LogInfo(self.name .. ":activate", self.name)
   self.quest:setActive(true)
end
-----------------------------------------------------------------------

-----------------------------------------------------------------------
-- sets the quest inactive and resets the iactor configurations
-----------------------------------------------------------------------
function TriggerControl:deactivate()
   _LogInfo("TriggerControl:deactivate", "called")
   self.quest:setActive(false)
end
-----------------------------------------------------------------------

-----------------------------------------------------------------------
-- lua slot called when an action was performed on an interactor
-- associated with this quest
-----------------------------------------------------------------------
function TriggerControl:onInteractorEvent(p_Interactor, p_ActionID, p_InteractedInteractor)
   _LogInfo("TriggerControl:onInteractorEvent", p_Interactor)

   if p_Interactor == "iactor_bazar_canal_to_bazar_burger" and p_ActionID == Interactor.Leave then
      self.quest:setDone("DinarInShadyShop")
   end

   if (p_Interactor == "iactor_bazar_b_to_bazar_plaza" or p_Interactor == "iactor_palace_gate_street_to_bazar_plaza") and
       p_ActionID == Interactor.Leave then
      self.quest:setUndone("DinarInShadyShop")
   end

end
-----------------------------------------------------------------------

-----------------------------------------------------------------------
-- lua slot called when the mouse entered an iactor associated 
-- with this quest
-----------------------------------------------------------------------
function TriggerControl:onMouseEnteredInteractor(p_Interactor)
   _LogInfo("TriggerControl:onMouseEnteredInteractor", p_Interactor)
end
-----------------------------------------------------------------------

-----------------------------------------------------------------------
-- lua slot called when the mouse left an iactor associated 
-- with this quest
-----------------------------------------------------------------------
function TriggerControl:onMouseLeftInteractor(p_Interactor)
   _LogInfo("TriggerControl:onMouseLeftInteractor", p_Interactor)
end
-----------------------------------------------------------------------

-----------------------------------------------------------------------
-- lua slot called when the player entered a location that is associated
-- with this quest
-----------------------------------------------------------------------
function TriggerControl:onLocationEntered(p_Location)
   _LogInfo("TriggerControl:onLocationEntered", p_Location)
end
-----------------------------------------------------------------------

-----------------------------------------------------------------------
-- lua slot called when the player left a location that is associated
-- with this quest
-----------------------------------------------------------------------
function TriggerControl:onLocationLeft(p_Location)
   _LogInfo("TriggerControl:onLocationLeft", p_Location)

   if p_Location == "bazar" then
      CharacterControllerMgr:stop("char_dinar")
   end

   --reset assil's diffuse value which could have been altered 
   --by a shadow trigger within the left location
   char_assil:animateDiffuseFactor(1.0)

end
-----------------------------------------------------------------------

-----------------------------------------------------------------------
-- lua slot called whenever a location associated with this quest
-- has finished fading in
-----------------------------------------------------------------------
function TriggerControl:onLocationFadingInFinished(p_Location)
   _LogInfo("TriggerControl:onLocationFadingInFinished", p_Location)

   if p_Location == "bazar" and LocationMgr:getLocationActivePathNodes("bazar") == "plaza" then
      self.quest:setUndone("DinarInShadyShop")
   end

end
-----------------------------------------------------------------------

-----------------------------------------------------------------------
-- lua slot called when a trigger associated with this quest
-- has fired its event
-----------------------------------------------------------------------
function TriggerControl:onTriggerEvent(p_Trigger)
   _LogInfo("TriggerControl:onTriggerEvent", p_Trigger)
end
-----------------------------------------------------------------------

-----------------------------------------------------------------------
-- lua slot called when a trigger associated with this quest
-- has fired its event enhanced with the face the trigger was entered
-----------------------------------------------------------------------
function TriggerControl:onTriggerFaceEvent(p_Trigger,p_FrontFace)
   _LogInfo("TriggerControl:onTriggerFaceEvent", p_Trigger)

   if p_Trigger == "trig_bazar_stall_to_bazar_burger" then
      if p_FrontFace then
         self.quest:setDone("DinarInShadyShop")
      else
         self.quest:setUndone("DinarInShadyShop")
      end
   end

   if p_Trigger == "trig_home_shadowzone_a" then
      if p_FrontFace then
         char_assil:animateDiffuseFactor(1.0)
      else
         char_assil:animateDiffuseFactor(0.0)
      end
   end   

   if p_Trigger == "trig_home_shadowzone_b" then
      if p_FrontFace then
         char_assil:animateDiffuseFactor(1.0)
      else
         char_assil:animateDiffuseFactor(0.0)
      end
   end

   --[[if p_Trigger == "trig_sphinx_inside_shadowzone_a" then
      if p_FrontFace then
         char_assil:animateDiffuseFactor(1.0)
      else
         char_assil:animateDiffuseFactor(0.0)
      end
   end 
   ]]
end
-----------------------------------------------------------------------

-----------------------------------------------------------------------
-- check function
-----------------------------------------------------------------------
function TriggerControl:updateFromCheckList(p_Task, p_Done)
   --_LogInfo("TriggerControl:updateFromCheckList", "")

   if p_Task == "DinarInShadyShop" then
      if not QuestMgr:isQuestActive("Embassy") and not QuestMgr:isQuestActive("Sphinx") and
         not QuestMgr:isQuestActive("SunOfCairo") then
         if p_Done then
            char_dinar:setConfiguration("cfg_char_dinar_shady_shop")
            char_dinar:setPathnodeGraph("dinar")
            char_dinar:setPosition(2216, 645, -5115)
            char_dinar:setYaw(Degree(55))
            CharacterControllerMgr:walk("char_dinar", PinaVector3(2320, 649, -4768), Degree(352))
         else
            char_dinar:setConfiguration("cfg_char_dinar")
            char_dinar:setPathnodeGraph("dinar")
            char_dinar:setPosition(708, 1087, -5487)
            char_dinar:setYaw(Degree(-43))
            CharacterControllerMgr:walk("char_dinar", PinaVector3(895, 1074, -4876), Degree(330))
         end
      end
   end

end
-----------------------------------------------------------------------

