// quantumde1 developed software, licensed under MIT license.
module graphics.engine;

import raylib;

//dlang imports
import std.stdio;
import std.math;
import std.file;
import std.array;
import std.string;
import std.conv;
import std.typecons;
import std.random;
import std.datetime;

//graphics
import graphics.playback;
import graphics.gamelogic;
import ui.menu;

//dialogs
import dialogs.dialogbox;

//scripting imports
import scripts.lua;

//engine internal functions
import system.config;
import system.abstraction;
import variables;
import system.cleanup;

//C bindings
import core.stdc.stdlib;
import core.stdc.time;

void engine_loader()
{
    int screenWidth = 1344;
    int screenHeight = 1008;
    // Initialization
    SetExitKey(0);
    systemSettings = loadSettingsFromConfigFile();
    Image icon = LoadImage(systemSettings.iconPath.toStringz());
    // Window and Audio Initialization
    InitWindow(screenWidth, screenHeight, systemSettings.windowTitle.toStringz());
    SetWindowIcon(icon);
    UnloadImage(icon);
    //ToggleFullscreen();
    SetTargetFPS(60);
    //fallback font?
    textFont = LoadFont(systemSettings.fallbackFont.toStringz());
    // Fade In and Out Effects
    InitAudioDevice();
    helloScreen();
    ClearBackground(Colors.BLACK);
    EndDrawing();
    camera.target = Vector2(screenWidth/2.0f, screenHeight/2.0f);
    camera.offset = Vector2(screenWidth/2.0f, screenHeight/2.0f);
    camera.rotation = 0.0f;
    camera.zoom = 1.0f;
    while (true)
    {
        switch (currentGameState)
        {
            case GameState.MainMenu:
                debugWriteln("Showing menu.");
                showMainMenu();
                break;
            case GameState.InGame:
                gameInit();
                while (!WindowShouldClose())
                {
                    if (luaReload) {
                        resetAllScriptValues();
                        int luaExecutionCode = luaInit(luaExec);
                        if (luaExecutionCode != EngineExitCodes.EXIT_OK) {
                            writeln("[ERROR] Engine stops execution according to error code: ", 
                            luaExecutionCode.to!EngineExitCodes);
                            currentGameState = GameState.Exit;
                            break;
                        }
                        luaReload = false;
                    }
                    SetExitKey(0);
                    if (IsKeyPressed(KeyboardKey.KEY_F11)) {
                        ToggleFullscreen();
                    }
                    BeginDrawing();
                    ClearBackground(Colors.BLACK);
                    // main logic
                    BeginMode2D(camera);
                    backgroundLogic();
                    characterLogic();
                    // effects logic
                    effectsLogic();
                    EndMode2D();
                    //drawing dialogs
                    dialogLogic();
                    luaEventLoop();
                    EndDrawing();
                }
                break;
            case GameState.Exit:
                EndDrawing();
                unloadResourcesLogic();
                CloseAudioDevice();
                CloseWindow();
                return;

            default:
                break;
        }
    }
}