/*******************************************************************************************
*
*   NetWorld basic viewer
*   Copyright (c) 2020-2020 Guillaume Lozenguez
*
********************************************************************************************/

#include "raylib.h"

#include <stdlib.h>
#include <stdio.h>
#include <math.h>

#include "networld.h"
#include "player.h"
#include "controlpanel.h"
#include "random-map.h"

// Program attributes
//-------------------
const int screenWidth = 800;
const int screenHeight = 600;
const int targetFPS = 60;

void game_update(NetWorld * world, Player * players);

// Game attributes
//-----------------
bool game_end;

int main(int nbArg, char ** arg)
{
    int nodeCount = 5;
    int playerCount = 3;
    NetWorld * world= NetWorld_new(nodeCount, playerCount);
    Random_map(world);
    
    
    //Node_set(&(world->nodes[0]), (Vector2){1.4f, 1.28f}, ORANGE);
    printf("positionX %f\n", world->nodes[0].position.x);
    printf("positionY %f\n", world->nodes[0].position.y);
    printf("positionX %f\n", world->nodes[1].position.x);
    printf("positionY %f\n", world->nodes[1].position.y);
    printf("distXY %f\n", dist(world->nodes[0], world->nodes[1]));


    // Game Initialization
    //--------------------
    game_end= false;

    Panel * panel= Panel_new();
    Panel_initialize(panel, world);

    Color colors[] = {BLACK, RED, BLUE};
    Player * players = Player_newArray(playerCount, colors);
    for(int index = 0; index < world->size; index++){
        Player_add_Node(&(players[index % playerCount]), &(world->nodes[index]));
    }
    // Raylib Initialization
    //----------------------
    InitWindow(screenWidth, screenHeight, "NetWorld basic viewer");
    SetTargetFPS(targetFPS);

    // Main game loop
    world->currentPlayer = 0;
    Player_start_turn(&(players[world->currentPlayer]));
    while (!game_end && !WindowShouldClose())    // Detect window close button or ESC key
    {
        DrawFPS(10, 10);
        for(int index = 0; index < world->size; index++){
            Vector2 screenPosition= Panel_pixelFromPosition(panel, &(world->nodes[index]).position);
            world->nodes[index].collisionHitbox.x = screenPosition.x - 24;
            world->nodes[index].collisionHitbox.y = screenPosition.y - 24;
        }
        Panel_control(panel);
        game_update(world, players);
        Panel_draw(panel);
    }

    // proper closing
    //---------------
    NetWorld_delete(world);
    Player_delete(players);
    CloseWindow();   // Close window and OpenGL context
 
    return 0;
}

void game_update(NetWorld * world, Player * players)
{
    Player * currentPlayer = &(players[world->currentPlayer]);
    int playerCount = world->playerCount;
    Player * newPlayers = malloc(sizeof(Player)); 
    switch(currentPlayer->turnPhase){            
        case 1:
            if(currentPlayer->soldiers > 0){
                if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON)){
                    Vector2 mousePosition = GetMousePosition();
                    Player_add_soldier(currentPlayer, mousePosition);
                }
            }else{
                Rectangle nextPhase = {screenWidth-48,screenHeight-48,48,48};
                DrawRectangleRec(nextPhase, GREEN);
                if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON)){
                    Vector2 mousePosition = GetMousePosition();
                    if(CheckCollisionPointRec(mousePosition, nextPhase)){
                        Player_confirm_recrutement(currentPlayer);
                        printf("Recrutement confirmed\n");
                        currentPlayer->turnPhase = 2;
                        printf("Attack phase\n");
                    }
                }
            }
            if(IsMouseButtonPressed(MOUSE_RIGHT_BUTTON)){
                Vector2 mousePosition = GetMousePosition();
                Player_remove_soldier(currentPlayer, mousePosition);
            }
            break;
        case 2:
            if(currentPlayer->hasSelectedNode){
                if(IsMouseButtonPressed(MOUSE_RIGHT_BUTTON)){
                    Vector2 mousePosition = GetMousePosition();
                    Player_unselect_Node(currentPlayer, mousePosition);
                }
                if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON)){
                    Vector2 mousePosition = GetMousePosition();
                    Node * originNode = currentPlayer->selectedNode;

                    for(int nodeIndex = 0; nodeIndex < world->size; nodeIndex++){
                        Node* currentNode = &(world->nodes[nodeIndex]);
                        
                        if(CheckCollisionPointRec(mousePosition, currentNode->collisionHitbox)){
                            int nodePlayerID = currentNode->playerID;
                            if(nodePlayerID == currentPlayer->ID){
                                printf("Selected target Node\n");
                                currentPlayer->selectedNode = currentNode;
                            }
                            else if(Node_are_connected(originNode, currentNode) && (originNode->soldiers > 1) && originNode->canAttack){
                                printf("Engagine attack on target Node\n");
                                Player_attack_Node(currentPlayer, &(players[nodePlayerID]), originNode, currentNode);
                            }else{
                                printf("Failed to attack target : Origin has already attacked, nodes aren't connected or origin node has too few soldiers\n");
                            }
                        }
                    }
                }
            }else{
                if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON)){
                    Vector2 mousePosition = GetMousePosition();
                    Player_select_Node(currentPlayer, mousePosition);
                }
            }
            Rectangle nextPhase = {screenWidth-48,screenHeight-48,48,48};
            DrawRectangleRec(nextPhase, GREEN);
            if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON)){
                Vector2 mousePosition = GetMousePosition();
                if(CheckCollisionPointRec(mousePosition, nextPhase)){
                    printf("Attack phase ended\n");
                    currentPlayer->turnPhase = -1;
                }
            }
            break;
        case -1 :
            Player_end_turn(currentPlayer);
            printf("Turn ended\n");
            world->currentPlayer = (world->currentPlayer + 1) % playerCount;
            printf("Player changed\n");
            Player_start_turn(&(players[world->currentPlayer]));
            printf("Turn started\n");
            printf("Recrutement phase\n");
            break;
        default:;
    }
}
