AbstractMobParent = require "game.abstractmobs.parent"

AbstractCharacter = AbstractMobParent:extend()

local charutils = require "game.utils.characters"

function AbstractCharacter:new(name)
  self.simplename = name
  self.super.new(self)
end

function AbstractCharacter:initBasicElements()
  self.data = charutils.getCharacterData(self.simplename)
  self.name = self.data.name
  self.fullname = self.data.fullname
  self.turns = self.data.turns

  self.level     = self.data.startlevel
  self.exp       = charutils.getExpValue(self.level)
  self.exp_next  = charutils.getExpValue(self.level + 1)
end

function AbstractCharacter:createStats()
  local stats = {}
  local base_stats = self.data.stats

  stats.hpmax     = charutils.getHPValue(self.level,    base_stats.hpmax)
  stats.ppmax     = charutils.getPPValue(self.level,    base_stats.ppmax)
  stats.attack    = charutils.getStatValue(self.level,  base_stats.attack)
  stats.power     = charutils.getStatValue(self.level,  base_stats.power)
  stats.defense   = charutils.getStatValue(self.level,  base_stats.defense)
  stats.mind      = charutils.getStatValue(self.level,  base_stats.mind)
  stats.technic   = charutils.getStatValue(self.level,  base_stats.technic)
  stats.speed     = charutils.getStatValue(self.level,  base_stats.speed)

  return stats
end

function AbstractCharacter:createSkills()
  local learnedlist  = {}

  for i, v in ipairs(self.data.skills) do
    local tech_name, tech_level, isLearned = v[1], v[2], false
    if tech_level <= self.level then
      local canLearn = true
      for i, learnedSkill in ipairs(learnedlist) do

        -- We check if the skill have already been learned, to level-up it
        if learnedSkill.name == tech_name then
          canLearn = false
          learnedSkill.level = learnedSkill.level + 1
        end
      end

      if (canLearn) then
        local skilldata = {}
        skilldata.name      = tech_name
        skilldata.level     = 1
        skilldata.learnedAt = tech_level

        table.insert(learnedlist, skilldata)
      end

    end
    -- On continue ensuite d'itérer dans la liste
  end

  return learnedlist
end

function AbstractCharacter:setLevel(newlevel)
  self.level = newlevel
  local exp, exp_next, exp_current
  local exp_min     = charutils.getExpValue(self.level)
  local exp_max     = charutils.getExpValue(self.level + 1)
  exp = self.exp

  self.exp = math.max(math.min(exp, exp_max - 1), exp_min)
  self.exp_next = exp_max

  self.stats = self:createStats()
end

function AbstractCharacter:levelUp()
  self:setLevel(self.level + 1)
end

function AbstractCharacter:heal()
  self:initLife()
end

return AbstractCharacter
