#include "../Pieces/Generator.h"
#include "../Pieces/PiecesFiles.h"
#include "TextApp.h"

#include <chrono>
#include <filesystem>
#include <cmath>

static const int MAXIMUM_PIECES_SIZE = 10;

void testGeneratorForAllSizes(int max_size);
void testGeneratorForOneSize(int size);
void printPiecesByTypesForOneSize(int size);
void readStatsFromFilesForAllSizes(int max_size);


int main(int argc, char** argv) {
    std::srand(std::time(NULL));

    // CHECK PIECES FILES

    PiecesFiles pf;
    bool warned = false;
    for (int i = 1; i <= MAXIMUM_PIECES_SIZE; i++) {
        if (!std::filesystem::exists("data/pieces/" + std::to_string(i) + "minos.bin")) {
            if (!warned) {
                std::cout << "INFO: Pieces files for size " << i << " not found, generating..." << std::endl;
                warned = true;
            }
            
            pf.savePieces(i);
        }
    }

    // LAUNCH APP
    
    TextApp UI;
    UI.run();

    return 0;
}


void testGeneratorForAllSizes(int max_size) {
    using std::chrono::high_resolution_clock;
    using std::chrono::duration_cast;
    using std::chrono::duration;
    using std::chrono::milliseconds;
    Generator generator;

    for (int i = 1; i <= max_size; i++) {
        auto t1 = high_resolution_clock::now();
        std::vector<Polyomino> n_minos = generator.generatePolyominoes(i);
        auto t2 = high_resolution_clock::now();
    
        duration<double, std::milli> ms_double = t2 - t1;
        std::cout << "Generated " << n_minos.size() << " polyominoes of size " << i << " in " << ms_double.count() << "ms" << std::endl;
    }
}

void testGeneratorForOneSize(int size) {
    using std::chrono::high_resolution_clock;
    using std::chrono::duration_cast;
    using std::chrono::duration;
    using std::chrono::milliseconds;
    Generator generator;

    std::cout << "Generating " << size << "-minos" << std::endl;
    for (int i = 0; i < 10; i++) {
        auto t1 = high_resolution_clock::now();
        std::vector<Polyomino> n_minos = generator.generatePolyominoes(size);
        auto t2 = high_resolution_clock::now();
    
        duration<double, std::milli> ms_double = t2 - t1;
        std::cout << ms_double.count() << "ms" << std::endl;
    }
}

void printPiecesByTypesForOneSize(int size) {
    PiecesFiles piecesFiles;

    std::vector<Piece> pieces;
    std::vector<int> convexPieces;
    std::vector<int> holelessPieces;
    std::vector<int> otherPieces;
    piecesFiles.loadPieces(size, pieces, convexPieces, holelessPieces, otherPieces);

    std::cout << "Convex " << size << "-minos:" << std::endl;
    for (int index : convexPieces) {
        std::cout << pieces.at(index);
    }

    std::cout << "Holeless " << size << "-minos:" << std::endl;
    for (int index : holelessPieces) {
        std::cout << pieces.at(index);
    }

    std::cout << "Others " << size << "-minos:" << std::endl;
    for (int index : otherPieces) {
        std::cout << pieces.at(index);
    }
}

void readStatsFromFilesForAllSizes(int max_size) {
    PiecesFiles piecesFiles;
    for (int i = 1; i <= max_size; i++) {
        std::vector<Piece> pieces;
        std::vector<int> convexPieces;
        std::vector<int> holelessPieces;
        std::vector<int> otherPieces;
        piecesFiles.loadPieces(i, pieces, convexPieces, holelessPieces, otherPieces);

        std::cout << i << "-minos : " << pieces.size() << std::endl;
        std::cout << "Convex " << i << "-minos : " << convexPieces.size() << std::endl;
        std::cout << "Holeless " << i << "-minos : " << holelessPieces.size() << std::endl;
        std::cout << "Others " << i << "-minos : " << otherPieces.size() << std::endl;
    }
}
