local cls = class("cellIconPetDuelEntranceNew", G_UIModuleBase)
local petTpl = L_GameTpl:getPetTpl()
local elementTpl = L_GameTpl:getElementTypeTpl()
local _trialPetTpl = L_GameTpl:getTrialPetTpl()
local petElementColor = {
  [1] = "#E14F47",
  [2] = "#FF9537",
  [3] = "#CE8556",
  [4] = "#9BCE50",
  [5] = "#59D7D4",
  [6] = "#60BBFF",
  [7] = "#708BFF",
  [8] = "#FFF157",
  [9] = "#8C7195"
}
local layout = {61, 105}

function cls.bind()
  return {
    toggle_empty = false,
    none_img_select = false,
    active_img_select = false,
    img_bg = "",
    img_petIcon = "",
    img_elementBg = "",
    img_elementIcon = "",
    color_elementIcon = C_Color(1, 1, 1, 1),
    pos_elementIcon = C_Vector2(0, 0),
    img_subElementBg = "",
    img_subElementIcon = "",
    color_subElementIcon = C_Color(1, 1, 1, 1),
    pos_subElementIcon = C_Vector2(0, 0),
    active_subElementBg = false,
    txt_cost = "",
    txt_level = "",
    txt_petName = "",
    tagList = {
      moduleName = "pages/petDuel/cellKiBoDuelTag"
    },
    active_trialNode = false,
    active_forbiddenNode = false,
    active_formationNode = false,
    alpha_content = 1,
    shiningActive = false,
    shiningFxActive = false
  }
end

function cls.methods()
  return {
    onClick_select = function(self)
      if self.bind.callback then
        self.bind.callback(self)
      end
    end
  }
end

function cls:initData(data)
  self.petCfgId = data.petCfgId
  self.petLv = data.petLv
  self.petGuid = data.petGuid
  self.petItem = L_PetStore:getPetItem(self.petGuid)
  self.cellMode = data.cellMode
  self.slotId = data.slotId
  self.selectSlotId = data.selectSlotId
  self.isTrial = data.is_trial
  self.isForbidden = data.isForbidden
  self.bind.active_formationNode = true
  self.bind.callback = data.callback
  self:refreshView()
end

function cls:refresh()
  self.petCfgId = self.bind.petCfgId
  self.petLv = self.bind.petLv
  self.petGuid = self.bind.petGuid
  self.petItem = L_PetStore:getPetItem(self.petGuid)
  self.cellMode = self.bind.cellMode
  self.slotId = self.bind.slotId
  self.selectSlotId = self.bind.selectSlotId
  self.isTrial = self.bind.is_trial
  self.isForbidden = self.bind.isForbidden
  self.bind.active_formationNode = false
  self:refreshView()
end

function cls:refreshView()
  if self.cellMode == L_PetConst.PetDuelEntranceCellMode.PrepareEnemy then
    self.bind.toggle_empty = false
    self.bind.active_img_select = false
    self.bind.active_trialNode = false
    self.bind.active_forbiddenNode = false
    self.bind.alpha_content = 1
    local petCfg = petTpl:getTplById(self.petCfgId)
    self.bind.img_petIcon = petTpl:getKiboCardIcon(petCfg)
    self.bind.txt_level = string.format("Lv.%s", self.petLv)
    self.bind.txt_cost = tostring(L_PetStore:getPetOriginalCost(self.petCfgId))
    self.bind.txt_petName = petTpl:getName(petCfg)
    self:refreshElementInfo(petCfg)
    self:refreshTagList(self.petCfgId)
    return
  end
  if self.isTrial or self.petItem == nil then
    self.bind.shiningActive = false
    self.bind.shiningFxActive = false
  else
    self.bind.shiningActive = self.petItem:hasFlashGene()
    self.bind.shiningFxActive = self.petItem:hasFlashGene()
  end
  if self.cellMode == L_PetConst.PetDuelEntranceCellMode.PrepareSelf then
    self.bind.active_img_select = false
    self.bind.toggle_empty = self.petGuid <= 0
    self.bind.active_trialNode = self.isTrial and self.petGuid > 0
    self.bind.active_forbiddenNode = self.isForbidden
    self.bind.alpha_content = self.isForbidden and 0.6 or 1
    if self.petGuid <= 0 then
      return
    end
    local petCatchItemId = 0
    local petCfgId = 0
    local petLv = 0
    local isSpecial = 0
    if self.isTrial then
      local trialPetCfg = _trialPetTpl:getTplById(self.petGuid)
      petCfgId = _trialPetTpl:getTrialPet(trialPetCfg)
      petLv = _trialPetTpl:getTrialPetLevel(trialPetCfg)
      self:refreshTagList(petCfgId)
    else
      petCfgId = self.petItem:petCfgId()
      petCatchItemId = self.petItem:petCatchItemId()
      petLv = self.petItem:petLv()
      self:refreshTagList(self.petItem.id)
      isSpecial = self.petItem:isSpecialPet()
    end
    self.bind.txt_petName = self.bind.petName
    local petCfg = petTpl:getTplById(petCfgId)
    self.bind.img_petIcon = petTpl:getKiboCardIcon(petCfg, isSpecial)
    self.bind.txt_level = string.format("Lv.%s", petLv)
    self.bind.txt_cost = tostring(L_PetStore:getPetOriginalCost(petCfgId))
    self:refreshElementInfo(petCfg)
    return
  end
  if self.cellMode == L_PetConst.PetDuelEntranceCellMode.FormationSlot then
    self.bind.toggle_empty = self.petGuid <= 0
    self.bind.active_trialNode = self.isTrial and self.petGuid > 0
    self.bind.active_forbiddenNode = self.isForbidden
    self.bind.alpha_content = self.isForbidden and 0.6 or 1
    if self.bind.toggle_empty then
      self.bind.active_img_select = not self.slotId == self.selectSlotId
      self.bind.none_img_select = self.slotId == self.selectSlotId
    else
      self.bind.active_img_select = self.slotId == self.selectSlotId
      self.bind.none_img_select = not self.slotId == self.selectSlotId
    end
    if self.petGuid <= 0 then
      return
    end
    local petCatchItemId = 0
    local petCfgId = 0
    local petLv = 0
    local isSpecial = 0
    if self.isTrial then
      local trialPetCfg = _trialPetTpl:getTplById(self.petGuid)
      petCfgId = _trialPetTpl:getTrialPet(trialPetCfg)
      petLv = _trialPetTpl:getTrialPetLevel(trialPetCfg)
      self:refreshTagList(petCfgId)
    else
      petCfgId = self.petItem:petCfgId()
      petCatchItemId = self.petItem:petCatchItemId()
      petLv = self.petItem:petLv()
      self:refreshTagList(self.petItem.id)
      isSpecial = self.petItem:isSpecialPet()
    end
    self.bind.txt_petName = self.bind.petName
    local petCfg = petTpl:getTplById(petCfgId)
    self.bind.img_petIcon = petTpl:getKiboCardIcon(petCfg, isSpecial)
    self.bind.txt_level = string.format("Lv.%s", petLv)
    self.bind.txt_cost = tostring(L_PetStore:getPetOriginalCost(petCfgId))
    self:refreshElementInfo(petCfg)
    return
  end
  if self.cellMode == L_PetConst.PetDuelEntranceCellMode.FormationPetPool then
    self.bind.active_img_select = 0 <= self.slotId
    self.bind.toggle_empty = false
    self.bind.active_trialNode = self.isTrial and self.petGuid > 0
    self.bind.active_forbiddenNode = self.isForbidden
    self.bind.alpha_content = self.isForbidden and 0.6 or 1
    local petCatchItemId = 0
    local petCfgId = 0
    local petLv = 0
    local isSpecial = 0
    if self.isTrial then
      local trialPetCfg = _trialPetTpl:getTplById(self.petGuid)
      petCfgId = _trialPetTpl:getTrialPet(trialPetCfg)
      petLv = _trialPetTpl:getTrialPetLevel(trialPetCfg)
      local petCfg = petTpl:getTplById(petCfgId)
      self.bind.txt_petName = petTpl:getName(petCfg)
      self:refreshTagList(petCfgId)
    else
      petCfgId = self.petItem:petCfgId()
      petCatchItemId = self.petItem:petCatchItemId()
      petLv = self.petItem:petLv()
      self.bind.txt_petName = self.petItem.pet_name
      self:refreshTagList(self.petItem.id)
      isSpecial = self.petItem:isSpecialPet()
    end
    local petCfg = petTpl:getTplById(petCfgId)
    self.bind.img_petIcon = petTpl:getKiboCardIcon(petCfg, isSpecial)
    self.bind.txt_level = string.format("Lv.%s", petLv)
    self.bind.txt_cost = tostring(L_PetStore:getPetOriginalCost(petCfgId))
    self:refreshElementInfo(petCfg)
    return
  end
end

function cls:refreshElementInfo(petCfg)
  local elementIds = petTpl:getElement(petCfg)
  local eTpl = elementTpl:getTplById(elementIds[1])
  self.bind.img_bg = elementTpl:getKiBoCardElementBg(eTpl, 1)
  self.bind.img_elementBg = elementTpl:getKiBoCardElementBg(eTpl, 2)
  self.bind.img_elementIcon = elementTpl:getPetPuzzleIcon(eTpl, 2)
  local _, elementIconColor = C_ColorUtility.TryParseHtmlString(petElementColor[elementIds[1]])
  self.bind.color_elementIcon = elementIconColor
  self.bind.active_subElementBg = false
  if elementIds[1] == 3 then
    self.bind.pos_elementIcon = C_Vector2(0, 2)
  end
  if 1 < #elementIds then
    local sub_eTpl = elementTpl:getTplById(elementIds[2])
    self.bind.active_subElementBg = true
    self.bind.img_subElementBg = elementTpl:getKiBoCardElementBg(eTpl, 2)
    self.bind.img_subElementIcon = elementTpl:getPetPuzzleIcon(sub_eTpl, 2)
    local _, subElementIconColor = C_ColorUtility.TryParseHtmlString(petElementColor[elementIds[2]])
    self.bind.color_subElementIcon = subElementIconColor
    if elementIds[2] == 3 then
      self.bind.pos_subElementIcon = C_Vector2(0, 2)
    end
  end
end

function cls:refreshTagList(petId)
  if not self.isBind then
    return
  end
  local tpl = petTpl:getTplById(petId)
  local tags = petTpl:getKiboDuelTag(tpl)
  local data = {}
  for _, id in ipairs(tags) do
    table.insert(data, {tagId = id})
  end
  self.bind.tagList:clear()
  self.bind.tagList:insert_array(data)
end

function cls:setSelectHero(select)
  if self.cellMode ~= L_PetConst.PetDuelEntranceCellMode.FormationSlot then
    return
  end
  if self.bind.toggle_empty then
    self.bind.active_img_select = not self.slotId == self.selectSlotId
    self.bind.none_img_select = self.slotId == self.selectSlotId and not select
  else
    self.bind.active_img_select = self.slotId == self.selectSlotId and not select
    self.bind.none_img_select = not self.slotId == self.selectSlotId
  end
end

return cls
