-------------------------------------------------------------------------------------------
local HintsArray = {}
local hintWindows = {"Inventory", "ho_counter_list", "ObjectsList"}
-------------------------------------------------------------------------------------------
function AddHint(scenes, _master, _hint)
	  local f = function(scene, v )
		if HintsArray[scene] == nil then 
		  HintsArray[scene] ={} 
		end
		
		table.insert(HintsArray[scene], v )
	  end
	  
	  h =  { master = _master, hint = _hint }
	  if type(scenes) == "string" then
		f(scenes, h)
		
	  elseif type(scenes) == "table" then
		for i=1,#scenes do 
		  f(scenes[i], h)
		end    
	  end
end
-------------------------------------------------------------------------------------------
local GameHintTime = 10*1000
  action["hint_showGameHint"] = function(r, c, hint)
  --if states["sys_was_hint_"..hint] == 1 then return end
  --states["sys_was_hint_"..hint] = 1 
  OpenHintWindowBox(hint)
end
-------------------------------------------------------------------------------------------
function ShowGameHint(scene)
	if s.sys_get_tutorial==0 then 
	  states["sys_hint_box_window_quit"] = 0
	  q = Cmd()
	  if type(scene) == "string" then
		--q(tSetState("sys_block_dialog", 1))
		q(tAction("hint_showGameHint", scene))
		--q(tSetState("sys_block_dialog", 0))
		return
	  end  
	  if HintsArray[scene:GetName()] == nil then return end
	  local i = 1
	 --q(tSetState("sys_block_dialog", 1))
	  while HintsArray[scene:GetName()][i] ~= nil do
		if HintsArray[scene:GetName()][i]["master"] == nil or  HintsArray[scene:GetName()][i]["master"]() == true 
		then      
		  q(tAction("hint_showGameHint", HintsArray[scene:GetName()][i]["hint"]))
		  q(tWaitForStateChange("sys_hint_box_window_quit"))
		end
		i = i + 1
	  end
	 --q(tSetState("sys_block_dialog", 0))
	end
end
-------------------------------------------------------------------------------------------
local function GetRegions()
  local regions = GetSceneNodesByType(EMystNodeActionRegion2D)
  local region_tab = {}
  local dialog_tab = {}
  if regions == nil or #regions == 0 then return nil end
  for i=1,#regions do
    if regions[i]:IsInteractive() and regions[i]:IsVisible() and regions[i]:GetCursor() ~= "-1" then
      local name = regions[i]:GetName()
      if string.sub( name, 1, 7) == "reg_dlg" then
        table.insert(dialog_tab, regions[i])
      else
        table.insert(region_tab, regions[i])
      end
    end
  end
  outTab = nil
  if #dialog_tab > 0 then outTab = dialog_tab else outTab = region_tab end
  
  if #outTab == 0 then return nil end
  -- return outTab[math.random(#outTab)]  
  return outTab
end
-------------------------------------------------------------------------------------------
local function GetRegion()
  local regions = GetRegions()
  if regions then
    return regions[math.random(#regions)]
  end
end
-------------------------------------------------------------------------------------------
local function GetGates()
  local gates = GetSceneNodesByType(EMystNodeGateRegion2D)
  local notVisited = {}
  --local visited = {}
  if gates == nil or #gates == 0 then return nil end
  for i=1,#gates do
    if gates[i]:IsInteractive() and gates[i]:IsVisible()
    then
      local hoNotSolvedStateName = gates[i]:GetDestination().."_ho_not_solved"
      
      if  states[gates[i]:GetDestination().."_visit"] == 0 or s[hoNotSolvedStateName] == 1  then
        table.insert(notVisited, gates[i])
      --else 
        --table.insert(visited, gates[i])
      end
    end
  end
  -- outTab = nil
  -- if #notVisited > 0 then outTab = notVisited else outTab = visited end
  
  -- if #outTab == 0 then return nil end
  -- return outTab[math.random(#outTab)]
  -- if #notVisited > 0 then
    -- return notVisited[math.random(#notVisited)]
  -- end
  
  if #notVisited > 0 then
     return notVisited
  end
end

local LoopedHintEffectName = "game_hint"
local function GetGate()
  local gates = GetGates()
  if gates ~= nil and #gates > 0 then
    return gates[math.random(#gates)]
  end
end

function PlayHintLooped()
  
  local ho = GetSceneNodesByType(EMystNodeHiddenGroup)
  if ho ~= nil and #ho > 0 then return end
  
  local gates = GetGates()
  local regions = GetRegions()
  local names = ""
  local f = function(tab)
    for k,v in pairs(tab) do
      if v:GetCursor() ~= "-1" then 
		if names ~= "" then names = names.."," end
        names = names..v:GetName()
        local regionName = v:GetName()
		if regionName ~=nil and _G[s.sys_current_scene] ~= nil then 
			local region = _G[s.sys_current_scene][regionName]
			local no_hint_property = nil
			if region~=nil then
        if region.self == nil then print(regionName) end
				no_hint_property = region.self:GetProperty("no_hint")
			end
			if no_hint_property == nil then
				ParticleAdd(LoopedHintEffectName, 2500, v:GetCenter(),regionName)
			end
		end	
      end
    end
  end
  if gates~=nil then	
    f(gates)  end
  if regions~=nil then
    f(regions)  end
  s["sys_hints_stars_names"] = names
end

function ShowHint()
  local node = GetRegion()
  if node == nil then node = GetGate() end
  if node == nil then return end

  PlaySound("snd_fx_hint")  
  ParticleAdd(states["hint_gate_particle"], 2500, node:GetCenter())
end

function SetEffectParticleName(key, value)
  states[key] = value
end

-------------------------------------------------------------------------------------------
-- HO Hints

local MaxHintFrames   = 46
local MaxClockFrames  = 61
local HintLoadingTime = 11
local ClockWnd = nil

-------------------------------------------------------------------------------------------
local GetControlName = function(name)
  if s.sys_makeda_gui == 1 then return name.."_m" end
  return name
end

function PlayHoHint(windowName, senderName, offset)
  local hoModule = _G[MystHiddenGroup:GetMainLuaModuleName()]
  if hoModule == nil or not hoModule.IsAnyActive() then
    return false
  end  

  PlaySound("snd_fx_hint_ho")
  
  hoModule.PlayHint(offset)

  if windowName == nil then 
    return false 
  end
  
  return true
end
-------------------------------------------------------------------------------------------
function UpdateHOTimer(time_past, Time)
  if ClockWnd == nil then return end
  l = ClockWnd:GetCurrentIndex()
  n = math.floor( (time_past * MaxClockFrames) / Time )
  if n < 0 then n = 0 end
  if n > MaxClockFrames then n = MaxClockFrames end
  ClockWnd:SetItemIndex(n)
  if l ~= n then PlaySound("snd_fx_timer") end
end

function SetTimerWidget(parentWidget, widget)
  HintLoadingTime = tonumber(Config["hint_loading_time"])
  if HintLoadingTime == nil then HintLoadingTime = 60 end
  ClockWnd = StaticImage( Widget(parentWidget):FindChild(widget) )
end

function ResetHoTimer()
  if ClockWnd ~= nil then ClockWnd:SetItemIndex(0) end
  ClockWnd = nil
end


