//
// Created by sasha on 27.11.2025.
//

#include "gamescreen.h"

#include <format>

#include "engine/engine.h"
#include "engine/widgets/image.h"
#include "record_store.h"
#include "audio_effect.h"
#include "engine/locale.h"

GameScreen::GameScreen() : IScreen("game") {
    SDL_Log("=> GameScreen::GameScreen");
    this->setName("GameScreen");
    this->setBackgroundColor(0x57,0x74,0xD9);

    count_turn=0;
    count_time=0;

    button_back = new Image(this,16,34,57,32);
    button_back->setName("button_back");
    button_back->setCallback(CallbackType::EVENT_ON_CLICK,[]{
        AudioEffect::click();
        Engine::setActiveScreen("level_selection");
    });

    turn = new Image(this,91,34,70,32);

    time = new Image(this,179,34,115,32);

    refresh = new Image(this,312,34,32,32);
    refresh->setName("refresh");
    refresh->setCallback(CallbackType::EVENT_ON_CLICK,[this]{
        AudioEffect::click();
        replay();

    });

    circle = new Image(this,129,122,103,103);

    text_circle = new Text(this,110,150);
    text_circle->setCached(true);


    text_count_time = new Text(this,216,40.5,"inter.ttf",16,{255,255,255});

    text_count_turn = new Text(this,121,40.5,"inter.ttf",16,{255,255,255});

    board = new Board(this,26,335);
    board->setName("board");
    board->onTurn([this]{
        AudioEffect::move_cube();
        count_turn++;
        reInitCounters();

    });
    board->onWon(std::bind(&GameScreen::onWon,this));

    timer = new Timer(this);
    timer->start(1000,[this]{
        count_time++;
        reInitCounters();

    });

    victory = new ModalVictory(this,26,174,"green");
    victory->onMenu([]{
        AudioEffect::click();
        Engine::setActiveScreen("main");
    });
    victory->onReplay([this]{
        AudioEffect::click();
        replay();
    });
    victory->hide();
}

void GameScreen::onActive(const VariantMap &args) {
    curLevel = std::get<std::string>(args.at("level"));
    Locale *locale = Locale::getInstance();


    if (curLevel == "easy"){
        this->setBackgroundColor(0xA0,0xFF,0xC4);
        button_back->setTexture("button_back_green.png");
        turn->setTexture("label_turn_green.png");
        time->setTexture("label_time_green.png");
        refresh->setTexture("button_refresh_green.png");
        circle->setTexture("game_circle_green.png");
        text_circle->setText(locale->tr("Easy"),"inter.ttf",40,{255,255,255});

        board->createBoard("easy");
    }
    if (curLevel == "normal"){
        this->setBackgroundColor(0x57,0x74,0xD9);
        button_back->setTexture("button_back_blue.png");
        turn->setTexture("label_turn_blue.png");
        time->setTexture("label_time_blue.png");
        refresh->setTexture("button_refresh_blue.png");
        circle->setTexture("game_circle_blue.png");
        text_circle->setText(locale->tr("Normal"),"inter.ttf",40,{255,255,255});

        board->createBoard("normal");
    }
    if (curLevel == "hard"){
        this->setBackgroundColor(0xFF,0xB4,0xB0);
        button_back->setTexture("button_back_red.png");
        turn->setTexture("label_turn_red.png");
        time->setTexture("label_time_red.png");
        refresh->setTexture("button_refresh_red.png");
        circle->setTexture("game_circle_red.png");
        text_circle->setText(locale->tr("Hard"),"inter.ttf",40,{255,255,255});

        board->createBoard("hard");
    }

    // Выравнивание текста по центру
    SDL_FRect rect = text_circle->getFRect();
    SDL_FRect rect_window = Engine::getTemplateWindowSize();
    text_circle->move((rect_window.w-rect.w)/2,rect.y);

    victory->setType(curLevel);
    victory->hide();

    count_time=0;
    count_turn=0;
    reInitCounters();
    timer->start();
}

void GameScreen::onWon() {
    RecordStore recordStore;

    timer->stop();
    SDL_Log("save: %i %i",count_turn,count_time);

    victory->showNewRecord(false);
    if (recordStore.isRecord(curLevel,count_turn,count_time)){
        victory->showNewRecord(true);
        recordStore.save(curLevel,count_turn,count_time);
    }

    victory->showVictory(count_turn,count_time);

}

void GameScreen::reInitCounters() {
    int h = count_time/3600;
    int m = (count_time - h*3600)/60;
    int s = count_time - h*3600 - m*60;
    std::string str_time = std::format("{:02}:{:02}:{:02}", h, m, s);
    text_count_time->setText(str_time,"inter.ttf",16,{255,255,255});


    std::string turn_string = std::to_string(count_turn);
    if (turn_string.size()==1) turn_string="  "+turn_string;
    if (turn_string.size()==2) turn_string=" "+turn_string;
    text_count_turn->setText(turn_string,"inter.ttf",16,{255,255,255});

}

void GameScreen::replay() {
    victory->hide();

    board->intermix();
    count_time=0;
    count_turn=0;

    reInitCounters();
    timer->start();
}
