--------------------------------------------------------------------------------
--- Globals
--------------------------------------------------------------------------------
Global("gwtSearchWindow", View.SearchWindow)
Global("gwtSearchWindowGrid", gwtSearchWindow.Grid)
Global("gwtSearchName", gwtSearchWindowGrid.SearchName)
Global("gwtSearchButton", gwtSearchWindowGrid.SearchButton)
Global("gwtSearchResultContainer", gwtSearchWindowGrid.SearchResult)
Global("gwtSearchResultTemplate", View.SearchResultTemplate)

local availableSearchItems = nil
local resultContainerItems = nil

--------------------------------------------------------------------------------
--- LINES
--------------------------------------------------------------------------------
local resultItemsFunctions = nil
resultItemsFunctions = 
{
  index = 0,
  GetFreeIndex = function()
    resultItemsFunctions.index = resultItemsFunctions.index + 1
    return resultItemsFunctions.index
  end,
  CreateItem = function( index )
    local index = resultItemsFunctions.GetFreeIndex()
    local item = { }
    item.widget = mainForm:CreateWidgetByDesc(gwtSearchResultTemplate.widget:GetWidgetDesc())
    item.Button = { }
    item.Button.widget = item.widget:GetChildChecked("Button", false)
    item.Button.widget:SetName(tostring(index))
    item.Image = { }
    item.Image.widget = item.widget:GetChildChecked("Image", false)
    item.name = tostring(index)
    resultContainerItems[index] = item
    return item
  end,
  GetItem = function( index )
    if resultContainerItems[index] then
      return resultContainerItems[index]
    end
    return resultItemsFunctions.CreateItem( index )
  end,
  SearchItemByName = function( widgetname )
    for k, v in resultContainerItems do
      if v.name == widgetname then
        return v
      end
    end
    return nil
  end,
  Clear = function()
    resultContainerItems = { }
    gwtSearchResultContainer.widget:RemoveItems()
  end,
  Update = function()
    local searchResult = { }
    local searchText = gwtSearchName:getText()
    for k, v in availableSearchItems do
      if common.CompareWStringEx(searchText, k) < 1 then
        table.insert(searchResult, { name = k, texture = v})
      end
    end
    table.sort(searchResult, function(a,b)
      return common.CompareWStringEx(a.name, b.name)<0
    end)
    local i = 0
    resultItemsFunctions.Clear()
    for k, v in searchResult do
      local item = resultItemsFunctions.GetItem(i)
      item.Button.widget:SetVal("button_label", v.name)
      item.text = v.name
      item.Image.widget:SetBackgroundTexture(v.texture)
      gwtSearchResultContainer.widget:PushBack(item.widget)
    end
  end
}
--------------------------------------------------------------------------------
--- REACTIONS
--------------------------------------------------------------------------------
onReaction[gwtSearchButton.reactionName] = function(params)
  resultItemsFunctions.Update()
end
onReaction[gwtSearchResultTemplate.Button.reactionName] = function(params)
  local button = resultItemsFunctions.SearchItemByName(params.sender)
  gwtName:setText(button.text)
  UpdateBuffImage(true)
  gwtSearchWindow:show(false)
end
onReaction[gwtSearchName.reactionEnter] = onReaction[gwtSearchButton.reactionName]
onReaction[gwtSearchName.reactionESC] = function(params)
  gwtSearchWindow:show(false)
end
--------------------------------------------------------------------------------
--- Functions
--------------------------------------------------------------------------------
function OpenSearch( table )
  gwtSearchName:setFocus(true)
  if table then
    availableSearchItems = table
  end  
  resultItemsFunctions.Update()
  gwtSearchWindow:show(true)
end
function ClearItemTables()
  availableSearchItems = nil
  resultContainerItems = nil
end
--------------------------------------------------------------------------------
--- INITIALIZATION
--------------------------------------------------------------------------------
function Init()  
  gwtSearchWindow:show(false)
  gwtSearchResultTemplate.widget:Show(false)
  gwtSearchWindow.widget:SetPriority(2010)
  gwtSearchWindow.onClose = ClearItemTables
end
--------------------------------------------------------------------------------
Init()
--------------------------------------------------------------------------------
