local this = L_BevTree:registClass("guide.changeMainControl", L_BevTree.action)

function this:param()
  self.useDefaultHero = 0
  self.heroId = ""
end

function this:parse()
  self.useDefaultHero = self:getParam("useDefaultHero", true)
  self.heroId = self:getParam("heroId", true)
end

function this:execute()
  local heroId
  if not math.isEmpty(self.useDefaultHero) then
    heroId = L_GameUtil.getDefaultHeroId()
  else
    heroId = self.heroId
  end
  if math.isEmpty(heroId) then
    errorf("\230\150\176\230\137\139\229\188\149\229\175\188changeMainControl\232\138\130\231\130\185\230\156\170\233\133\141\231\189\174\230\173\163\231\161\174", 2)
    return L_BevTree.taskResult.Success
  end
  if self.result == L_BevTree.taskResult.Success then
    return self.result
  end
  self.result = L_BevTree.taskResult.Running
  local curForType = L_FormationManager:getCurFormationType()
  local posData = L_FormationStore:getFormationPosData(curForType)
  local heroGuid, index
  for i, guid in pairs(posData) do
    if not math.isEmpty(guid) then
      local hero = L_HeroStore:getHero(guid)
      if hero and L_HeroStore:getHeroConfigId(hero) == heroId then
        heroGuid = guid
        index = i
      end
    end
  end
  if index == AzurWorld.playerMgr:GetMainFormationHeroIndex(L_PlayerStore:getPlayerId()) + 1 then
    self.result = L_BevTree.taskResult.Success
    return self.result
  end
  if not math.isEmpty(index) then
    L_FormationManager:switchMyPlayerMainControl(index - 1, true)
  end
  self.result = L_BevTree.taskResult.Success
  return self.result
end

return this
