#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <math.h>
#include <string.h>
#include <time.h>

#include "graphical-aspect.h"
#include "networld.h"
#include "controlpanel.h"
#include "player.h"

#include "raylib.h"




void Graphic_ShowPlayerInfo(Player * player, int screenWidth, int screenHeight, Font font){
    Rectangle rec = {0, screenHeight - 250, 250, 250}; 
    DrawRectangleLinesEx(rec, 10, player->color);
    //Font font = LoadFont("Game On_PersonalUseOnly.ttf");
    char * playerNumber = malloc(20*sizeof(char));
    sprintf(playerNumber, "Player %d", player->ID);
    DrawTextEx(font, playerNumber, (Vector2){35, screenHeight - 240}, 30.0, 3.0, player->color);

    char * soldierNumber = malloc(40*sizeof(char));
    sprintf(soldierNumber, "Soldiers left  %d", player->soldiers);
    DrawTextEx(font, soldierNumber, (Vector2){15, screenHeight - 180}, 24.0, 3.0, player->color);

    int totalTroops = 0;
    for(int i =0; i < player->nodeCount; i++){
        totalTroops += player->nodes[i]->soldiers;
    }
    char * str_totalTroops = malloc(40*sizeof(char));
    sprintf(str_totalTroops, "Total Troops  %d", totalTroops);
    DrawTextEx(font, str_totalTroops, (Vector2){15, screenHeight - 150}, 24.0, 3.0, player->color);

    char * str_nodeCount = malloc(40*sizeof(char));
    sprintf(str_nodeCount, "Nodes owned  %d", player->nodeCount);
    DrawTextEx(font, str_nodeCount, (Vector2){15, screenHeight - 120}, 24.0, 3.0, player->color);

    free(playerNumber);
    free(soldierNumber);
    free(str_totalTroops);
    free(str_nodeCount);
}

void Graphic_RecrutementPhase(Player * player, int screenWidth, int screenHeight, Font font){
    Rectangle rec = {screenWidth-300, 100 , 300, 400}; 
    DrawRectangleLinesEx(rec, 10, player->color);
    DrawTextEx(font, "Recrutement Phase",(Vector2){screenWidth-275, 120}, 23.0, 3.0, player->color);
    DrawTextRec(font, "   LEFT click on one of your node to recruit a soldier", (Rectangle){screenWidth-285, 175, 270, 100}, 20.0, 3.0, true, player->color);
    DrawTextRec(font, "   RIGHT click on the same node to remove the soldier", (Rectangle){screenWidth-285, 295, 270, 100}, 20.0, 3.0, true, player->color);
    DrawTextRec(font, "   click Confirm once finished", (Rectangle){screenWidth-285, 415, 270, 100}, 20.0, 3.0, true, player->color);

}
void Graphic_AttackPhase(Player * player, int screenWidth, int screenHeight, Font font){
    Rectangle rec = {screenWidth-300, 100 , 300, 400}; 
    DrawRectangleLinesEx(rec, 10, player->color);
    DrawTextEx(font, "Attack Phase",(Vector2){screenWidth-265, 120}, 23.0, 3.0, player->color);
    DrawTextRec(font, "   FIRST click on the node you want to attack from", (Rectangle){screenWidth-285, 175, 270, 100}, 20.0, 3.0, true, player->color);
    DrawTextRec(font, "   THEN click on one of neighbouring node you want to attack", (Rectangle){screenWidth-285, 295, 270, 100}, 20.0, 3.0, true, player->color);
    DrawTextRec(font, "   click Next Turn once finished", (Rectangle){screenWidth-285, 415, 270, 100}, 20.0, 3.0, true, player->color);
}

void Graphic_WhoseTurnIsIt(Player * player, int screenWidth, int screenHeight, Font font){
    //Rectangle rec = {0, screenHeight - 250, 250, 250}; 
    //DrawRectangleLinesEx(rec, 10, player->color);
    //Font font = LoadFont("Game On_PersonalUseOnly.ttf");
    char * whoseTurn = malloc(20*sizeof(char));
    sprintf(whoseTurn, "Player %d Turn", player->ID);
    DrawTextEx(font, whoseTurn, (Vector2){screenWidth/2 - (MeasureTextEx(font, whoseTurn, 50.0, 3.0).x)/2 , 30}  , 50.0, 3.0, player->color);

    free(whoseTurn);
}

Rectangle Graphic_ConfirmButton(Font font)
{
    Rectangle nextPhase = {200, 200, 220, 55};     
    DrawRectangleRec(nextPhase, GREEN);
    DrawTextEx(font, "Confirm",(Vector2){220 , 210} , 40.0, 3.0, BLACK); 
    return nextPhase;
}

void Graphic_MouseHoverNodeRecrutement(Player * player, Vector2 mousePosition, Panel * panel){
    for(int nodeIndex = 0; nodeIndex < player->nodeCount; nodeIndex++){
        Node* currentNode = player->nodes[nodeIndex];
        if(CheckCollisionPointRec(mousePosition, currentNode->collisionHitbox)){
            //printf("collision noeud !\n");
            Vector2 screenPosition= Panel_pixelFromPosition(panel, &(currentNode->position) );
            DrawCircleV(screenPosition, 29, MAGENTA);
            //DrawCircleV(screenPosition, 45, RAYWHITE);
            if(player->soldiers > 0){
                DrawText("+1", (int)screenPosition.x + 30 , (int)screenPosition.y, 20, MAGENTA);
            }
        }
    }
    
}

void Graphic_MouseHoverNodeChooseAttacker(Player * player, Vector2 mousePosition, Panel * panel){
    for(int nodeIndex = 0; nodeIndex < player->nodeCount; nodeIndex++){
        Node* currentNode = player->nodes[nodeIndex];
        if(CheckCollisionPointRec(mousePosition, currentNode->collisionHitbox)){
            Vector2 screenPosition= Panel_pixelFromPosition(panel, &(currentNode->position) );
            DrawCircleV(screenPosition, 29, GOLD);
            //DrawCircleV(screenPosition, 45, RAYWHITE);
            if(!player->hasSelectedNode){
                DrawText("attacker", (int)screenPosition.x + 30 , (int)screenPosition.y, 20, GOLD);
            }    
        }
    }
    if(player->hasSelectedNode){
        Vector2 screenPosition= Panel_pixelFromPosition(panel, &(player->selectedNode->position) );
        DrawCircleV(screenPosition, 29, GOLD);
        DrawText("attacker", (int)screenPosition.x + 30 , (int)screenPosition.y, 20, GOLD);
    }
}
void Graphic_MouseHoverNodeChooseTarget(Node * originNode, Vector2 mousePosition, Panel * panel){
    for(int neighbourIndex = 0; neighbourIndex < originNode->card; neighbourIndex++){
        Vector2 screenPosition = Panel_pixelFromPosition(panel, &(originNode->edges[neighbourIndex]._target->position) );
        /* if( &(originNode->edges[neighbourIndex]._target->color) = &(originNode->color)){          
            DrawCircleV(screenPosition, 29, GREEN);
        } */
    }
}





#define MAX_FRAME_SPEED     15
#define MIN_FRAME_SPEED      1

void diceRolling()
{   printf("dice rolling begin\n");    // Initialization
    //--------------------------------------------------------------------------------------
    const int screenWidth1 = 800;
    const int screenHeight1 = 450;

    InitWindow(screenWidth1, screenHeight1, "raylib [texture] example - texture rectangle");

    // NOTE: Textures MUST be loaded after Window initialization (OpenGL context is required)
    Texture2D diceTexture = LoadTexture("diceRolling.png");        // Texture loading

    Vector2 position = { 350.0f, 100.0f };
    Rectangle frameRec = { 0.0f, 0.0f, (float)diceTexture.width, (float)diceTexture.height/9 };
    int currentFrame = 0;

    int framesCounter = 0;
    int framesSpeed = 10;  
    int frameExit =  0;        // Number of spritesheet frames shown by second
    bool endAnimation = false;
    SetTargetFPS(60);               // Set our game to run at 60 frames-per-second
    //--------------------------------------------------------------------------------------

    // Main game loop
    while (!WindowShouldClose() )    // Detect window close button or ESC key
    {
        // Update
        //----------------------------------------------------------------------------------
        framesCounter++;
        frameExit++;
        printf("%d frameExit \n", frameExit);
        if(frameExit == 100) endAnimation = true;
        if (framesCounter >= (60/framesSpeed))
        {
            framesCounter = 0;
            currentFrame++;

            if (currentFrame > 8) currentFrame = 0;

            frameRec.y = (float)currentFrame*(float)diceTexture.height/9;
        }
        //----------------------------------------------------------------------------------

        // Draw
        //----------------------------------------------------------------------------------
        BeginDrawing();

            ClearBackground(RAYWHITE);
            //Vector2 * positionDiceAttacker = malloc(sizeof(Vector2)*attackerNbDice) ;
            /* for(int attackerDice = 0; attackerDice < attackerNbDice ; attackerDice++){
                
            } */
            DrawTextureRec(diceTexture, frameRec, position, WHITE);  // Draw part of the texture

        EndDrawing();
        //----------------------------------------------------------------------------------
    }

    // De-Initialization
    //--------------------------------------------------------------------------------------
           // Texture unloading

    CloseWindow();                // Close window and OpenGL context
    UnloadTexture(diceTexture);
    //--------------------------------------------------------------------------------------

    
}

