local module = class("moduleHomeBagCropInfo", G_UIModuleBase)
local _homeSeedTpl = L_GameTpl:getHomeSeedsTpl()
local _dropTpl = L_GameTpl:getDropTpl()
local _homeDropTpl = L_GameTpl:getHomeDropTpl()
local _commonItemTpl = L_GameTpl:getCommonItemTpl()

function module.bind()
  return {
    txt_num = "0",
    txt_name = "",
    img_icon = "",
    img_iconFilter = "",
    img_color = "",
    txt_time = "",
    txt_reward = "",
    txt_water = "",
    txt_rewardHave = ""
  }
end

function module.methods()
  return {
    onClickPlant = function(self)
      local manager = AzurWorld.HomeMgr
      local guid = manager.currInteractBuildGuid
      local buildData = C_HomeUtil.GetBuildData(guid)
      local buildingEntity = manager:GetHomeBuild(guid)
      if buildData and buildData:GetGroupConfig().type == L_Const.WorldMapBuildType.WMBT_FIELD_NEW and buildData.crop.seedId == 0 then
        local seedTpl = _homeSeedTpl:getSeedTplByItem(self._seedItemId)
        if not seedTpl then
          errorf("\231\167\141\229\173\144\231\137\169\229\147\129\230\178\161\230\156\137\233\133\141\231\189\174\231\167\141\229\173\144id" .. self._seedItemId)
          return
        end
        local condition = _homeSeedTpl:getCondition(seedTpl)
        if L_ConditionManager:isComplete(condition) then
          L_UI:close("pageHomeBag", nil, function()
            buildingEntity:InvokePlant(self._seedItemId)
          end)
        else
          L_FlyMsgManager:showNormalMsg(L_ConditionManager:getTipDesc(condition))
        end
        return
      end
      L_FlyMsgManager:showNormalMsg(L_WordsTpl:getValue("ui_moduleHomeBagCropInfo_04"))
    end
  }
end

function module:setSeedId(seedItemId, isIn, forceAni)
  self._seedItemId = seedItemId
  local conf = L_ItemTplManager:getCommonItem(seedItemId)
  local seedTpl = _homeSeedTpl:getTplBySeedItemId(seedItemId)
  if not seedTpl then
    errorf("\231\167\141\229\173\144\231\137\169\229\147\129\230\178\161\230\156\137\233\133\141\231\189\174\231\167\141\229\173\144id" .. seedItemId)
    return
  end
  self.bind.txt_num = tostring(L_BagStore:getItemNum(seedItemId))
  self.bind.txt_name = conf.name
  self.bind.img_icon = conf.icon
  self.bind.img_color = string.format("Altas/HomeBag/tex_homewarehouse_bg_seedquality%d.png", conf.quality)
  local commonItemTpl = _commonItemTpl:getTplById(seedItemId)
  self.bind.img_iconFilter = commonItemTpl and _commonItemTpl:getFilterIcon(commonItemTpl) or ""
  local buffCondition = L_HomeBuffManager:getCropCondition(_homeSeedTpl:getId(seedTpl))
  local growTime, preHarTime = L_HomeBuffManager:getCropGrowParam(_homeSeedTpl:getId(seedTpl), buffCondition)
  local totalTime = growTime + preHarTime
  self.bind.txt_time = L_TimeUtil.secondToString(totalTime, L_TimeUtil.secondToStringShowType.TWO_UNIT)
  self.bind.txt_reward = _homeSeedTpl:getNumShow(seedTpl)
  self.bind.txt_water = L_WordsTpl:getValue("ui_moduleHomeBagCropInfo_03", {
    [0] = _homeSeedTpl:getNeedWater(seedTpl)
  })
  local dropId = _homeSeedTpl:getHomeDropId(seedTpl)
  local dropTpl = _homeDropTpl:getTplById(dropId[1])
  local rewardItemId = _homeDropTpl:getItemId(dropTpl)
  local rewardItemType = _homeDropTpl:getItemType(dropTpl)
  local rewardItemConf = L_ItemTplManager:getItemConfig(rewardItemType, rewardItemId[1])
  self.bind.txt_rewardHave = L_WordsTpl:getValue("ui_moduleHomeBagCropInfo_02", {
    [0] = rewardItemConf.name,
    [1] = L_BagStore:getItemNum(rewardItemId[1])
  })
  Unity.UI.LayoutRebuilder.ForceRebuildLayoutImmediate(self.bindComponents.cropTag)
end

return module
