
-- constants
local ObjectMax = 9

local ButtonsMap = {}
local Buttons = {}

local ObjectsCount = {}
local ObjectsLeft = {}

local Clock = nil
local ColorNormal = { 1, 1, 1 }
local ColorDisabled = { 0.5, 0.5, 0.5 }

local function SetDimensions(HO)
  local cols, rows
  
  if HO ~= nil then
    if HO:GetProperty("gui_rows") then rows = tonumber(HO:GetProperty("gui_rows")) end
    if HO:GetProperty("gui_columns") then cols = tonumber(HO:GetProperty("gui_columns")) end
  end
  if cols == nil then cols = tonumber(Config["ho_list_cols_count"]) end
  if rows == nil then rows = tonumber(Config["ho_list_rows_count"]) end
  if rows == nil then rows = 3 end
  if cols == nil then cols = 3 end
  
  ObjectMax = rows * cols
  local r, c, dr, dc
  r = 0
  c = 0
  
  dr = 1/rows
  dc = 1/cols
  
  Buttons = {}
  
  for i = 1, 16 do
    txt = StaticText( self:FindChild("btn"..tostring(i)) )
    if c >= cols then r = r + 1 c = 0 end
    txt:SetRealPos(c * dc, r * dr)
    txt:SetRealSize(dc, dr)
    c = c + 1
    if i <= ObjectMax then 
        Buttons[i] = txt 
        txt:SetVisible(true)
    else
        txt:SetVisible(false)
    end
  end
end

function OnCreate()  
  SetDimensions()
  --Clock = self:FindChild("Clock")
end

function OnShow()
  SetTimerWidget(self:GetName(), "clock_anim")
end

function Preproces(objects)
  local n = 0
  for i = 1,#objects do
    local name = L10n:ExpandString(objects[i])
    if ObjectsCount[ name ] == nil then ObjectsCount[ name ] ={0, false, {}} end
    ObjectsCount[name][1] = ObjectsCount[ name ][1] + 1
    table.insert(ObjectsCount[name][3], objects[i])
    n = n + 1
  end
  ObjectsCount.__length = n
end

local function MakeCaption(item, translate)
  local caption = item
  
  if translate == nil or translate == true then
    caption = L10n:ExpandString(item)
  end  
  
  if ObjectsCount[caption][1] > 1 then
    caption = caption.." ("..tostring(ObjectsCount[caption][1])..")" end
  return caption
end

function AddObjects(objects, CurrentHO)
  
  SetDimensions(CurrentHO)
  ButtonsMap    = {}
  ObjectsCount  = {}
  
  Preproces(objects)
  
  local objsCount = ObjectsCount.__length
  
  local l = 1
  local i = 1
  --for i=1,ObjectMax do
  while i <= ObjectMax do
    if l <= objsCount then
      local name = L10n:ExpandString(objects[l])
      if not ObjectsCount[name][2]  then
        --ButtonsMap[objects[l]] = Buttons[i]
        ButtonsMap[name] = Buttons[i]        
        Buttons[i]:SetText(MakeCaption(objects[l]))
        Buttons[i]:SetTextColor(ColorNormal[1], ColorNormal[2], ColorNormal[3])
        Buttons[i]:SetVisible(true)
        ObjectsCount[name][2] = true
        i = i + 1
      end
    else
      Buttons[i]:SetVisible(false)
      i = i + 1
    end
    l = l + 1
  end
  
  
  ObjectsLeft = {}
  for k,_ in pairs(ObjectsCount) do 
    if k ~= "__length" and ObjectsCount[k][2] == false then
      ObjectsLeft[ #ObjectsLeft + 1 ] = k --ObjectsCount[k][3][1]
      for _,k in pairs(ObjectsCount[k][3]) do
        CurrentHO:SetElementInteractive( k, false)
      end
    end
  end
  
  --[[
  local n = ObjectsCount.__length - ObjectMax
  while  n > 0 do
    ObjectsLeft[ #ObjectsLeft + 1 ] = objects[objsCount - n + 1]
    local name = L10n:ExpandString( objects[objsCount - n + 1] )
    for _,v in pairs(ObjectsCount[name][3]) do
      CurrentHO:SetElementInteractive( v, false)
    end    
    n = n - 1
  end 
  ]]  
end

function DisableObject(objName, CurrentHO)
  local name = L10n:ExpandString(objName)
  
  if ButtonsMap[name] ~= nil and ObjectsCount[name][1] > 1  then
    ObjectsCount[name][1] = ObjectsCount[name][1] - 1
    ButtonsMap[name]:SetText(MakeCaption(objName))    
  
  elseif ButtonsMap[name] ~= nil and #ObjectsLeft == 0   then
    ButtonsMap[name]:SetTextColor(ColorDisabled[1], ColorDisabled[2], ColorDisabled[3])
    
  elseif #ObjectsLeft > 0 then
    local btn = ButtonsMap[name]
    if btn == nil then return end
    local nxtObj = ObjectsLeft[#ObjectsLeft]
    ObjectsLeft[#ObjectsLeft] = nil
    --name = L10n:ExpandString(nxtObj)
    btn:SetText(MakeCaption(nxtObj, false))
    ButtonsMap[nxtObj]  = btn
    
    for _,v in pairs(ObjectsCount[nxtObj][3]) do
      CurrentHO:SetElementInteractive( v, true)
    end    
  end
end

function SetClock(min, sec, time)
  if string.len(sec) == 1 then sec = "0"..sec end 
  if string.len(min) == 1 then min = "0"..min end
  Clock:SetText(min..":"..sec)
end

function GetButtonPosition(name)
  name = L10n:ExpandString(name)
  pos  = ButtonsMap[name]:GetAbsRelPos()
  size = ButtonsMap[name]:GetAbsRelSize()
  size.y = 0
  return  pos + (size/2)
end

function MenuButtonClick(_sender)
  if states["sys_block_esc"] == 0 then
    OpenMainMenu()
  end
end

function DiaryButtonClick(_sender)
  d = OpenDiary()
  -- todo: kom.
  if d == nil then return end
  d:SetVisible(true)
end

function SetSenderCaption(_sender)
  SetInterfaceCaption(L10n:ExpandString(_sender:GetCaption()))
end

function ResetCaption(_sender)
  SetInterfaceCaption("")
end

function HintClick(_sender)
    if CanTakeHintClue() then
        SetHintAnimOnZero()
        PlayHoHint(self:GetName(), _sender:GetName())
    end
end

function onDiaryLeave()
  self:FindChild("diary_btn"):SetVisible(true)
  self:FindChild("diary_anim"):SetVisible(false)
end
 
function onDiaryEnter(_sender)
  self:FindChild("diary_btn"):SetVisible(false)
  self:FindChild("diary_anim"):SetVisible(true)  
  AnimateWidget("ObjectsList", "diary_anim", 0.5, 50)
  SetSenderCaption(_sender)
end
 
function onDiaryLeaveCaption()
  ResetCaption()
  onDiaryLeave()
end

function GetCounterWidget()
  return self:FindChild("counter")
end

function PulitzerEnter()
  caption = L10n:ExpandString("sys_bonus_name").." "..tostring( states["ho_bonuses_count"] )
  SetInterfaceCaption(caption)
end


