/*
    LevelSwitcher.h


    ------------------------------------
    Copyright (c) 2020-2023 MCL Software
    Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
    The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#include "Levels.h"

class LevelSwitcher
{
    public:
        int currentLevel;
        void nextLevel(bool calledFromConsensualLevel)
        {
            currentLevel++;

            pb.player -> removeAnimators();
            pb.scnmgr -> addToDeletionQueue(pb.rootNode);

            currentLevel = loadLevel(currentLevel);

            pb.showLoadingOverlay = false;
            pb.showNextLevelOverlay = false;
            pb.device -> getCursorControl() -> setPosition(pb.screenWidth / 2, pb.screenHeight / 2);
        }

        void proceedWithLevelSwitch()
        {
            if(flag)
            {
                flag = false;
                nextLevel(true);
            }
        }

        void playerCanProgress(bool calledFromConsensualLevel)
        {
            if(calledFromConsensualLevel)
            {
                pb.showNextLevelOverlay = true;
            }

            if((calledFromConsensualLevel && (pb.er.isKeyDown(irr::KEY_RETURN) || pb.er.isMouseDown())) || !calledFromConsensualLevel)
            {
                pb.showLoadingOverlay = true;
                flag = true;
            }
        }

        void checkIfPlayerShouldGoToNextLevel()
        {
            if(currentLevel == 1)
            {
                if(pb.player -> getAbsolutePosition().Y < -400)
                {
                    playerCanProgress(false);
                }
            }

            else if(currentLevel == 2)
            {
                irr::core::aabbox3d<irr::f32> aabb = irr::core::aabbox3d<irr::f32>(irr::core::vector3df(21481, 5, 20900), irr::core::vector3df(21481, 400, 21000));
                if(pb.isPlayerCollidingWithAABB(aabb))
                {
                    playerCanProgress(true);
                }

                else
                {
                    pb.showNextLevelOverlay = false;
                }
            }

            else if(currentLevel == 3)
            {
                irr::core::aabbox3d<irr::f32> aabb = irr::core::aabbox3d<irr::f32>(irr::core::vector3df(-7566, -250, -3042), irr::core::vector3df(-7530, 0, -2960));
                if(pb.isPlayerCollidingWithAABB(aabb))
                {
                    playerCanProgress(true);
                }

                else
                {
                    pb.showNextLevelOverlay = false;
                }
            }

            else if(currentLevel == 4)
            {
                if(pb.player -> getAbsolutePosition().Z < -232)
                {
                    playerCanProgress(true);
                }

                else
                {
                    pb.showNextLevelOverlay = false;
                }
            }

            else if(currentLevel == 5)
            {
                irr::core::aabbox3d<irr::f32> aabb = irr::core::aabbox3d<irr::f32>(irr::core::vector3df(167, -90, 774), irr::core::vector3df(187, 90, 794));
                if(pb.isPlayerCollidingWithAABB(aabb))
                {
                    playerCanProgress(true);
                }

                else
                {
                    pb.showNextLevelOverlay = false;
                }
            }

            else if(currentLevel == 6)
            {
                if(pb.player -> getAbsolutePosition().X < -310)
                {
                    playerCanProgress(true);
                }

                else
                {
                    pb.showNextLevelOverlay = false;
                }
            }

            else if(currentLevel == 7)
            {
                if(pb.player -> getAbsolutePosition().Z > 270)
                {
                    playerCanProgress(true);
                }

                else
                {
                    pb.showNextLevelOverlay = false;
                }
            }

            else if(currentLevel == 8)
            {
                irr::core::aabbox3d<irr::f32> aabb = irr::core::aabbox3d<irr::f32>(irr::core::vector3df(-182, -584, 69), irr::core::vector3df(-104, 0, 69));
                if(pb.isPlayerCollidingWithAABB(aabb))
                {
                    playerCanProgress(true);
                }

                else
                {
                    pb.showNextLevelOverlay = false;
                }
            }

            else if(currentLevel == 9)
            {
                irr::core::aabbox3d<irr::f32> aabb = irr::core::aabbox3d<irr::f32>(irr::core::vector3df(1587, 0, 27510), irr::core::vector3df(1768, 400, 27625));
                if(pb.isPlayerCollidingWithAABB(aabb))
                {
                    playerCanProgress(true);
                }

                else
                {
                    pb.showNextLevelOverlay = false;
                }
            }

            else if(currentLevel == 10)
            {
                irr::core::aabbox3d<irr::f32> aabb = irr::core::aabbox3d<irr::f32>(irr::core::vector3df(431, 0, 500), irr::core::vector3df(431, 400, 660));
                if(pb.isPlayerCollidingWithAABB(aabb))
                {
                    playerCanProgress(true);
                }

                else
                {
                    pb.showNextLevelOverlay = false;
                }
            }

            else if(currentLevel == 11)
            {
                irr::core::aabbox3d<irr::f32> aabb = irr::core::aabbox3d<irr::f32>(irr::core::vector3df(-287, 0, -108), irr::core::vector3df(-287, 400, 10));
                if(pb.isPlayerCollidingWithAABB(aabb))
                {
                    playerCanProgress(true);
                }

                else
                {
                    pb.showNextLevelOverlay = false;
                }
            }

            else if(currentLevel == 12)
            {
                irr::core::aabbox3d<irr::f32> aabb = irr::core::aabbox3d<irr::f32>(irr::core::vector3df(-1114, 0, -64346), irr::core::vector3df(-1075, 100, -64300));
                if(pb.isPlayerCollidingWithAABB(aabb))
                {
                    playerCanProgress(true);
                }

                else
                {
                    pb.showNextLevelOverlay = false;
                }
            }

            else if(currentLevel == 13)
            {
                irr::core::aabbox3d<irr::f32> aabb = irr::core::aabbox3d<irr::f32>(irr::core::vector3df(-9652, 0, -6533), irr::core::vector3df(-9600, 800, -6433));
                if(pb.isPlayerCollidingWithAABB(aabb))
                {
                    playerCanProgress(true);
                }

                else
                {
                    pb.showNextLevelOverlay = false;
                }
            }

            else if(currentLevel == 14)
            {
                if(pb.player -> getAbsolutePosition().Y < -400)
                {
                    playerCanProgress(false);
                }
            }

            else if(currentLevel == 15)
            {
                irr::core::aabbox3d<irr::f32> aabb = irr::core::aabbox3d<irr::f32>(irr::core::vector3df(-778, 0, -20), irr::core::vector3df(-817, 400, 215));
                if(pb.isPlayerCollidingWithAABB(aabb))
                {
                    playerCanProgress(false);
                }

                else
                {
                    pb.showNextLevelOverlay = false;
                }
            }

            else if(currentLevel == 16)
            {
                irr::core::aabbox3d<irr::f32> aabb = irr::core::aabbox3d<irr::f32>(irr::core::vector3df(-210, 0, -420), irr::core::vector3df(-205, 400, -415));
                if(pb.isPlayerCollidingWithAABB(aabb))
                {
                    playerCanProgress(true);
                }

                else
                {
                    pb.showNextLevelOverlay = false;
                }
            }

            else if(currentLevel == 17)
            {
                if(pb.player -> getAbsolutePosition().Z < 0)
                {
                    playerCanProgress(false);
                }
            }

            else if(currentLevel == 18)
            {
                if(pb.player -> getAbsolutePosition().Y < -1899)
                {
                    playerCanProgress(false);
                }
            }

            else if(currentLevel == 19)
            {
                if(pb.player -> getAbsolutePosition().X > 2000)
                {
                    playerCanProgress(false);
                }
            }

            else if(currentLevel == 20)
            {
                irr::core::aabbox3d<irr::f32> aabb = irr::core::aabbox3d<irr::f32>(irr::core::vector3df(2609, -100, 2823), irr::core::vector3df(2700, 400, 2900));
                if(pb.isPlayerCollidingWithAABB(aabb))
                {
                    playerCanProgress(true);
                }

                else
                {
                    pb.showNextLevelOverlay = false;
                }
            }

            else if(currentLevel == 21)
            {
                if(pb.player -> getAbsolutePosition().X > 1404 && pb.player -> getAbsolutePosition().Z < -1117)
                {
                    playerCanProgress(true);
                }

                else
                {
                    pb.showNextLevelOverlay = false;
                }
            }

            else if(currentLevel == 22)
            {
                irr::core::aabbox3d<irr::f32> aabb = irr::core::aabbox3d<irr::f32>(irr::core::vector3df(-390, 80, -200), irr::core::vector3df(-390, 130, -166));
                if(pb.isPlayerCollidingWithAABB(aabb))
                {
                    playerCanProgress(true);
                }

                else
                {
                    pb.showNextLevelOverlay = false;
                }
            }

            else if(currentLevel == 23)
            {
                irr::core::aabbox3d<irr::f32> aabb = irr::core::aabbox3d<irr::f32>(irr::core::vector3df(688, -80, 476), irr::core::vector3df(1416, 130, 1322));
                if(pb.isPlayerCollidingWithAABB(aabb))
                {
                    playerCanProgress(true);
                }

                else
                {
                    pb.showNextLevelOverlay = false;
                }
            }

            else if(currentLevel == 24)
            {
                irr::core::aabbox3d<irr::f32> aabb = irr::core::aabbox3d<irr::f32>(irr::core::vector3df(-249, -120, -118), irr::core::vector3df(233, 290, 69));
                if(pb.isPlayerCollidingWithAABB(aabb))
                {
                    playerCanProgress(true);
                }

                else
                {
                    pb.showNextLevelOverlay = false;
                }
            }

            else if(currentLevel == 25)
            {
                if(pb.player -> getAbsolutePosition().Z > 1780)
                {
                    pb.player -> removeAnimators();
                    pb.scnmgr -> addToDeletionQueue(pb.rootNode);
                    showCredits();
                    pb.device -> closeDevice();
                }
            }

            else
            {
                irr::core::aabbox3d<irr::f32> aabb = irr::core::aabbox3d<irr::f32>(irr::core::vector3df(432, 0, -300), irr::core::vector3df(500, 300, 466));
                if(pb.isPlayerCollidingWithAABB(aabb))
                {
                    playerCanProgress(false);
                }
            }
        }

    private:
        bool flag = false;
};

