local this = class("cellTeamUnitHud", G_UIModuleBase)
local _heroTpl = L_GameTpl:getHeroTpl()
local _petInfoTpl = L_GameTpl:getPetTpl()
local _elementTpl = L_GameTpl:getElementTypeTpl()
local _professionTpl = L_GameTpl:getProfessionTpl()
local _elementTypeTpl = L_GameTpl:getElementTypeTpl()

function this.bind()
  return {
    go_active = false,
    txt_name = "",
    txt_lv = "",
    imgProfession = "",
    imgHeroElement = "",
    imgPetElement = "",
    goHero = false,
    goPet = false,
    color_element = nil,
    isOn_ChangePos = false,
    go_btn_single = false,
    go_btn_double = false,
    go_btn_single_revert = false,
    go_btn_double_revert = false,
    alpha = 1,
    revert_alpha = 1
  }
end

function this.methods()
  return {
    onClick_viewDetail = function(self)
      if self.bind.go_active then
        if self.bind.roleType == L_Const.roleType.hero then
          L_UI:open("pageHero", {
            heroId = self.bind.roleId,
            pageType = L_Const.heroPageType.DetailMode
          })
        elseif self.bind.roleType == L_Const.roleType.pet then
          L_UI:open("pagePet", {
            petId = self.bind.roleId
          })
        end
      end
    end,
    onClick_viewDetail_single = function(self)
      if self.bind.go_active then
        if self.bind.roleType == L_Const.roleType.hero then
          L_UI:open("pageHero", {
            heroId = self.bind.roleId,
            pageType = L_Const.heroPageType.DetailMode
          })
        elseif self.bind.roleType == L_Const.roleType.pet then
          L_UI:open("pagePet", {
            petId = self.bind.roleId
          })
        end
      end
    end,
    onClick_viewDetail_single_revert = function(self)
      if self.bind.go_active then
        if self.bind.roleType == L_Const.roleType.hero then
          L_UI:open("pageHero", {
            heroId = self.bind.roleId,
            pageType = L_Const.heroPageType.DetailMode
          })
        elseif self.bind.roleType == L_Const.roleType.pet then
          L_UI:open("pagePet", {
            petId = self.bind.roleId
          })
        end
      end
    end,
    onClick_viewDetail_revert = function(self)
      if self.bind.go_active then
        if self.bind.roleType == L_Const.roleType.hero then
          L_UI:open("pageHero", {
            heroId = self.bind.roleId,
            pageType = L_Const.heroPageType.DetailMode
          })
        elseif self.bind.roleType == L_Const.roleType.pet then
          L_UI:open("pagePet", {
            petId = self.bind.roleId
          })
        end
      end
    end,
    onClick_Down = function(self)
      if self.parent then
        if self.parent.removeHero and self.bind.roleType == L_Const.roleType.hero then
          self.parent:removeHero(self.bind.index)
        elseif self.parent.removePet and self.bind.roleType == L_Const.roleType.pet then
          self.parent:removePet(self.bind.index)
        end
      end
    end,
    onClick_Down_revert = function(self)
      if self.parent then
        if self.parent.removeHero and self.bind.roleType == L_Const.roleType.hero then
          self.parent:removeHero(self.bind.index)
        elseif self.parent.removePet and self.bind.roleType == L_Const.roleType.pet then
          self.parent:removePet(self.bind.index)
        end
      end
    end
  }
end

function this:getIndex()
  return self.bind.index
end

function this:open()
  self:initCell()
end

function this:initCell()
  if not self.isBind then
    return
  end
  self.bind.isOn_ChangePos = self.bind.changePosMode == true
  self.bind.go_active = not math.isEmpty(self.bind.roleId)
  if self.bind.go_active then
    if self.bind.roleType == L_Const.roleType.hero then
      self:setHeroData()
    elseif self.bind.roleType == L_Const.roleType.pet then
      self:setPetData()
    end
  end
  self.showBtn = self.parent.pageName == "pageFormationMain" or self.parent.pageName == "pageWearPet"
end

function this:setHeroData()
  local serverData = L_HeroStore:getHero(self.bind.roleId)
  self._configId = L_HeroStore:getHeroConfigId(serverData)
  self.bind.txt_name = L_HeroManager:getHeroName(self.bind.roleId)
  self.bind.txt_lv = tostring(L_HeroStore:getHeroLevel(serverData))
  self.bind.goHero = true
  self.bind.goPet = false
  local tpl_hero = _heroTpl:getTplById(self._configId)
  local professionId = _heroTpl:getPosition(tpl_hero)
  local pTpl = _professionTpl:getTplById(professionId)
  self.bind.imgProfession = _professionTpl:getIcon(pTpl)
  local elementId = _heroTpl:getElement(tpl_hero)
  local eTpl = _elementTpl:getTplById(elementId)
  self.bind.imgHeroElement = _elementTpl:getProfessionColor(eTpl, L_Const.professionColorType.circle)
  local tpl_element = _elementTpl:getTplById(elementId)
  local _, color = C_ColorUtility.TryParseHtmlString(_elementTypeTpl:getElementColor(tpl_element))
  self.bind.color_element = color
end

function this:setPetData()
  local serverData = L_PetStore:getPetItem(self.bind.roleId)
  self._configId = serverData.id
  self.bind.txt_name = serverData.pet_name
  self.bind.txt_lv = tostring(serverData.lv)
  self.bind.goHero = false
  self.bind.goPet = true
  local infoTpl = _petInfoTpl:getTplById(serverData.id)
  local elementId = _petInfoTpl:getFirstElement(infoTpl)
  local eTpl = _elementTpl:getTplById(elementId)
  self.bind.imgPetElement = _elementTpl:getPetElem(eTpl)
  local tpl_element = _elementTpl:getTplById(elementId)
  local _, color = C_ColorUtility.TryParseHtmlString(_elementTypeTpl:getElementColor(tpl_element))
  self.bind.color_element = color
end

function this:getRoleType()
  return self.bind.roleType
end

function this:getGuid()
  return self.bind.roleId
end

function this:getUnitId()
  return L_GameUtil.getUnitIdByRole(self._configId, self.bind.roleType)
end

function this:getSelfRect()
  return self.bindComponents.hud
end

function this:getBtnGroup(revert)
  if revert then
    return self.bindComponents.btnGroupRevert
  end
  return self.bindComponents.btnGroup
end

function this:setRevertBtn(revert)
  self.isBtnRevert = revert
  self.bindComponents.btnGroup.gameObject:SetActive(not revert and self.showBtn)
  self.bindComponents.btnGroupRevert.gameObject:SetActive(revert and self.showBtn)
end

function this:isEnable()
  return self.bind.go_active
end

function this:setActive(active)
  self.bind.go_active = active
end

function this:setFollow()
  L_FormationManager:setFormationHudScreenPos(self)
end

function this:setAlpha(alpha)
  self.bind.alpha = alpha
  self.bind.revert_alpha = alpha
end

function this:hideEditUI()
  self.bind.go_btn_double_revert = false
  self.bind.go_btn_single_revert = false
  self.bind.go_btn_single = false
  self.bind.go_btn_double = false
end

function this:setUIState(bRevert, bDouble)
  self.bind.go_btn_double_revert = bRevert and bDouble
  self.bind.go_btn_single_revert = bRevert and not bDouble
  self.bind.go_btn_single = not bRevert and not bDouble
  self.bind.go_btn_double = not bRevert and bDouble
end

function this:checkDownBtn(state)
  if not self.showBtn then
    return
  end
  if self.bind.bEdit ~= nil and self.bind.bEdit == false then
    return
  end
  local isHeroEditState = state == L_FormationConst.FormationEditStatus.EditHero
  local isPetEditState = state == L_FormationConst.FormationEditStatus.EditPet
  if self.bind.roleType == L_Const.roleType.hero then
    if self.isBtnRevert then
      self.bind.go_btn_double_revert = isHeroEditState
      self.bind.go_btn_single_revert = not isHeroEditState
      self.bind.go_btn_single = false
      self.bind.go_btn_double = false
    else
      self.bind.go_btn_double_revert = false
      self.bind.go_btn_single_revert = false
      self.bind.go_btn_single = not isHeroEditState
      self.bind.go_btn_double = isHeroEditState
    end
  elseif self.bind.roleType == L_Const.roleType.pet then
    if self.isBtnRevert then
      self.bind.go_btn_double_revert = isPetEditState
      self.bind.go_btn_single_revert = not isPetEditState
      self.bind.go_btn_single = false
      self.bind.go_btn_double = false
    else
      self.bind.go_btn_double_revert = false
      self.bind.go_btn_single_revert = false
      self.bind.go_btn_single = not isPetEditState
      self.bind.go_btn_double = isPetEditState
    end
  elseif self.isBtnRevert then
    self.bind.go_btn_double_revert = true
    self.bind.go_btn_single_revert = false
    self.bind.go_btn_single = false
    self.bind.go_btn_double = false
  else
    self.bind.go_btn_double_revert = false
    self.bind.go_btn_single_revert = false
    self.bind.go_btn_single = false
    self.bind.go_btn_double = true
  end
end

return this
