local this = class("modulePlow", G_UIModuleBase)
local _homeBuildingNumTpl = L_GameTpl:getHomeBuildingNumTpl()
local _homeBuildingTpl = L_GameTpl:getHomeBuildingTpl()
local _commonConditionTpl = L_GameTpl:getCommonConditionTpl()

function this.bind()
  return {
    active_pc = true,
    label_plowNum = "",
    active_label_plowNum = true,
    active_self = false,
    active_plow = true,
    active_retireFarmLand = false,
    active_phone = false,
    active_btnPlow = true,
    txt_plowNum = "",
    active_btnRetireFarmLand = false
  }
end

function this.methods()
  return {
    onClick_plow = function(self)
      self:onPressKeyCodeE()
    end,
    onClick_retireFarmLand = function(self)
      self:onPressKeyCodeR()
    end
  }
end

function this:open(...)
  this.super.open(self, ...)
  if not L_DeviceTpl:getIsPc() then
    self.bind.active_pc = false
    self.bind.active_phone = true
  end
  self.homeBuildingTplOfPlow = _homeBuildingTpl:getTplById(L_HomeConst.HomeBuildIdOfPlow)
  self.homeBuildingGroupIdOfPlow = _homeBuildingTpl:getGroupId(self.homeBuildingTplOfPlow)
  self.csHomeManager = AzurWorld.HomeMgr
  self.csHomeStore = self.csHomeManager:GetHomeStore()
  L_ShortCutManager:addShortKeyFlag(self, "E", L_Const.AnchorType.Bottom, self.bindComponents.rectPlow, L_Vector3.new(0, -10))
  L_ShortCutManager:addShortKeyFlag(self, "R", L_Const.AnchorType.Bottom, self.bindComponents.rectRetireFarmLand, L_Vector3.new(0, -10))
  L_ShortCutManager:addListener(L_ShortCutConst.event.KeyCodeE, self.onPressKeyCodeE, self)
  L_ShortCutManager:addListener(L_ShortCutConst.event.KeyCodeR, self.onPressKeyCodeR, self)
  self.onHomeBuildSyncHandler = handler(self, self.onHomeBuildSync)
  self.csHomeStore:RegisterEvent(C_EHomeStoreEvent.HomeBuildSync, self.onHomeBuildSyncHandler)
  self:registerConditionsOfUnlockMaxPlowBuildNum()
  L_HomeManager:addListener(L_HomeManager.event.triggerBuilding, self.onCurrInteractBuildGuidChanged, self)
  self:refreshLabelPlowNum()
  self.updateHandle = handler(self, self.update)
  C_UpdateSource.AddUpdateEventHandler(self.updateHandle, "modulePlow.update")
end

function this:update()
  self.oldActiveSelf = self.bind.active_self
  local active
  local curHomeMainMode = self.csHomeManager.curHomeMainMode:GetHashCode()
  if curHomeMainMode == L_HomeConst.homeMainMode.CROP or curHomeMainMode == L_HomeConst.homeMainMode.BUILD then
    active = true
  else
    active = false
  end
  if active == true and self.homeBuildingGroupIdOfPlow then
    local curMaxCanBuildNum = self.curMaxCanBuildNum
    active = 0 < curMaxCanBuildNum and true or false
  end
  self.bind.active_self = active
  if self.oldActiveSelf ~= self.bind.active_self then
    L_BattleDataManager:sendEvent(L_BattleDataManager.event.refreshPageMainLeftBottomLayout)
  end
end

function this:close(...)
  this.super.close(self, ...)
  C_UpdateSource.RemoveUpdateEventHandler(self.updateHandle)
  L_ShortCutManager:clearShortKeyFlag(self)
  L_ShortCutManager:removeListener(L_ShortCutConst.event.KeyCodeE, self.onPressKeyCodeE)
  L_ShortCutManager:removeListener(L_ShortCutConst.event.KeyCodeR, self.onPressKeyCodeR)
  self.csHomeStore:UnregisterEvent(C_EHomeStoreEvent.HomeBuildSync, self.onHomeBuildSyncHandler)
  self:unregisterConditionsOfUnlockMaxPlowBuildNum()
  L_HomeManager:removeListener(L_HomeManager.event.triggerBuilding, self.onCurrInteractBuildGuidChanged)
end

function this:hide()
  self:hideModules()
end

function this:activate()
  self:open()
  self:openModules()
  self:show()
  self:showModules()
end

function this:deActivate()
  self:hide()
  self:hideModules()
  self:close()
  self:closeModules()
  L_BattleDataManager:sendEvent(L_BattleDataManager.event.refreshPageMainLeftBottomLayout)
end

function this:registerConditionsOfUnlockMaxPlowBuildNum()
  self.conditionNodeList = {}
  for _, configId in pairs(_homeBuildingNumTpl:getConfigIdListByBuildingGroupId(self.homeBuildingGroupIdOfPlow)) do
    local homeBuildingNumTpl = _homeBuildingNumTpl:getTplById(configId)
    local unlockConditionGroup = _homeBuildingNumTpl:getUnlockConditionGroup(homeBuildingNumTpl)
    local conditionNode = L_ConditionManager:registGroup("modulePlow", unlockConditionGroup, self.onConditionGroupNodeStateChanged, self)
    if conditionNode ~= nil then
      table.insert(self.conditionNodeList, conditionNode)
    end
  end
end

function this:unregisterConditionsOfUnlockMaxPlowBuildNum()
  for _, conditionNode in pairs(self.conditionNodeList) do
    L_ConditionManager:unRegist(conditionNode, "modulePlow")
  end
end

function this:tryInvokeRetireFarmland()
  local buildingEntity = self.csHomeManager:GetHomeBuild(self.csHomeManager.currInteractBuildGuid)
  local C_homeBuildData = buildingEntity:GetStoreData()
  local C_homeBuildCropData = C_homeBuildData.crop
  local homeCropState = C_homeBuildCropData:GetStateInt()
  if homeCropState ~= L_HomeConst.HomeCropState.NONE then
    L_GameUtil.showCommonTip({
      txtContent = L_Lang:get(L_WordsTpl:getValue("notice_home_hoe_reset_confirm")),
      confirmCallback = function()
        self.csHomeManager:TryInvokeRetireFarmland()
      end
    })
  else
    self.csHomeManager:TryInvokeRetireFarmland()
  end
end

function this:tryInvokePlow()
  local curMaxCanBuildNum = _homeBuildingNumTpl:getCurMaxCanBuildNumByBuildingGroupId(self.homeBuildingGroupIdOfPlow)
  local buildNumOfPlow = self.csHomeStore:GetBuildNumOfPlow()
  if curMaxCanBuildNum > buildNumOfPlow then
    self.csHomeManager:TryInvokePlow()
  else
    local hasImproveSpace = false
    local targetConditionNode
    for _, conditionNode in pairs(self.conditionNodeList) do
      if not conditionNode:isComplete() then
        hasImproveSpace = true
        targetConditionNode = conditionNode
        break
      end
    end
    if hasImproveSpace then
      local targetCondition = targetConditionNode:getCondition()
      local commonConditionConfigId = targetCondition[1]
      local conditionParam = targetCondition[2]
      local commonConditionTpl = _commonConditionTpl:getTplById(commonConditionConfigId)
      local conditionDescShowKey = _commonConditionTpl:getDescShowKey(commonConditionTpl)
      local conditionNodeCls = L_ConditionManager:getCls(commonConditionConfigId)
      local conditionDescShowKeyWithConditionParam = conditionNodeCls.descFunc and conditionNodeCls.descFunc(conditionDescShowKey, targetCondition) or conditionDescShowKey .. tostring(conditionParam)
      L_FlyMsgManager:showNormalMsgByKey("notice_home_hoe_nextlimit", {
        [0] = conditionDescShowKeyWithConditionParam
      })
    else
      L_FlyMsgManager:showNormalMsg(L_Lang:get(L_WordsTpl:getValue("notice_home_hoe_reachlimit")))
    end
  end
end

function this:onPressKeyCodeE()
  if self.bind.active_self and self.bind.active_plow then
    self:tryInvokePlow()
  end
end

function this:onPressKeyCodeR()
  if self.bind.active_self and self.bind.active_retireFarmLand then
    self:tryInvokeRetireFarmland()
  end
end

function this:onCurrInteractBuildGuidChanged()
  local currInteractBuildGuid = self.csHomeManager.currInteractBuildGuid
  if currInteractBuildGuid ~= 0 then
    local baseHomeBuild = self.csHomeManager:GetHomeBuild(currInteractBuildGuid)
    if baseHomeBuild then
      local homeBuildId = baseHomeBuild:GetHomeBuildId()
      if homeBuildId == L_HomeConst.HomeBuildIdOfPlow then
        self.bind.active_plow = false
        self.bind.active_label_plowNum = false
        self.bind.active_btnPlow = false
        if L_SystemUnlockStore:getSystemIsOpen(L_SystemConst.enum.retireFarmLand) then
          self.bind.active_retireFarmLand = true
          self.bind.active_btnRetireFarmLand = true
        else
          self.bind.active_retireFarmLand = false
          self.bind.active_btnRetireFarmLand = false
        end
        return
      end
    end
  end
  self.bind.active_plow = true
  self.bind.active_label_plowNum = true
  self.bind.active_retireFarmLand = false
  self.bind.active_btnPlow = true
  self.bind.active_btnRetireFarmLand = false
end

function this:onHomeBuildSync()
  self:refreshLabelPlowNum()
end

function this:onConditionGroupNodeStateChanged()
  self:refreshLabelPlowNum()
end

function this:refreshLabelPlowNum()
  local curMaxCanBuildNum = _homeBuildingNumTpl:getCurMaxCanBuildNumByBuildingGroupId(self.homeBuildingGroupIdOfPlow)
  self.curMaxCanBuildNum = curMaxCanBuildNum
  local buildNumOfPlow = self.csHomeStore:GetBuildNumOfPlow()
  if curMaxCanBuildNum == buildNumOfPlow then
    self.bind.label_plowNum = L_GameUtil.fillColor(string.format("%d/%d", buildNumOfPlow, curMaxCanBuildNum), L_Const.colorHtml.red003)
    self.bind.txt_plowNum = L_GameUtil.fillColor(string.format("%d/%d", buildNumOfPlow, curMaxCanBuildNum), L_Const.colorHtml.red003)
    return
  end
  self.bind.label_plowNum = string.format("%d/%d", buildNumOfPlow, curMaxCanBuildNum)
  self.bind.txt_plowNum = string.format("%d/%d", buildNumOfPlow, curMaxCanBuildNum)
end

return this
