local this = class("moduleProductInfoDesc_normal", G_UIModuleBase)
local _productsTpl = L_GameTpl:getProductsTpl()
local _homeBuildingTpl = L_GameTpl:getHomeBuildingTpl()
local _homeBuildingProductionTpl = L_GameTpl:getHomeBuildingProductionTpl()
local _homeBuildingGroupTpl = L_GameTpl:getHomeBuildingGroupTpl()
local _homeLaborCapacityTpl = L_GameTpl:getHomeLaborCapacityTpl()
local _buildingTypeTpl = L_GameTpl:getHomeBuildingTypeTpl()
local _mountSaddleTpl = L_GameTpl:getMountSaddleTpl()
local _commonItemTpl = L_GameTpl:getCommonItemTpl()
local _foodTpl = L_GameTpl:getFoodTpl()

function this.bind()
  return {
    active_itemDesc = true,
    txt_itemDesc = "",
    active_topItemDesc = false,
    txt_topItemDesc = "",
    active_bottomItemDesc = false,
    txt_bottomItemDesc = "",
    txt_longDesc = "",
    rect_longDesc = C_Vector2(0, 0),
    txt_limitDesc = "",
    active_descLayout = false,
    active_moduleNum = false,
    module_num = L_Const.ModuleInfo.SelectNum,
    txt_time = "",
    active_startBtn_New = true,
    active_trans_canNotStart = false,
    text_canNotStart = ""
  }
end

function this.methods()
  return {}
end

function this:open()
  function self._refreshSelectNum()
    if self.UIData == nil or self.UIData.moduleProductInfo.moduleData.productInfoDescType ~= L_HomeConst.ProductInfoDescType.Normal then
      return
    end
    self:initSelectNum()
  end
  
  L_ProductStore:listenCallFunc(L_ProductStore.event.refreshHomeProduct, self._refreshSelectNum)
  L_PetStore:listenCallFunc(L_PetStore.event.refreshPet, self._refreshSelectNum, self)
end

function this:close()
  L_ProductStore:unListenCallFunc(L_ProductStore.event.refreshHomeProduct, self._refreshSelectNum)
  L_PetStore:unListenCallFunc(L_PetStore.event.refreshPet, self._refreshSelectNum)
end

function this:setData(productId, buildingGuid, moduleProductInfo)
  self.UIData = self.UIData or {}
  self.UIData.lastProductId = self.UIData.productId
  self.UIData.productId = productId
  self.UIData.selectNum = self.UIData.lastProductId == self.UIData.productId and self.UIData.selectNum or 1
  self.UIData.lastNumMax = self.UIData.lastProductId == self.UIData.productId and self.UIData.lastNumMax or 1
  self.UIData.buildingGuid = buildingGuid
  self.UIData.moduleProductInfo = moduleProductInfo
end

function this:refreshUI()
  local tpl = _productsTpl:getTplById(self.UIData.productId)
  local reward = L_DataUtil.parseRewardConfigItem(_productsTpl:getRewardId(tpl)[1])
  local item = L_ItemTplManager:getItemConfig(reward.itemType, reward.itemId)
  self.bind.active_itemDesc = true
  self.bind.active_topItemDesc = false
  self.bind.active_bottomItemDesc = false
  self:emit("onShowFoodItemDesc", false)
  self.bindComponents.rect_itemDesPart.anchoredPosition = C_Vector2(self.bindComponents.rect_itemDesPart.anchoredPosition.x, -402)
  self.bindComponents.rect_itemDesPart.sizeDelta = C_Vector2(self.bindComponents.rect_itemDesPart.sizeDelta.x, 384)
  self:restBtnState()
  if item.itemType == L_Const.resType.homeBuilding then
    self.bind.txt_itemDesc = ""
    self.bind.txt_longDesc = item.desc
  elseif item.itemType == L_Const.resType.collectionTool then
    local td = CS.Azur.Gameplay.Table.TDCollectionToolTable.GetData(reward.itemId)
    self.bind.txt_itemDesc = td.desc.value
    self.bind.txt_longDesc = td.specialDesc.value
  elseif item.itemType == L_Const.resType.mountSaddle then
    local itemTpl = _mountSaddleTpl:getTplById(reward.itemId)
    self.bind.txt_itemDesc = item.desc
    self.bind.txt_longDesc = _mountSaddleTpl:getSpecialDesc(itemTpl)
    local isInProduction = false
    if self.UIData.buildingGuid == 0 then
      isInProduction = L_ProductManager:onHadInFastProduct()
    else
      local queueData = L_ProductManager:getProduceQueueForBuilding(self.UIData.buildingGuid)
      for _, queueItemData in pairs(queueData) do
        if queueItemData.isEmpty == false and queueItemData.product and queueItemData.product.product_id then
          local productsTpl = _productsTpl:getTplById(queueItemData.product.product_id)
          local rewardItemIdInQueue = _productsTpl:getRewardId(productsTpl)[1][2]
          if rewardItemIdInQueue == item.itemId then
            isInProduction = true
          end
        end
      end
    end
    local isHas = AzurWorld.mountMgr:GetIdMountSaddleUnlock(item.itemId) == true
    self.bind.active_startBtn_New = isInProduction == false and isHas == false
    self.bind.active_trans_canNotStart = isInProduction or isHas
    if isInProduction then
      self.bind.text_canNotStart = L_Lang:get(L_WordsTpl:getValue("notice_home_product_willreachlimit"))
    elseif isHas then
      self.bind.text_canNotStart = L_Lang:get(L_WordsTpl:getValue("tag_home_product_reachlimit"))
    end
  elseif item.itemType == L_Const.resType.accessory then
    self.bind.txt_itemDesc = ""
    self.bind.txt_longDesc = item.desc
  else
    local itemTpl = _commonItemTpl:getTplById(reward.itemId)
    local itemType = _commonItemTpl:getType(itemTpl)
    local isFood = itemType == L_BagConst.bagItemType.food
    self.bind.active_itemDesc = not isFood
    self.bind.active_topItemDesc = isFood
    self.bind.active_bottomItemDesc = isFood
    if isFood then
      local foodTpl = _foodTpl:getTplById(reward.itemId)
      local satiety = _foodTpl:getSatietyScore(foodTpl)
      self.bind.txt_topItemDesc = string.concat(L_WordsTpl:getValue("ui_pet_food_desc"), satiety)
      self.bind.txt_bottomItemDesc = item.desc
      self:emit("onShowFoodItemDesc", true)
      self.bindComponents.rect_itemDesPart.anchoredPosition = C_Vector2(self.bindComponents.rect_itemDesPart.anchoredPosition.x, -386)
      self.bindComponents.rect_itemDesPart.sizeDelta = C_Vector2(self.bindComponents.rect_itemDesPart.sizeDelta.x, 406)
    else
      self.bind.txt_itemDesc = item.desc
    end
    if itemTpl ~= nil then
      self.bind.txt_longDesc = _commonItemTpl:getSpecialDesc(itemTpl)
    else
      self.bind.txt_longDesc = ""
    end
  end
  self.bind.rect_longDesc = C_Vector2(0, 0)
  self:initSelectNum()
end

function this:restBtnState()
  self.bind.active_startBtn_New = true
  self.bind.active_trans_canNotStart = false
end

function this:initSelectNum()
  if self.UIData.productId == nil or self.UIData.productId <= 0 then
    return
  end
  local tpl = _productsTpl:getTplById(self.UIData.productId)
  local limit = _productsTpl:getNumberLimit(tpl)
  self.bind.active_moduleNum = 1 < limit
  if self.UIData.selectNum and self.UIData.lastProductId == self.UIData.productId then
    self.UIData.selectNum = limit and math.min(limit, self.UIData.selectNum) or self.UIData.selectNum
  else
    self.UIData.selectNum = 1
  end
  self:refreshCostInfo()
  if self.bind.active_moduleNum then
    local maxCanDo = self:getMaxCanDo()
    local max = math.min(maxCanDo, limit)
    if self.UIData.lastProductId == self.UIData.productId and self.UIData.lastNumMax == max then
      return
    end
    self.UIData.lastProductId = self.UIData.productId
    self.UIData.lastNumMax = max
    local func = function(num)
      self.UIData.selectNum = num
      self:refreshCostInfo()
    end
    self.modules.module_num:initModule(func, math.max(1, max), 1, self.UIData.selectNum)
  end
end

function this:refreshCostInfo()
  local protectProcessProductTime = L_GameConstTpl:getValue("HOME_PROCESS_PRODUCT_TIME")
  if self.UIData.buildingGuid == 0 then
    local onceTime = L_ProductManager:getFastProduceOnceTime(self.UIData.productId)
    if protectProcessProductTime > onceTime then
      onceTime = protectProcessProductTime
    end
    local allCostTime = math.ceil(onceTime * self.UIData.selectNum)
    self.bind.txt_time = string.format("%s", L_TimeUtil.secondToLangString(allCostTime))
    self:setSelectNum(self.UIData.selectNum)
  else
    self:setSelectNum(self.UIData.selectNum)
    local onceTime = L_ProductManager:getProduceOnceTime(self.UIData.buildingGuid, self.UIData.productId)
    if protectProcessProductTime > onceTime then
      onceTime = protectProcessProductTime
    end
    local allCostTime = math.ceil(onceTime * self.UIData.selectNum)
    self.bind.txt_time = string.format("%s", L_TimeUtil.secondToLangString(allCostTime))
  end
end

function this:getMaxCanDo()
  return self.UIData.moduleProductInfo:getNormalProductMaxCanDo()
end

function this:setSelectNum(selectNum)
  self.UIData.moduleProductInfo:setNormalProductSelectNum(selectNum)
end

function this:showSelf()
end

function this:hideSelf()
end

function this:getProductId()
  return self.UIData.productId
end

function this:getSelectNum()
  return self.UIData.selectNum
end

return this
