local this = class("moduleHomeRewardPanel", G_UIModuleBase)
local _HomeRanchTpl = L_GameTpl:getHomeRanchTpl()
local _homeBlockTpl = L_GameTpl:getHomeBlockTpl()
local RewardTab = {
  CAN_REWARD = 1,
  EMPTY = 2,
  LOCK = 3
}

function this.bind()
  return {
    active_harvestBagBg = true,
    active_rewardNumBg = true,
    rect_btnFold = C_Vector2(-718.7, -17.1),
    rotate_btnFold = C_Vector3.zero,
    txt_rewardNum = "",
    group_reward = -1,
    group_chest = -1,
    active_rewardList = true,
    list_reward = {
      moduleName = "pages/home/homeRanch/cellHomeRanchHarvestBag"
    }
  }
end

function this.methods()
  return {
    onClick_fold = function(self)
      if self.bind.active_harvestBagBg then
        self.bind.active_harvestBagBg = false
        self.bind.active_rewardList = false
        self.bind.rect_btnFold = C_Vector2(484.3, -17.1)
        self.bind.rotate_btnFold = C_Vector3(0, 0, 180)
      else
        self.bind.active_harvestBagBg = true
        self.bind.active_rewardList = true
        self.bind.rect_btnFold = C_Vector2(-718.7, -17.1)
        self.bind.rotate_btnFold = C_Vector3.zero
      end
    end,
    onClick_reward = function(self)
      if self._stationType == L_HomeConst.StationType.Ranch then
        local ranchData = L_HomeRanchStore:getBlockRanchData(self._blockId)
        if not ranchData or #ranchData.stores <= 0 then
          L_FlyMsgManager:showNormalMsgByKey("ui_pageHomeRanch_01")
          return
        end
        L_HomeRanchStore:req_getStoredRewards(self._blockId, function()
          self:refreshRanchData()
        end)
        return
      elseif self._stationType == L_HomeConst.StationType.Crop and 0 < self._autoData.rewards.Count then
        self._autoData:Req_GetReward(function()
          self:refreshCropData()
        end)
      end
    end,
    onClick_addSeed = function(self)
      self:emit("onClick_addSeed")
      if self._stationType == L_HomeConst.StationType.Ranch then
        return
      end
      L_UI:open("pageHomeCropBox", {
        buildingGuid = self._guid
      })
    end
  }
end

function this:open()
  L_HomeRanchStore:listenCallFunc(L_HomeRanchStore.event.refreshRanch, self.refreshRanchData, self)
end

function this:close()
  L_HomeRanchStore:unListenCallFunc(L_HomeRanchStore.event.refreshRanch, self.refreshRanchData, self)
  self._buildingData = nil
  self._autoData = nil
end

function this:initCropData(buildingGuid)
  self._stationType = L_HomeConst.StationType.Crop
  self._guid = buildingGuid
  self._buildingData = C_HomeUtil.GetBuildData(self._guid)
  self._autoData = self._buildingData.auto
  self:refreshCropData()
end

function this:refreshCropData()
  if self._stationType ~= L_HomeConst.StationType.Crop then
    return
  end
  self.bind.active_rewardNumBg = false
  local rewardList = self._autoData.rewards
  self.bind.group_reward = rewardList.Count > 0 and RewardTab.CAN_REWARD or RewardTab.EMPTY
  self.bind.group_chest = self.bind.group_reward
  local rewardBoxNum = rewardList.Count
  if 0 < rewardBoxNum and rewardBoxNum ~= #self.bind.list_reward then
    self.bind.list_reward:clear()
    self.bind.list_reward:insert_array(L_DataUtil.getEmptyModuleDatas(rewardBoxNum))
    for i = 1, #self.modules.list_reward do
      self.bind.list_reward:getItemCls(i):setItem(rewardList[i - 1])
    end
  end
end

function this:initRanchData(blockId)
  self._stationType = L_HomeConst.StationType.Ranch
  self._blockId = blockId
  self:refreshRanchData()
end

function this:refreshRanchData()
  if self._stationType ~= L_HomeConst.StationType.Ranch then
    return
  end
  self.bind.active_rewardNumBg = true
  local ranchData = L_HomeRanchStore:getBlockRanchData(self._blockId)
  local lv = ranchData and ranchData.lv or 1
  local ranchTpl = _HomeRanchTpl:getTplById(lv)
  local stores = ranchData and ranchData.stores or {}
  local listDatas = {}
  for i, store in ipairs(stores) do
    local stackNums = L_DataUtil.getItemStacks(store.itemtype, store.itemid, store.itemnum)
    for _, num in ipairs(stackNums) do
      table.insert(listDatas, {
        itemType = store.itemtype,
        itemId = store.itemid,
        itemNum = num
      })
    end
  end
  self.bind.group_reward = #listDatas ~= 0 and RewardTab.CAN_REWARD or math.isEmpty(ranchData.harvest_pet) and RewardTab.LOCK or RewardTab.EMPTY
  self.bind.group_chest = self.bind.group_reward
  local rewardBoxNum = #listDatas
  if #self.modules.list_reward ~= rewardBoxNum then
    self.bind.list_reward:clear()
    self.bind.list_reward:insert_array(L_DataUtil.getEmptyModuleDatas(rewardBoxNum))
  end
  self.bind.txt_rewardNum = string.format("<color=#4b5a2c>%s</color>/%s", #listDatas, _HomeRanchTpl:getStorageLimit(ranchTpl))
  for i, module in ipairs(self.modules.list_reward) do
    module:setItem(listDatas[i])
  end
end

return this
