/*
    Credits.h


    -------------------------------
    Copyright (c) 2022-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.
*/

#ifndef CREDITS_H
#define CREDITS_H
#ifdef _WIN32
#include <irrlicht.h>
#else
#include <irrlicht/irrlicht.h>
#endif

#include <vector>
#include <fstream>
#include "Base.h"

PogoBijoux pb;
std::vector<irr::gui::IGUIStaticText*> credits;

extern "C"
{
    #include "Sound.h"
}


irr::u32 then;


void showCredits()
{
    irr::video::ITexture* bgimg = pb.driver -> getTexture("img/sky/night.jpg");
    irr::video::ITexture* bijouxLogo = pb.driver -> getTexture("img/gui/bijoux.png");

    irr::video::ITexture* info = pb.driver -> getTexture("img/gui/info.png");

    irr::gui::IGUIFont* font = pb.guiEnv -> getFont("img/gui/font.png");

    pb.guiSkin -> setFont(font);
    pb.guiSkin -> setColor(irr::gui::EGDC_BUTTON_TEXT, irr::video::SColor(255, 255, 255, 255));

    ALuint music = loadSound("audio/music/credits.ogg");
    playSound(0, 0, music);

    float verticalOffset = 0;
    then = pb.device -> getTimer() -> getTime();

    // Load credits into vector
    std::wstring creditsText;

    std::wifstream creditsFile("data/CREDITS");
    if(creditsFile.is_open())
    {
        std::wstring creditsLineText;
        while(getline(creditsFile, creditsLineText))
        {
            creditsText += L"\n" + creditsLineText;
        }
    }


    while(pb.device -> run() && !pb.er.isKeyDown(irr::KEY_ESCAPE))
    {
        // Compute frame delta time and then use it to animate the elements.
		const irr::u32 now = pb.device -> getTimer() -> getTime();
		const irr::f32 frameDeltaTime = (irr::f32)(now - then) / 32.0F;
		then = now;

        verticalOffset -= 2 * frameDeltaTime;

        pb.driver -> beginScene(true, true, irr::video::SColor(255, 113, 113, 133));
        pb.driver -> draw2DImage(bgimg, irr::core::rect<irr::s32>(0, 0, pb.screenWidth, pb.screenHeight), irr::core::rect<irr::s32>(0, 0, pb.screenWidth, pb.screenHeight));

        pb.guiEnv -> addImage(bijouxLogo, irr::core::position2d<irr::s32>(pb.screenWidth / 2 - 458 / 2, pb.screenHeight / 2 + (400 + verticalOffset) / 2));
        pb.guiEnv -> addImage(info, irr::core::position2d<irr::s32>(pb.screenWidth / 2 - 128 / 2, pb.screenHeight / 2 + (600 + verticalOffset) / 2));

        irr::gui::IGUIStaticText* credits = pb.guiEnv -> addStaticText(creditsText.c_str(), irr::core::rect<irr::s32>(pb.screenWidth / 2 - 312 / 2, pb.screenHeight / 2 + (800 + verticalOffset) / 2, pb.screenWidth, pb.screenHeight));
        credits -> setAlignment(irr::gui::EGUIA_UPPERLEFT, irr::gui::EGUIA_UPPERLEFT, irr::gui::EGUIA_LOWERRIGHT, irr::gui::EGUIA_LOWERRIGHT);

        pb.guiEnv -> drawAll();
        pb.guiEnv -> clear();
        pb.driver -> endScene();
    }
    pb.guiEnv -> clear();
}

#endif
