-----------------------------------------------------------------------------------------
-- Start Activity
-----------------------------------------------------------------------------------------

function TutorialMission:StartActivity()
    print("START! -- TutorialMission:StartActivity()!");
    
    self.FightStage = { NOFIGHT = 0, DEFENDING = 1, ATTACK = 2, FIGHTSTAGECOUNT = 3 };

    self.AreaTimer = Timer();
    self.StepTimer = Timer();
    self.ScreenChange = false;

    self.CurrentFightStage = self.FightStage.NOFIGHT;
    self.CPUBrain = nil;

    --------------------------
    -- Set up teams

    -- Team 2 is always CPU
    self.CPUTeam = Activity.TEAM_2;

    for team = 0, self.TeamCount - 1 do
        if (team == self.CPUTeam) and (self.CPUBrain == nil) then
-- TODO: Make special CPU brain actor which will only appear in CPU brain fights, and have to be placed in the scenes
            self.CPUBrain = MovableMan:GetUnassignedBrain(self.CPUTeam);
            if not self.CPUBrain then
                -- Couldn't find an available brain in the scene, so make one and place it in a reasonable position
                this.CPUBrain = CreateActor("Brain Case", "Base.rte");
                if this.CPUBrain then
                    local brainPos = Vector();
                    otherBrain = MovableMan:GetClosestBrainActor(self:OtherTeam(team));
                    if otherBrain then
                        brainPos = otherBrain.Pos;
                        brainPos.X = brainPos.X + SceneMan.SceneWidth / 2;
                        brainPos.Y = brainPos.Y + SceneMan.SceneHeight - 200;
                        SceneMan:WrapPosition(brainPos);
                    -- No brain found on other team - then just place somewhere in the ground, spaced out
                    else
                        if team == Activity.TEAM_1 then
                            brainPos:SetXY(SceneMan.SceneWidth * 0.25, SceneMan.SceneHeight * 0.75);
                        end
                        if team == Activity.TEAM_2 then
                            brainPos:SetXY(SceneMan.SceneWidth * 0.75, SceneMan.SceneHeight * 0.75);
                        end
                    end
                    self.CPUBrain.Pos = brainPos;
                    self.CPUBrain.Team = team;
                    -- Add the new CPU brain to the scene
                    MovableMan:AddActor(self.CPUBrain); 
                    otherBrain = nil;
                end
            end
        end
    end
    
    --------------------------
    -- Set up players
    
    for player = 0, self.PlayerCount - 1 do
        -- If we can't find an unassigned brain in the scene to give each player, then force to go into editing mode to place one
        local foundBrain = MovableMan:GetUnassignedBrain(self:GetTeamOfPlayer(player));
        if not foundBrain then
            print("Can't find player brain for tutorial game mode!")
        else
            -- Set the found brain to be the selected actor at start
            self:SetPlayerBrain(foundBrain, player);
            self:SwitchToActor(foundBrain, player, self:GetTeamOfPlayer(player));
            self:SetLandingZone(self:GetPlayerBrain(player).Pos, player);
            -- Set the observation target to the brain, so that if/when it dies, the view flies to it in observation mode
            self:SetObservationTarget(self:GetPlayerBrain(player).Pos, player)
        end
    end
    
    --------------------------
    -- Set up tutorial
    
	self.AreaTimer:Reset();
	self.StepTimer:Reset();
	
end

-----------------------------------------------------------------------------------------
-- Pause Activity
-----------------------------------------------------------------------------------------

function TutorialMission:PauseActivity(pause)
    print("PAUSE! -- TutorialMission:PauseActivity()!");
end

-----------------------------------------------------------------------------------------
-- End Activity
-----------------------------------------------------------------------------------------

function TutorialMission:EndActivity()
    print("END! -- TutorialMission:EndActivity()!");
end

-----------------------------------------------------------------------------------------
-- Update Activity
-----------------------------------------------------------------------------------------

function TutorialMission:UpdateActivity()
--    print("UPDATE! -- TutorialMission:Update()!");
    
    --------------------------
    -- Iterate through all human players

    for player = 0, self.PlayerCount - 1 do
        -- The current player's team
        local team = self:GetTeamOfPlayer(player);
        
        -- Make sure the game is not already ending
        if not (self.ActivityState == Activity.OVER) then
            -- Check if any player's brain is dead
            if not MovableMan:IsActor(self:GetPlayerBrain(player)) then
                self:SetPlayerBrain(nil, player);
                FrameMan:SetScreenText("Your brain has been destroyed!", player, 333, -1, false);

                -- Now see if all brains of this player's team are dead, and if so, end the game
                if not MovableMan:GetClosestBrainActor(team) then
                    self.WinnerTeam = self:OtherTeam(team);
                    ActivityMan:EndActivity();
                end

                self:ResetMessageTimer(player);
            end
        -- Game over, show the appropriate messages until a certain time
        elseif not self.GameOverTimer:IsPastSimMS(self.GameOverPeriod) then
-- TODO: make more appropriate messages here for run out of funds endings
            if team == self.WinnerTeam then
                FrameMan:SetScreenText("Your competition's wetware is mush!", player, 0, -1, false);
            else
                FrameMan:SetScreenText("Your brain has been destroyed!", player, 0, -1, false);
            end
        end
	end

    ------------------------------------------------
    -- Iterate through all teams

    for team = 0, self.TeamCount - 1 do
    
        -------------------------------------------
        -- Check for victory conditions
       
       -- Make sure the game isn't already ending
       if (not (self.ActivityState == Activity.OVER)) and (not (team == self.CPUTeam)) then
       
       end
    end
    
    --------------------------------------------
    -- Battle logic
    
    if self:GetControlledActor(Activity.PLAYER_1) then
        -- Triggered defending stage
        if self.CurrentFightStage == self.FightStage.NOFIGHT and SceneMan.Scene:WithinArea("AIAttack", self:GetControlledActor(Activity.PLAYER_1).Pos) then
            -- Take over control of screen messages
            self:ResetMessageTimer(Activity.PLAYER_1);
            -- Display the text of the current step
            FrameMan:ClearScreenText(Activity.PLAYER_1);
            FrameMan:SetScreenText("DEFEND YOUR BRAIN AGAINST THE INCOMING FORCES!", Activity.PLAYER_1, 500, 8000, true);
            -- This will make all the enemy team AI's go into brain hunt mode
            for actor in MovableMan.Actors do
                if actor.Team == self.CPUTeam then
                    actor:SetControllerMode(Controller.CIM_AI, -1);
                    actor.AIMode = Actor.AIMODE_BRAINHUNT;
                end
            end
            
            -- Advance the stage
            self.CurrentFightStage = self.FightStage.DEFENDING;
        end
    end
    
    -----------------------------------------------------
    -- Check for victory conditions
    
    -- Check if the CPU brain is dead
    if not MovableMan:IsActor(self.CPUBrain) and ActivityMan:ActivityRunning() then
        self.CPUBrain = nil;
        -- Proclaim player winner end
        self.WinnerTeam = Activity.TEAM_1
        -- Finito!
        ActivityMan:EndActivity();
    end
end