--[[
=============================================
common.lua by Cancan

Variables and functions that can be used in diff applications/games

Email: cancangm (at) gmail (dot) com
=============================================
]]

--- Class

Selection = {
  current = 1,
  previous = 1,
  lines = 16,
  top = 1,
  max = 0,
  name = "",
  title = "",
  table = {}
}

--- General variables

cwd = System.currentDirectory()
oldPad = nil
goprg = true
idx = 0
mode = 0


--[[
================
   Functions
================
]]


--- Create the Selection

function Selection:New()
   c = {} 
   setmetatable(c, self) 
   self.__index = self 
   return c
end

--- Initialize the Selection

function Selection:Init(path, ext, title)
  local contents = System.listDirectory(cwd .. "/" .. path .. "/")

  self.table = {}
  self.max = 0

  table.sort(contents, function (a, b) return a.name < b.name end)

  for idx,file in contents do
    if ext == "" then
      if (string.sub(file.name,1,1) ~= ".") and file.directory then
        self.max = self.max + 1
        self.table[self.max] = file.name
        table.setn(self.table, self.max)
      end
    else
      if (string.lower(string.sub(file.name, string.len(file.name)-3)) == ext) then
        self.max = self.max + 1
        self.table[self.max] = string.sub(file.name, 1, string.len(file.name)-4)
      end
    end
  end

  self.title = title

  --- Windows Debugging end
end

--- Draw the Selection

function Selection:Draw()
  boardsel:clear()
  boardsel:blit(0, 0, selection, 0, 0, selection:width(), selection:height(), true)

  boardsel:print(math.floor((selection:width() - (string.len(self.title) * 8)) / 2), 4, self.title, helpcolor)

  idx = self.top
  while (idx <= self.max) and (idx < (self.top + self.lines)) do
    if self.max > 0 then
      if self.current == idx then
        boardsel:print(5, 25 + ((idx - self.top) * 12), self.table[idx], selected)
      else
        boardsel:print(5, 25 + ((idx - self.top) * 12), self.table[idx], unselected)
      end
    end
    idx = idx + 1
  end

  screen:blit(math.floor((480 - selection:width()) / 2), math.floor((272 - selection:height()) / 2), boardsel, 0, 0, boardsel:width(), boardsel:height(), true);
end

--- Event in a selection

function Selection:Event()
  if pad:up() then
    if self.current > self.top then
      self.current = self.current - 1
    else
      if self.current > 1 then
        self.current = self.top - 1
        self.top = self.top - self.lines
      end
    end

  elseif pad:down() then
    if self.current < self.max then
      self.current = self.current + 1
      if self.current == (self.top + self.lines) then
        self.top = self.current
      end
    end

  elseif pad:right() then
    if (self.top + self.lines) <= self.max then
      self.top = self.top + self.lines
      self.current = self.top
    end

  elseif pad:left() then
    if self.top ~= 1 then
      self.current = self.current - 1
      self.top = self.top - self.lines
    end

  elseif pad:cross() then
    mode = 0
    self:Load()
    self.previous = self.current

  elseif pad:circle() then
    self.current = self.previous
    if math.mod(self.current, self.lines) == 0 then
      self.top = self.current - self.lines + 1
    else
      self.top = self.current - math.mod(self.current, self.lines) + 1
    end
    mode = 0
  end
end
