#include "raylib.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define MAX_QUESTS 100
#define SAVE_FILE "player_stats.txt"

// Enum für die Menü-Tabs
typedef enum {
    TAB_QUESTS = 0,
    TAB_STORY,
    TAB_CHAIN,
    TAB_TREE
} MenuTab;

// Struktur für eine Quest
typedef struct {
    char title[64];
    char description[256];
    int xp_reward;
    int cp_reward;
    bool isCompleted;
    MenuTab type; // TAB_QUESTS (Normal) oder TAB_STORY
} Quest;

// Globale Variablen für den Spielstand
int playerXP = 0;
int playerCP = 0;
Quest quests[MAX_QUESTS];
int questCount = 0;

// Funktion zum Speichern der Werte
void SaveStats() {
    FILE *file = fopen(SAVE_FILE, "w");
    if (file != NULL) {
        fprintf(file, "%d\n%d\n", playerXP, playerCP);
        fclose(file);
    }
}

// Funktion zum Laden der Werte
void LoadStats() {
    FILE *file = fopen(SAVE_FILE, "r");
    if (file != NULL) {
        fscanf(file, "%d\n%d\n", &playerXP, &playerCP);
        fclose(file);
    }
}

// Hilfsfunktion zum Zeichnen eines Buttons und Prüfen von Klicks
bool DrawButton(Rectangle bounds, const char* text, Color color, Color hoverColor) {
    Vector2 mousePoint = GetMousePosition();
    bool isHovered = CheckCollisionPointRec(mousePoint, bounds);
    
    DrawRectangleRec(bounds, isHovered ? hoverColor : color);
    DrawRectangleLinesEx(bounds, 2, BLACK);
    
    int textWidth = MeasureText(text, 20);
    DrawText(text, bounds.x + (bounds.width / 2) - (textWidth / 2), bounds.y + (bounds.height / 2) - 10, 20, BLACK);
    
    return isHovered && IsMouseButtonPressed(MOUSE_BUTTON_LEFT);
}

// Funktion zum Erstellen einer neuen Quest (Test-Daten)
void CreateRandomQuest(MenuTab type) {
    if (questCount >= MAX_QUESTS) return;
    
    Quest q;
    q.type = type;
    q.isCompleted = false;
    
    int qNum = questCount + 1;
    if (type == TAB_QUESTS) {
        snprintf(q.title, sizeof(q.title), "Nebenquest #%d", qNum);
        snprintf(q.description, sizeof(q.description), "Sammle 5 Kraeuter im Wald fuer den Alchemisten.");
        q.xp_reward = GetRandomValue(50, 150);
        q.cp_reward = GetRandomValue(10, 50);
    } else {
        snprintf(q.title, sizeof(q.title), "Story Kapitel %d", qNum);
        snprintf(q.description, sizeof(q.description), "Besiege den Drachen und rette das Dorf aus den Flammen!");
        q.xp_reward = GetRandomValue(500, 1000);
        q.cp_reward = GetRandomValue(100, 300);
    }
    
    quests[questCount] = q;
    questCount++;
}

int main(void) {
    // Initialisierung
    const int screenWidth = 1000;
    const int screenHeight = 700;
    InitWindow(screenWidth, screenHeight, "RPG Quest Menu");
    SetTargetFPS(60);

    LoadStats(); // Lade XP und CP beim Start

    MenuTab currentTab = TAB_QUESTS;

    while (!WindowShouldClose()) {
        // --- UPDATE ---
        Vector2 mousePoint = GetMousePosition();

        // --- DRAW ---
        BeginDrawing();
        ClearBackground(RAYWHITE);

        // Header: Stats anzeigen
        DrawRectangle(0, 0, screenWidth, 60, DARKGRAY);
        DrawText(TextFormat("Total XP: %d", playerXP), 20, 20, 20, GREEN);
        DrawText(TextFormat("Total CP: %d", playerCP), 250, 20, 20, GOLD);
        DrawText("Gespeichert in player_stats.txt", screenWidth - 300, 20, 16, LIGHTGRAY);

        // Tab-Buttons zeichnen
        const char* tabNames[] = {"Quests", "Story-Quests", "Chain-Quests", "Tree-Quests"};
        for (int i = 0; i < 4; i++) {
            Rectangle tabRect = { 20 + i * 200, 80, 180, 40 };
            Color tabColor = (currentTab == i) ? LIGHTGRAY : GRAY;
            if (DrawButton(tabRect, tabNames[i], tabColor, LIGHTGRAY)) {
                currentTab = i;
            }
        }

        // Trennlinie
        DrawLine(20, 130, screenWidth - 20, 130, BLACK);

        // Inhalt basierend auf dem ausgewählten Tab
        if (currentTab == TAB_CHAIN || currentTab == TAB_TREE) {
            // Coming Soon für Chain und Tree
            DrawText("Coming Soon...", screenWidth / 2 - MeasureText("Coming Soon...", 40) / 2, screenHeight / 2, 40, MAROON);
        } else {
            // Button zum Erstellen einer neuen Quest
            Rectangle createBtn = { screenWidth - 220, 150, 200, 40 };
            if (DrawButton(createBtn, "+ Neue Quest erstellen", SKYBLUE, BLUE)) {
                CreateRandomQuest(currentTab);
            }

            // Quests anzeigen
            int drawY = 150;
            for (int i = 0; i < questCount; i++) {
                if (quests[i].type == currentTab) {
                    Rectangle questBox = { 20, drawY, screenWidth - 260, 100 };
                    DrawRectangleRec(questBox, quests[i].isCompleted ? Fade(GREEN, 0.3f) : Fade(RAYWHITE, 0.9f));
                    DrawRectangleLinesEx(questBox, 1, BLACK);

                    // Titel und Beschreibung
                    DrawText(quests[i].title, questBox.x + 10, questBox.y + 10, 20, DARKBLUE);
                    DrawText(quests[i].description, questBox.x + 10, questBox.y + 40, 16, DARKGRAY);
                    
                    // Belohnungen
                    DrawText(TextFormat("Belohnung: %d XP | %d CP", quests[i].xp_reward, quests[i].cp_reward), 
                             questBox.x + 10, questBox.y + 70, 16, PURPLE);

                    // Abhaken-Button (nur wenn noch nicht abgeschlossen)
                    if (!quests[i].isCompleted) {
                        Rectangle completeBtn = { questBox.x + questBox.width - 160, questBox.y + 30, 140, 40 };
                        if (DrawButton(completeBtn, "Abschliessen", LIME, GREEN)) {
                            quests[i].isCompleted = true;
                            playerXP += quests[i].xp_reward;
                            playerCP += quests[i].cp_reward;
                            SaveStats(); // Direkt speichern bei Fortschritt
                        }
                    } else {
                        DrawText("Abgeschlossen!", questBox.x + questBox.width - 150, questBox.y + 40, 20, DARKGREEN);
                    }

                    drawY += 120; // Abstand zur nächsten Quest
                }
            }
            
            // Falls keine Quests vorhanden sind
            if (drawY == 150) {
                DrawText("Keine Quests in dieser Kategorie vorhanden.", 40, 200, 20, GRAY);
            }
        }

        EndDrawing();
    }

    // Aufräumen
    CloseWindow();
    return 0;
}