dofile("ho_gui.lua")

local objects = {}
local diabledObjects = {}
local GUI = nil
local Active = false
CurrentHO = nil

function GetInterface( ho_type )
  G = nil
  if ho_type == "shadows" then
    G = HOShadowGui()
    return G
  end
  if ho_type == "counter" then
    G = HOCounterGui()
    return G
  end
  if ho_type == "items" then
    G = HOItemsGui()
    return G
  end
  -- ho_type == "list" 
  G = HOListGui()
  return G    
  --printerror("[HO] Zły typ hidden object, dopuszczalne to:", "list", "shadows")
end

function AddObject(object)
  --objects[#objects + 1] = object:GetName()
  objects[#objects + 1] = object
end

-- po kliknieciu w przedmiot

action["ho_disable_object"] = function(r, c, object)
  s.sys_block_esc = s.sys_block_esc - 1
  DisableObject(object)
end

local Queue = nil
local Queues = {}
function ObjectFound(object)
  s.sys_block_esc = s.sys_block_esc + 1
  Queue = PlayEfect(object)
  Queue(tAction("ho_disable_object", object))
  --Queue:SetHolded()
  
  -- co by nie wynilowac kolejki przed jej zakonczeniem
  local idx = 1
  while Queues[idx] ~= nil and Queues[idx]:IsActive() do idx = idx + 1 end
  Queues[idx] = Queue
  
  PlaySound("snd_take")
end

function AddObjectToDisabled(object)
  diabledObjects[#diabledObjects + 1]  = object
end

function DisableObject(object)
  GUI:RemoveItem(object, CurrentHO)
end

function GamePause()
s.sys_block_only_esc = 0
  GUI:SetVisible(false)
  CurrentHO = nil
  EmptyObjects()
  
  Active = false
  --HintWnd:SetVisible(false)
  Mouse.ClickHolder:Stop()
  
  Widgets.Inventory.Unlock()
  s.sys_ho_active = 0
  
  ResetHoTimer()
end

function GameResume(HO)
s.sys_block_only_esc = 1
  Widgets.Inventory.Lock(false)
  
  GUI = GetInterface( HO:GetProperty("Type") ) 
  GUI:Open(HO:GetProperty("Name"))
  
  CurrentHO = HO
  GUI:AddItems(objects, CurrentHO)
  for k,v in pairs(diabledObjects) do
    GUI:RemoveItem(v, HO)
  end  
  EmptyObjects()
  
  Active = true
  s.sys_ho_active = 1
  
  --HintWnd = OpenHintWindow()
  
  Mouse.ClickHolder:Start()
end

-- wywolywane po wzieciu wszystkich przedmiotow
function GameEnd(HO)
  if Queue ~= nil then
    Queue( tAction("ho_game_end", HO))
  else
    DoGameEnd(HO)
  end
end

function DoGameEnd(HO)
  local hoNotSolvedStateName = MystSceneManager:GetInstance():GetCurrentView():GetName().."_ho_not_solved"
  s[hoNotSolvedStateName] = 0
  local hide_gui_prop = HO:GetProperty("hide_gui")
  if hide_gui_prop ~= nil then hide_gui_prop = hide_gui_prop:upper() end
  EmptyObjects()
  CurrentHO = nil
  Active = false
  
  if hide_gui_prop ~= "FALSE" then
    GUI:SetVisible(false)
    --HintWnd:SetVisible(false)
  end
  Mouse.ClickHolder:Stop()
  s.sys_ho_active = 0
  HO:LuaCall("OnGameEnd")
  
  Widgets.Inventory.Unlock()
end

action["ho_game_end"] = function(r, c, HO)
  DoGameEnd(HO)
end

function GameStart(HO)
  local hoNotSolvedStateName = MystSceneManager:GetInstance():GetCurrentView():GetName().."_ho_not_solved"
  s[hoNotSolvedStateName] = 1
  s.sys_block_only_esc = 1  
  Widgets.Inventory.Lock(false)
  CurrentHO = HO
  GUI = GetInterface( HO:GetProperty("Type") )
  GUI:Open(HO:GetProperty("Name"))
  GUI:AddItems(objects, CurrentHO)

  EmptyObjects()
  
  Active = true
  s.sys_ho_active = 1
  --HintWnd = OpenHintWindow()
  
  Mouse.ClickHolder:Start()  
end

function GameStop(HO)
  EmptyObjects()
  CurrentHO = nil
  Active = false
  s.sys_block_only_esc = 0
  GUI:SetVisible(false)
  --HintWnd:SetVisible(false)
  Mouse.ClickHolder:Stop()
  s.sys_ho_active = 0
  
  Widgets.Inventory.Unlock()
  ResetHoTimer()
end

function GameFail(HO)
  Mouse.ClickHolder:Stop()
  EmptyObjects()
  
  HO:LuaCall("OnGameFail")
end

function PreLoad()
  if s.sys_ho_active == 0 then return end
  GameStop()
end

function BonusFound(object)
  states["ho_bonuses_count"] = states["ho_bonuses_count"] + 1
  MoveParticle("ho_take", Mouse.GetPosition(), Mouse.GetPosition() , 1)
end

function Update(time_past, all_time)
  --Widgets.hint_window.HO_Update(time_past, all_time)
  UpdateHOTimer(time_past, all_time)
end

function EmptyObjects()
  objects = {}
  diabledObjects = {}
end

function PlayEfect(object)
    local visible_inverted = CurrentHO:GetProperty("visible_inverted")
    if visible_inverted then
        return  MoveParticle("ho_take", GUI:GetObjectPos(object) , Mouse.GetPosition(), 1, object:GetName())
    else
        return  MoveParticle("ho_take", Mouse.GetPosition(), GUI:GetObjectPos(object) , 1, object:GetName())
    end  
end

function IsAnyActive()
  return Active
end
local sys_lastHintClickTime = Time:GetTime()
function PlayHint(offset)
  local dT = Time:GetTime() - sys_lastHintClickTime
  sys_lastHintClickTime = Time:GetTime()
  if dT < 2 then
      return
  end
    
  --if s.sys_ho_active == 0 then return end
  if offset == nil then offset = Ogre.Vector2(0, 0) end
  obj = CurrentHO:GetRandomObject()
  if obj == nil then return end
  pos = Ogre.Vector2()
  pos.y = obj:GetCenter().y + offset.y
  pos.x = obj:GetCenter().x + offset.x
  
  ParticleAdd(Config["hint_particle_name"], nil, pos, obj:GetName() )
end
