local this = class("cellProduceTarget", G_UIModuleBase)
local _homeBuildingTpl = L_GameTpl:getHomeBuildingTpl()
local _foodGroupTpl = L_GameTpl:getFoodGroupTpl()
local _taskTpl = L_GameTpl:getTaskTpl()
local _taskTypeTpl = L_GameTpl:getTaskTypeTpl()
local _productsMultiQualityTpl = L_GameTpl:getProductsMultiQualityTpl()
local _commonItemTpl = L_GameTpl:getCommonItemTpl()

function this.bind()
  return {
    emptyActive = true,
    contentActive = false,
    commonIcon = nil,
    commonIconActive = true,
    buildingIcon = nil,
    buildingIconActive = false,
    starList = {},
    itemName = "",
    itemNameActive = true,
    selectIconActive = false,
    lockIconActive = false,
    petNeededIconActive = false,
    iconBg = nil,
    limitCountActive = false,
    limitText = "",
    unlockText = "",
    icon_foodType = "",
    active_iconHasGet = false,
    text_hasGet = L_Lang:get(L_WordsTpl:getValue("tag_home_product_reachlimit")),
    active_taskNode = false,
    img_taskType = nil,
    color_taskTypeImg = nil
  }
end

function this.methods()
  return {
    onClick_select = function(self)
      self:emit("onClick_select", self.bind)
      self:emit("onClick_selectId", self.bind.id, self.bind.isFood)
      if self._callback then
        self._callback()
      end
    end
  }
end

function this:refresh()
  self.bind.iconBg = "UI/Atlas/HomeProductBuilding/tex_homebuilding_bg_raritysqrtd_none"
  self.bind.limitCountActive = false
  self.bind.active_iconHasGet = false
  self.bind.itemNameActive = false
  if self.bind.isFood then
    self.bind.contentActive = true
    self.bind.emptyActive = false
    local tpl = _foodGroupTpl:getTplById(self.bind.id)
    self.bind.commonIcon = _foodGroupTpl:getIcon(tpl)
    self.bind.icon_foodType = string.format("UI/Atlas/FoodType/tex_icon_foodtype_tag_0%s.png", _foodGroupTpl:getFoodType(tpl))
    self.bind.itemNameActive = true
    local foodGroupId = self.bind.id
    local foodGroupTpl = _foodGroupTpl:getTplById(foodGroupId)
    self.bind.itemName = _foodGroupTpl:getName(foodGroupTpl)
    local foodId = _productsMultiQualityTpl:getItemIdListByFoodGroupId(foodGroupId)[1]
    local commonItemTpl = _commonItemTpl:getTplById(foodId)
    local foodQuality = _commonItemTpl:getRarity(commonItemTpl)
    self.bind.iconBg = string.format("UI/Atlas/HomeProductBuilding/tex_homebuilding_bg_raritysqrtd_%s.png", foodQuality or 0)
    self:setStars(foodQuality + 1)
  elseif self.bind.itemData then
    self.bind.contentActive = true
    self.bind.emptyActive = true
    self.bind.commonIconActive = self.bind.itemData.itemType ~= L_Const.resType.homeBuilding
    self.bind.buildingIconActive = self.bind.itemData.itemType == L_Const.resType.homeBuilding
    local conf = L_ItemTplManager:getItemConfig(self.bind.itemData.itemType, self.bind.itemData.itemId)
    if self.bind.itemData.itemType == L_Const.resType.homeBuilding then
      self:refreshCellOfHomeBuildingType(conf)
    elseif self.bind.itemData.itemType == L_Const.resType.mountSaddle then
      self:refresCellOfMounSaddleType(conf)
    else
      self:refreshCellOfCommon(conf)
    end
  else
    self.bind.contentActive = false
    self.bind.emptyActive = true
  end
end

function this:refreshCellOfHomeBuildingType(conf)
  self.bind.buildingIcon = conf.icon
  local tpl_building = _homeBuildingTpl:getTplById(self.bind.itemData.itemId)
  local groupId = _homeBuildingTpl:getGroupId(tpl_building)
  local limitCnt = L_HomeStore:getBuildingLimitCount(groupId)
  local count = L_HomeStore:getCurNumByBuildingGroupId(groupId)
  if limitCnt == nil then
    self.bind.limitCountActive = false
    self.bind.itemNameActive = true
  else
    self.bind.limitCountActive = true
    self.bind.itemNameActive = false
    self.bind.limitText = string.format("%d/%d", count, limitCnt)
  end
end

function this:refresCellOfMounSaddleType(conf)
  self:refreshCellOfCommon(conf)
  local isHas = AzurWorld.mountMgr:GetIdMountSaddleUnlock(self.bind.itemData.itemId) == true
  self.bind.active_iconHasGet = isHas
  self.bind.limitCountActive = true
  self.bind.limitText = string.format("%d/%d", isHas and 1 or 0, 1)
  self.bind.itemNameActive = false
end

function this:refreshCellOfCommon(conf)
  self.bind.itemNameActive = true
  self.bind.commonIcon = conf.icon
  self.bind.iconBg = string.format("UI/Atlas/HomeProductBuilding/tex_homebuilding_bg_raritysqrtd_%s.png", conf.quality or 0)
  self:setStars(conf.quality + 1)
end

function this:setStars(num)
  if not self.isBind then
    return
  end
  local stars = {}
  for i = 1, num do
    table.insert(stars, {})
  end
  self.bind.starList:clear()
  self.bind.starList:insert_array(stars)
end

function this:checkNeedShowTraceTaskNodeByTraceProductId(traceProductId)
  if self.bind.productId then
    if self.bind.productId == traceProductId then
      return true
    end
  elseif self.bind.productIdList then
    for _, productId in ipairs(self.bind.productIdList) do
      if productId == traceProductId then
        return true
      end
    end
  end
  return false
end

function this:refreshTraceTaskNode(traceTaskId)
  if traceTaskId ~= nil then
    self.bind.active_taskNode = true
    local taskTpl = _taskTpl:getTplById(traceTaskId)
    local tabType = _taskTpl:getTabType(taskTpl)
    local taskTypeTpl = _taskTypeTpl:getTplById(tabType)
    self.bind.img_taskType = _taskTypeTpl:getIcon(taskTypeTpl)
    self.bind.color_taskTypeImg = _taskTypeTpl:getColor(taskTypeTpl)
  else
    self.bind.active_taskNode = false
  end
end

function this:hideTraceTaskNode()
  self.bind.active_taskNode = false
end

return this
