--[[
=============================================
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()
  board:clear()
  if self.title == "Themes" then
    board:blit(0, 0, selectiont)
  else
    board:blit(0, 0, selection)
  end

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

  screen:blit(0, 0, board)
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
      self.previous = self.current
    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
      self.previous = self.current
    end

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

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

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

  elseif pad:circle() and self.title == "Themes" 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