local this = class("cellPetTipHomeLabor", G_UIModuleBase)
local _HomeTalentTpl = L_GameTpl:getHomeTalentTpl()
local _homeLaborTypeTpl = L_GameTpl:getHomeLaborTypeTpl()
local _petHomeTalentTpl = L_GameTpl:getPetHomeTalentTpl()
local _homeBuffTpl = L_GameTpl:getHomeBuffTpl()
local StationBuildingType2LaborDescIndex = {
  [L_HomeConst.StationBuildingType.HomeBuilding] = 1,
  [L_HomeConst.StationBuildingType.Collection] = 2,
  [L_HomeConst.StationBuildingType.Ranch] = 3,
  [L_HomeConst.StationBuildingType.Crop] = 4
}
local levelText = {
  [1] = "I",
  [2] = "II",
  [3] = "III",
  [4] = "IV",
  [5] = "V"
}
local path_infoBg = {
  [1] = "UI/Atlas/PetTalent/tex_pet_bg_tips_talent_infobg1.png",
  [2] = "UI/Atlas/PetTalent/tex_pet_bg_tips_talent_infobg2.png",
  [3] = "UI/Atlas/PetTalent/tex_pet_bg_tips_talent_infobg3.png"
}
local color_infoBg = {
  [1] = {
    line = L_Const.colorHtml.grey015,
    talentName = L_Const.colorHtml.blue001,
    talentDesc = L_Const.colorHtml.grey015
  },
  [2] = {
    line = L_Const.colorHtml.purple003,
    talentName = L_Const.colorHtml.purple004,
    talentDesc = L_Const.colorHtml.purple003
  },
  [3] = {
    line = L_Const.colorHtml.yellow004,
    talentName = L_Const.colorHtml.yellow020,
    talentDesc = L_Const.colorHtml.yellow004
  }
}
local path_starDesc = {
  [1] = "UI/Atlas/PetTalent/tex_pet_dec_tips_talent_infodec_fx1.png",
  [2] = "UI/Atlas/PetTalent/tex_pet_dec_tips_talent_infodec_fx2.png",
  [3] = "UI/Atlas/PetTalent/tex_pet_dec_tips_talent_infodec_fx3.png"
}
local path_talentIcon = {
  [1] = "UI/Atlas/PetTalent/tex_bg_talent_bg1.png",
  [2] = "UI/Atlas/PetTalent/tex_bg_talent_bg2.png",
  [3] = "UI/Atlas/PetTalent/tex_bg_talent_bg3.png"
}
local path_talentLight = {
  [1] = "UI/Atlas/PetTalent/tex_dec_talent_light1.png",
  [2] = "UI/Atlas/PetTalent/tex_dec_talent_light2.png",
  [3] = "UI/Atlas/PetTalent/tex_dec_talent_light3.png"
}

function this.bind()
  return {
    img_icon = "",
    txt_name = "",
    txt_score = "",
    list_laborDesc = {
      moduleName = "pages/pet/cellLaborDesc"
    },
    list_build = {
      moduleName = "pages/pet/new/cellPetLaborBuild"
    },
    active_list_build = true,
    go_infoBg = false,
    img_infoBg = "",
    img_starDes = "",
    img_talentIcon = "",
    img_talentTypeIcon = "",
    txt_talentLevel = "",
    color_talent_e = C_Color(1, 1, 1, 1),
    color_line = C_Color(1, 1, 1, 1),
    txt_talentName = "",
    color_talentName = C_Color(1, 1, 1, 1),
    img_light = "",
    color_talentDesc0 = C_Color(1, 1, 1, 1),
    txt_talentDesc = "",
    color_talentDesc = C_Color(1, 1, 1, 1),
    active_noTalentPart = false,
    size_root = C_Vector2(512, 182),
    activate_disableIcon = false
  }
end

function this:open()
  if self.bind.isSelectCoolie then
    self.bind.go_desc = false
    self.active_list_build = false
  end
  local laborId = self.bind.laborId
  local laborGrade = self.bind.laborGrade
  local petId = self.bind.petId
  local homeLaborTypeTpl = _homeLaborTypeTpl:getTplById(laborId)
  local petHomeTalentTpl = _petHomeTalentTpl:getTplById(petId)
  local petSize = _petHomeTalentTpl:getSizeType(petHomeTalentTpl)
  local talentId = self.bind.talentId
  local isSelectCoolie = self.bind.isSelectCoolie
  self.bind.img_icon = _homeLaborTypeTpl:getLaborIcon(homeLaborTypeTpl)
  self.bind.txt_name = _homeLaborTypeTpl:getLaborName(homeLaborTypeTpl)
  self.bind.txt_score = tostring(laborGrade)
  local buffId = _homeLaborTypeTpl:getHomeBuffIdList(homeLaborTypeTpl)[1]
  local homeBuffTpl = _homeBuffTpl:getTplById(buffId)
  local buffValueType = _homeBuffTpl:getType(homeBuffTpl)
  local buffValue = L_HomeBuffManager:getLaborBuffValue(laborId, laborGrade) / 10000
  local textOfBuffValue
  if buffValueType == L_HomeConst.buffValueType.PERCENT then
    local showBuffValue = buffValue * 100
    local showBuffValueIntPart = math.ceil(showBuffValue - showBuffValue % 1)
    local showBuffValueFloatPart = showBuffValue % 1
    local showBuffValue = 0.5 <= showBuffValueFloatPart and showBuffValueIntPart + 1 or showBuffValueIntPart
    textOfBuffValue = showBuffValue .. "%"
  else
    local showBuffValue = buffValue
    local showBuffValueIntPart = math.ceil(showBuffValue - showBuffValue % 1)
    local showBuffValueFloatPart = showBuffValue % 1
    local showBuffValue = 0.5 <= showBuffValueFloatPart and showBuffValueIntPart + 1 or showBuffValueIntPart
    textOfBuffValue = tostring(showBuffValue)
  end
  if isSelectCoolie then
    if self.bind.stationBuildingType == nil then
      return
    end
    local laborDescIndex = StationBuildingType2LaborDescIndex[self.bind.stationBuildingType]
    if laborDescIndex == nil then
      return
    end
    local tmp = {}
    local descLangString = _homeLaborTypeTpl:getLaborDescList(homeLaborTypeTpl)[laborDescIndex]
    if descLangString ~= nil and descLangString ~= "" then
      table.insert(tmp, {
        txt_desc = self:getNoTalentDescText()
      })
    end
    self.bind.list_laborDesc:clear()
    self.bind.list_laborDesc:insert_array(tmp)
  else
    local tmp = {}
    for i = 1, #_homeLaborTypeTpl:getLaborDescList(homeLaborTypeTpl) do
      local descLangString = _homeLaborTypeTpl:getLaborDescList(homeLaborTypeTpl)[i]
      if descLangString ~= nil and descLangString ~= " " and descLangString ~= "" then
        table.insert(tmp, {
          txt_desc = self:getNoTalentDescText(i)
        })
      end
    end
    self.bind.list_laborDesc:clear()
    self.bind.list_laborDesc:insert_array(tmp)
  end
  self.bind.list_build:clear()
  if not isSelectCoolie then
    local buildingIconList = _homeLaborTypeTpl:getBuildingIconListByPetSize(homeLaborTypeTpl, petSize)
    local buildingNameList = _homeLaborTypeTpl:getBuildingNameListByPetSize(homeLaborTypeTpl, petSize)
    local tmp = {}
    for _, _ in pairs(buildingIconList) do
      table.insert(tmp, {})
    end
    self.bind.list_build:insert_array(tmp)
    for i = 1, #self.bind.list_build do
      local cellPetLaborBuild = self.bind.list_build:getItemCls(i)
      local params = {
        icon = buildingIconList[i],
        text = buildingNameList[i]
      }
      cellPetLaborBuild:setCellIconBagShowParams(params)
    end
  end
  if talentId then
    self.bind.active_noTalentPart = false
    self.bind.go_infoBg = true
    local tpl = _HomeTalentTpl:getTplById(talentId)
    self.bind.img_infoBg = path_infoBg[_HomeTalentTpl:getTalentLevel(tpl)]
    self.bind.img_starDes = path_starDesc[_HomeTalentTpl:getTalentLevel(tpl)]
    self.bind.img_talentIcon = path_talentIcon[_HomeTalentTpl:getTalentLevel(tpl)]
    self.bind.img_talentTypeIcon = _HomeTalentTpl:getTypeIcon(tpl)
    self.bind.txt_talentLevel = levelText[_HomeTalentTpl:getTalentLevel(tpl)]
    self.bind.img_light = path_talentLight[_HomeTalentTpl:getTalentLevel(tpl)]
    local _, lineColor = C_ColorUtility.TryParseHtmlString(color_infoBg[_HomeTalentTpl:getTalentLevel(tpl)].talentName .. "80")
    self.bind.color_line = lineColor
    self.bind.txt_talentName = string.format("%s\194\183%s", _HomeTalentTpl:getTalentName(tpl), levelText[_HomeTalentTpl:getTalentLevel(tpl)])
    local _, nameColor = C_ColorUtility.TryParseHtmlString(color_infoBg[_HomeTalentTpl:getTalentLevel(tpl)].talentName)
    self.bind.color_talentName = nameColor
    self.bind.txt_talentDesc = _HomeTalentTpl:getTalentDescription(tpl)
    local _, descColor0 = C_ColorUtility.TryParseHtmlString(color_infoBg[_HomeTalentTpl:getTalentLevel(tpl)].talentDesc .. "6B")
    self.bind.color_talentDesc0 = descColor0
    local _, descColor = C_ColorUtility.TryParseHtmlString(color_infoBg[_HomeTalentTpl:getTalentLevel(tpl)].talentDesc)
    self.bind.color_talentDesc = descColor
    self.bind.color_talent_e = descColor
  else
    self.bind.go_infoBg = false
    if isSelectCoolie then
      self.bind.active_noTalentPart = false
      local tmp = {}
      table.insert(tmp, {
        txt_desc = self:getNoTalentDescText()
      })
      self.bind.list_laborDesc:clear()
      self.bind.list_laborDesc:insert_array(tmp)
    end
  end
  self.bind.size_root = C_Vector2(512, self.bindComponents.desc.preferredHeight + 102)
end

function this:getNoTalentDescText(index)
  local laborId = self.bind.needLaborType and self.bind.needLaborType or self.bind.laborId
  local homeLaborTypeTpl = _homeLaborTypeTpl:getTplById(laborId)
  local noTalentDesc
  if self.bind.stationBuildingType == nil then
    noTalentDesc = _homeLaborTypeTpl:getLaborDescDetailList(homeLaborTypeTpl)[index]
  else
    noTalentDesc = _homeLaborTypeTpl:getLaborDescDetailList(homeLaborTypeTpl)[self.bind.stationBuildingType]
  end
  local buffId = _homeLaborTypeTpl:getHomeBuffIdList(homeLaborTypeTpl)[1]
  local homeBuffTpl = _homeBuffTpl:getTplById(buffId)
  local buffValueType = _homeBuffTpl:getType(homeBuffTpl)
  local buffValue = L_HomeBuffManager:getLaborBuffValue(laborId, self.bind.laborGrade) / 10000
  local textOfBuffValue
  if buffValueType == L_HomeConst.buffValueType.PERCENT then
    local showBuffValue = buffValue * 100
    local showBuffValueIntPart = math.ceil(showBuffValue - showBuffValue % 1)
    local showBuffValueFloatPart = showBuffValue % 1
    local showBuffValue = 0.5 <= showBuffValueFloatPart and showBuffValueIntPart + 1 or showBuffValueIntPart
    textOfBuffValue = showBuffValue .. "%"
  else
    local showBuffValue = buffValue
    local showBuffValueIntPart = math.ceil(showBuffValue - showBuffValue % 1)
    local showBuffValueFloatPart = showBuffValue % 1
    local showBuffValue = 0.5 <= showBuffValueFloatPart and showBuffValueIntPart + 1 or showBuffValueIntPart
    textOfBuffValue = tostring(showBuffValue)
  end
  return L_Lang:get(noTalentDesc, {
    [0] = textOfBuffValue
  })
end

return this
