--------------------------------------------------------------------
-- Big Item Panel
--------------------------------------------------------------------

local WndImage = nil
local ItemsList = {}
local ItemsIterator = 1

function OnShow()
  Widgets.Inventory.Lock(true)
end

function OnCreate()
end

function OnOutClick()
  Close()
end

function OnDestroy()
  ItemsList = {}
  ItemsIterator = 1
  Dialogs.StopLine()
end

function OnHide()
  ItemsList = {}
  ItemsIterator = 1
  Widgets.Inventory.Unlock()
  Dialogs.StopLine()
end
--------------------------------------------------------------------
function CloseButtonClick(_sender)
  Close()
end
--------------------------------------------------------------------
function OnKeyPress(sender, key)
  if _key == KC_ESCAPE then
    Close()
  end
end

function Close()
  self:SetVisible(false)
  if Items.CloseBig ~= nil then
    Items.CloseBig(ItemsList[ItemsIterator], self)
  end
  Widgets.Inventory.HideBigPanel()
end

action["BigItem_lockInventory"] = function()
  Widgets.Inventory.Lock(true)
end

function ItemName2Text(itemName)
    function Ex(label)
        return L10n:ExpandString(label)
    end
    
    function Space(spaceCount)
        local str = ""
        for i=1,spaceCount do
            str = str.." " 
        end
        return str
    end
    
    local ret = ""
    
    if itemName == "i_letter_1" then
        ret = Ex("doc_01_01").." \n\n"..Ex("doc_01_02")..Ex("doc_01_03")..Ex("doc_01_04")
    end
    if itemName == "i_letter_2" then
        ret = Ex("doc_01_05")..Ex("doc_01_06")..Ex("doc_01_07")..Ex("doc_01_08")
    end
    if itemName == "i_letter_3" then
        ret = Ex("doc_01_09")..Ex("doc_01_10")..Ex("doc_01_11")..Ex("doc_01_12")
    end
    
    if itemName == "i_letter_4" then
        ret = Ex("doc_01_13")..Ex("doc_01_14")..Ex("doc_01_15").."\n\n"..Ex("doc_01_16").." \n"..Ex("doc_01_17")
    end
    

    

    return ret
end

local function ShowItem(item)
  local setImage = true
  
 
  if item ~= "" then
    Widget("TextAreaItemBig"):SetText(ItemName2Text(item))  
  end
 
  
  local com = "abt_"..item.."_hero"
  if L10n:StringExists(com) and L10n:ExpandString(com) ~= "" then
    if item== "i_letter" then
        if s[com]==0 then
            q = Cmd(tTalk(com))
            q(tAction("BigItem_lockInventory"))  
        end
    else
        q = Cmd(tTalk(com))
        q(tAction("BigItem_lockInventory"))        
    end
  end
  
  if WndImage == nil then 
      WndImage = StaticImage(self:FindChild("Image"))
  end

  if Items.OpenBig ~= nil then
    setImage = Items.OpenBig(item, self)
  end
  
  if setImage then
    WndImage:SetImage(item, "BigItemsSet", item.."_b")
    --WndImage:SetImage(item, "BigItemsSet", "big_items")
  end
  
  UpdateArrows()
end

function MousePressed(_sender, _x, _y, _btn)
  if _btn == MB_RIGHT then
    Close()
  end
end

function ItemClick(_sender)
  Items.BigEvent(ItemsList[ItemsIterator], MystInventory:GetActive():GetSelectedItem())
end

function SetItem(item)
  s["abt_i_letter_hero"] = 0 
  ItemsList = item:split(",")
  ItemsIterator = 1
  ShowItem(ItemsList[ItemsIterator])
end

function GetItem()
  return ItemsList[ItemsIterator]
end

function IsVisible()
  return self:IsVisible()
end

function OnNextItem(_sender)
  if ItemsIterator < #ItemsList then
    ItemsIterator = ItemsIterator + 1
    ShowItem(ItemsList[ItemsIterator])
  end
end

function OnPrevItem(_sender)
  if ItemsIterator > 1 then
    ItemsIterator = ItemsIterator - 1
    ShowItem(ItemsList[ItemsIterator])
  end
end

local blink_time = GetTime()
local focus = false

function Update()
    if GetTime() > blink_time + 0.8 then
        if ItemsIterator <  #ItemsList then
        --    Button("NextItem"):SetMouseFocus(focus)
        end
        
         if ItemsIterator > 1 then
        --    Button("PrevItem"):SetMouseFocus(focus)
        end
        
        focus = not focus 
        blink_time = GetTime()
    end
end

function UpdateArrows()
  right = Button(self:FindChild("NextItem"))
  left = Button(self:FindChild("PrevItem"))
  left:SetVisible(ItemsIterator > 1 )
  right:SetVisible(ItemsIterator <  #ItemsList )
  --left:SetVisible( ItemsIterator > 1 )
  --right:SetVisible( ItemsIterator <  #ItemsList )
  --print(ItemsIterator, #ItemsList)
end

