local this = class("moduleKiBoDescription", G_UIModuleBase)
local petTpl = L_GameTpl:getPetTpl()
local trialPetTpl = L_GameTpl:getTrialPetTpl()
local petSkillTpl = L_GameTpl:getPetSkillTpl()

function this.bind()
  return {
    module_kiBoSkillDesc = {
      moduleName = "pages/petDuel/moduleKiBoSkillDesc"
    },
    contentActive = false
  }
end

function this.methods()
  return {}
end

function this:open()
  L_KiBoDuelManager:addListener(L_KiBoDuelManager.event.summonStatusChange, self.onSummonStatusChange, self)
end

function this:close()
end

function this:onSummonStatusChange()
  self.bind.contentActive = L_KiBoDuelManager:getSummonStatus() == L_KiBoDuelManager.summonStausEnum.inSummon
end

function this:initModule(petInfo)
  local petId
  local petLevel = 1
  local petData, skills, talents
  if petInfo.is_trial then
    local trialId = petInfo.id
    local trialTpl = trialPetTpl:getTplById(trialId)
    petId = trialPetTpl:getTrialPet(trialTpl)
    petLevel = trialPetTpl:getTrialPetLevel(trialTpl)
    skills = self:getTrialSkillInfo(petId, petLevel)
    table.sort(skills, function(a, b)
      return a.skillType > b.skillType
    end)
    talents = self:getTrialTalentInfo(petId, petLevel)
  else
    local id = petInfo.id
    petData = L_PetStore:getPetItem(id)
    petId = petData.id
    petLevel = petData.lv
    skills = self:getSkillInfo(petId, petData, petLevel)
    talents = self:getTalentInfo(petId)
  end
  local skillFeatures = {}
  for _, v in pairs(skills) do
    table.insert(skillFeatures, v)
  end
  for _, v in pairs(talents) do
    table.insert(skillFeatures, v)
  end
  self.modules.module_kiBoSkillDesc:initModule({
    skills = skillFeatures,
    nextType = L_Const.kiBoDuelInfoType.talentNext,
    previousType = L_Const.kiBoDuelInfoType.talentPrevious
  })
end

function this:getTrialTalentInfo(petConfigId, petLevel)
  local data = {}
  local tpl = petTpl:getTplById(petConfigId)
  local kiboSkill = petTpl:getKiboBPropertyskillList(tpl)
  if not table.isEmpty(kiboSkill) then
    for i, v in pairs(kiboSkill) do
      local level = self:getSkillLevelByLevel(petConfigId, petLevel)
      table.insert(data, {
        skillId = v,
        skillLevel = level,
        isFeature = true
      })
    end
  end
  local skills = petTpl:getBPropertyskillList(tpl)
  if not table.isEmpty(skills) then
    for i, v in pairs(skills) do
      local level = self:getSkillLevelByLevel(petConfigId, petLevel)
      table.insert(data, {
        skillId = v,
        skillLevel = level,
        isFeature = true
      })
    end
  end
  return data
end

function this:getTrialSkillInfo(petConfigId, petLevel)
  local data = {}
  local tpl = petTpl:getTplById(petConfigId)
  local kiboSkill = petTpl:getKiboSkillList(tpl)
  if not table.isEmpty(kiboSkill) then
    for _, v in pairs(kiboSkill) do
      local level = self:getSkillLevelByLevel(petConfigId, petLevel)
      table.insert(data, {
        skillType = v[1],
        skillId = v[2],
        skillLevel = level,
        isSkill = true
      })
    end
  end
  return data
end

function this:getTalentInfo(petConfigId)
  local tpl = petTpl:getTplById(petConfigId)
  local data = {}
  local kiboSkill = petTpl:getKiboFPropertySkillList(tpl)
  if not table.isEmpty(kiboSkill) then
    for _, v in pairs(kiboSkill) do
      table.insert(data, {
        skillId = v,
        skillLevel = 1,
        isFeature = true
      })
    end
  end
  local fixedSkillPool = petTpl:getFeatureSkillList(tpl)
  if not table.isEmpty(fixedSkillPool) then
    for _, v in pairs(fixedSkillPool) do
      table.insert(data, {
        skillId = v,
        skillLevel = 1,
        isFeature = true
      })
    end
  end
  local randomSkillPool = petTpl:getRandomSkillList(tpl)
  if not table.isEmpty(randomSkillPool) then
    for _, v in pairs(randomSkillPool) do
      table.insert(data, {
        skillId = v[2],
        skillLevel = 1,
        isFeature = true
      })
    end
  end
  return data
end

function this:getSkillInfo(petConfigId, pet, petLevel)
  local tpl = petTpl:getTplById(petConfigId)
  local data = {}
  if tpl then
    local signatureSkillPool = petTpl:getSignatureSkillList(tpl)
    if not table.isEmpty(signatureSkillPool) then
      for _, v in pairs(signatureSkillPool) do
        local level = 1
        if pet ~= nil then
          level = self:getSKillLevelByPetIetm(pet, v[2])
        else
          level = self:getSkillLevelByLevel(petConfigId, petLevel)
        end
        if not math.isEmpty(level) then
          table.insert(data, {
            skillId = v[2],
            skillLevel = level,
            isSkill = true
          })
        end
      end
    end
    local commonSkillPool = petTpl:getCommonSkillList(tpl)
    if not table.isEmpty(commonSkillPool) then
      for _, v in pairs(commonSkillPool) do
        if not math.isEmpty(v[2]) then
          local level = 1
          if pet ~= nil then
            level = self:getSKillLevelByPetIetm(pet, v[2])
          else
            level = self:getSkillLevelByLevel(petConfigId, petLevel)
          end
          if not math.isEmpty(level) then
            table.insert(data, {
              skillId = v[2],
              skillLevel = level,
              isSkill = true
            })
          end
        end
      end
    end
  end
  return data
end

function this:getSKillLevelByPetIetm(pet, skillId)
  for _, v in pairs(pet.skills) do
    if v.skill_id == skillId then
      return v.skill_lv
    end
  end
end

function this:getSkillLevelByLevel(petConfigId, level)
  local levels = petSkillTpl:getPetLevels(petConfigId)
  local curLevel = 1
  for _, v in pairs(levels) do
    if level > petSkillTpl:getPetLevel(v) and curLevel < petSkillTpl:getSkillLevel(v) then
      curLevel = petSkillTpl:getSkillLevel(v)
    end
  end
  return curLevel
end

return this
