#include <stdio.h>
#include <stdlib.h>
#include <graph.h>
#include <string.h>

#include "taquin.h"
#include "graph_sup.h"

void UpdateSelection(char* selection, int param) {
    int MenuHeight = HEIGHT - 120, i;
    if (selection == "lines") {
        char str[30];
        for (i = 3; i <= 8; i++) {
            sprintf(str, "./images/assets/sel_%d.png", i);
            ChargerImage(str, 1001, 88 + 81 * (i-3), 0, 0, 61, 61);
        }
        ChargerImage("./images/assets/sel_halo.png", 1001, 88 + 81 * (param-3), 0, 0, 61, 61);
    } else if (selection == "columns") {
        char str[30];
        for (i = 3; i <= 8; i++) {
            sprintf(str, "./images/assets/sel_%d.png", i);
            ChargerImage(str, 737, 88 + 81 * (i-3), 0, 0, 61, 61);
        }
        ChargerImage("./images/assets/sel_halo.png", 737, 88 + 81 * (param-3), 0, 0, 61, 61);
    } else if (selection == "image"){
	    ChargerImage("./images/assets/img_sel.png", 0, MenuHeight/3 * param, 0, 0, 603, 198);
    }
};

int DrawMenu() {
    ClearButtons();
    EffacerEcran(GetColor(255, 255, 255));

    int MenuHeight = HEIGHT - 120;
    int Lines = 3, Columns = 3, i;
    int X_image = 0, Y_image = 0;

    char filename[100];
    
    char str[3];
    for (i = 0; i <= 5; i++) {
        AddButton(737, 88 + 81 * i, 61, 61, 10 + i); //Colonnes
        AddButton(1001, 88 + 81 * i, 61, 61, 20 + i); //Lignes
    }

    ChargerImage("./images/assets/menu_background.png", 0, 0, 0, 0, WIDTH, HEIGHT);
    AddButton(WIDTH/4 * 3 - 100, HEIGHT - 100, 200, 80, 1); //Lancer le Taquin
    AddButton(WIDTH/4 - 100, HEIGHT -100, 200, 80, 2); //Quitter

    UpdateSelection("lines", Lines);
    UpdateSelection("columns", Columns);
    
    AddButton(0, 0, WIDTH/2, MenuHeight/3, 3); //Luna
    AddButton(0, MenuHeight/3, WIDTH/2, MenuHeight/3, 4); //Zanitsu
    AddButton(0, MenuHeight/3 *2, WIDTH/2, MenuHeight/3, 5); //Bonsaï

    while (1) {
        if (DrawNextFrame()) {
            if (SourisCliquee()) {
                int BT_ID = GetButton(_X, _Y);

                if (BT_ID >= 10 && BT_ID <= 15) {
                    Columns = BT_ID - 7;
                    UpdateSelection("columns", Columns);
                } else if (BT_ID >= 20 && BT_ID <= 25) {
                    Lines = BT_ID - 17;
                    UpdateSelection("lines", Lines);
                } else if (BT_ID >= 3 && BT_ID <= 5){
                    ChargerImage("./images/assets/menu_background.png", 0, 0, 0, 0, WIDTH, HEIGHT);
                    UpdateSelection("lines", Lines);
                    UpdateSelection("columns", Columns);
                    UpdateSelection("image", BT_ID - 3);

                    if (BT_ID == 3){
                        X_image = 500;
                        Y_image = 445;
                        strcpy(filename, "./images/luna.png");
                    } else if (BT_ID == 4){
                        X_image = 450;
                        Y_image = 311;
                    strcpy(filename, "./images/zenitsu.png");
                    } else if (BT_ID == 5){
                        X_image = 400;
                        Y_image = 400;
                        strcpy(filename, "./images/bonsai.png");
                    }
                } else {
                    if (BT_ID == 1 && X_image != 0) {
                        ClearButtons();
                        EffacerEcran(GetColor(255, 255, 255));
                        CreateTaquin(filename, X_image, Y_image, Lines, Columns);
                        return 1;
                    } else if (BT_ID == 2) {
                        return 0;
                    }
                }
            }
        }
    }
    return 0;
}
