local this = class("homeManager", G_EventManagerBase)
require("modules.world.home.homeConst")
this.event = {
  triggerBuilding = "triggerBuilding",
  cropPlayExp = "cropPlayExp"
}
local _homeObjTpl = L_GameTpl:getHomeObjTpl()
local _homeBuildingTpl = L_GameTpl:getHomeBuildingTpl()
local _homeBuildingGroupTpl = L_GameTpl:getHomeBuildingGroupTpl()
local _homeTileTpl = L_GameTpl:getHomeTileTpl()
local _homeBlockTpl = L_GameTpl:getHomeBlockTpl()
local _homeStageTpl = L_GameTpl:getHomeStageTpl()

function this:ctor()
  this.super.ctor(self)
  self._homeRoot = nil
  self._homePool = nil
  self._indexToBlockEntity = {}
  self._currIntractBuildId = 0
  self.selectedCropItemId = 0
  L_GameStore:listenCallFunc(L_GameStore.event.enterGame, self.registConditionNodeOfHomeStage, self)
end

function this:registConditionNodeOfHomeStage()
  local homeStage2UnlockCondition = _homeStageTpl:getHomeStage2UnlockCondition()
  for homeStage, unlockCondition in pairs(homeStage2UnlockCondition) do
    L_ConditionManager:regist(string.format("condition_%d", homeStage), unlockCondition, function(self, conditionState, ...)
      local C_HomeManager = AzurWorld.HomeMgr
      C_HomeManager:OnHomeStageUnlockConditionNodeStateChanged()
    end, self)
  end
end

function this:setCurrInteractId(id)
  self._currIntractBuildId = id
  self:sendEvent(L_HomeManager.event.triggerBuilding)
end

function this:getCurrInteractId()
  return self._currIntractBuildId
end

function this:setCropPlayExp(pos, num)
  self:sendEvent(L_HomeManager.event.cropPlayExp, {pos = pos, num = num})
end

function this:getBlockEntityByIndex(blockIndex)
  return self._indexToBlockEntity[blockIndex]
end

function this:setHomeRootActive(isActive)
  printf("\232\174\190\231\189\174\229\174\182\229\155\173\229\174\160\231\137\169", isActive)
  AzurWorld.HomeMgr:SetAllPetHide(not isActive, "luaHomeManager")
  AzurWorld.HomeMgr:SetRootActive(isActive)
end

function this:_resetRoot()
  if L_CommonUtil.isValid(self._homeRoot.gameObject) then
    self._homeRoot.transform.position = L_Vector3.getTemp()
    self._homeRoot.transform:SetRelativeEulerAngles(L_Vector3.getTemp())
  end
  for _, block in pairs(self._indexToBlockEntity) do
    block:onResetRoot()
  end
end

function this:getBlockIndexByPlayer()
  local playerEntity = L_PlayerManager:getMainControlEntity()
  local pos = playerEntity and playerEntity:getWorldPosition()
  if not pos then
    return
  end
  return self:getBlockIndexByPlayerPos(pos)
end

function this:getBlockIndexByPlayerPos(pos, blockType)
  for i, v in pairs(self._indexToBlockEntity) do
    if not blockType or v.blockType == blockType then
      local playerPosInBlock = v.homeBlockArea.editArea:InverseTransformPoint(pos)
      local tpl = L_GameTpl:getHomeTileTpl():getTplById(i)
      playerPosInBlock = playerPosInBlock / L_GameTpl:getHomeTileTpl():getGridSize(tpl)
      local w = L_GameTpl:getHomeTileTpl():getWidth(tpl)
      local h = L_GameTpl:getHomeTileTpl():getHeight(tpl)
      if w > playerPosInBlock.x and h > playerPosInBlock.z and playerPosInBlock.x > 0 and playerPosInBlock.z > 0 then
        local gridId = L_HomeUtil.parsePosToGridId(playerPosInBlock.x, playerPosInBlock.z)
        local tpl = _homeTileTpl:getTplById(i)
        local banMap = _homeTileTpl:getBanTileList(tpl)
        if not table.containsValue(banMap or {}, gridId) then
          return i
        end
      end
    end
  end
  return nil, nil
end

function this:isInBuildBlock(pos)
  local index = L_HomeManager:getBlockIndexByPlayerPos(pos)
  local tpl = _homeBlockTpl:getTplById(index)
  return tpl and _homeBlockTpl:getBlockType(tpl) == L_HomeConst.blockType.BUILD or false
end

function this:homeSpawn(path)
  return self._homePool:spawn(path)
end

function this:homeDespawn(go)
  self._homePool:despawn(go)
end

function this:asyncSpawnById(id, callback)
  local tpl = _homeObjTpl:getTplById(id)
  local path = _homeObjTpl:getObjPath(tpl)
  self._homePool:asyncSpawn(path, callback)
end

function this:getBuildingEntityByGuid(guid)
  for i, v in pairs(self._indexToBlockEntity) do
    if v.buildPart and v.buildPart:getAllBuildingEntity()[guid] then
      return v.buildPart:getAllBuildingEntity()[guid]
    end
  end
end

function this:setEditMode(homeMode, editPart)
  self._homeMode = homeMode
  for i, v in pairs(self._indexToBlockEntity) do
    if v.blockType == L_HomeConst.blockType.BUILD then
      v.buildPart:showPlayerEdit(homeMode == L_Const.HomeEditorMode.playerEditor and editPart == v.editPart)
    end
  end
end

function this:getHomeMode()
  return self._homeMode
end

function this:getPlayerFaceGrid(offset)
  local mainControl = L_PlayerManager:getMainControlEntity()
  if not mainControl then
    return
  end
  local playerTrans = mainControl:getTransform()
  local playerPos = mainControl:getWorldPosition()
  local dir = L_HomeUtil.getFormalDir(playerTrans.forward)
  local pos = playerPos + dir * offset
  local index = L_HomeManager:getBlockIndexByPlayerPos(pos)
  local blockEntity = self:getBlockEntityByIndex(index)
  if blockEntity then
    local outPos = blockEntity:getBlockPosByWorldPos(pos)
    return index, outPos, dir
  end
end

function this:getBuildTypeById(buildId)
  local tpl_building = _homeBuildingTpl:getTplById(buildId)
  local groupId = _homeBuildingTpl:getGroupId(tpl_building)
  local tpl_buildingGroup = _homeBuildingGroupTpl:getTplById(groupId)
  local buildingType = _homeBuildingGroupTpl:getType(tpl_buildingGroup)
  return buildingType
end

return this
