//
// Created by sasha on 01.12.2025.
//

#include "modal_victory.h"

#include <format>

#include "engine/engine.h"
#include "engine/locale.h"

ModalVictory::ModalVictory(IWidget *parent, float x, float y,const std::string &type): IWidget (parent,x,y,311,465) {
    Locale *locale = Locale::getInstance();

    dialog_shading = new Image(this,0,0,Engine::getWindowSize().w,Engine::getWindowSize().h,"dialog_shading.png");

    image = new Image(this,x,y,311,465,"");

    button_replay = new ButtonVictory(this,x+19.5,y+376,locale->tr("Play again"),type);
    button_replay->setCallback(CallbackType::EVENT_ON_CLICK,[this]{
        if (callbacks.contains("onReplay")) {
            callbacks["onReplay"]();
        }
    });
    button_menu = new ButtonVictory(this,x+159.5,y+376,locale->tr("Menu"),type);
    button_menu->setCallback(CallbackType::EVENT_ON_CLICK,[this]{
        if (callbacks.contains("onMenu")) {
            callbacks["onMenu"]();
        }
    });

    description = new Text(this,x+99.5,y+125);
    description->setCached(true);
    description->setText(locale->tr("Victory"),"inter.ttf",30,{255,255,255});

    label_turn = new LabelInfoVictory(this,x+19.5,y+185,locale->tr("Turns"),type);
    label_time = new LabelInfoVictory(this,x+19.5,y+257,locale->tr("Times"),type);


    txtNewRecord = new Text(this,x+19.5,y+333);
    txtNewRecord->setCached(true);
    txtNewRecord->hide();
}

void ModalVictory::showVictory(int count_turn,int count_time){
    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);
    label_time->setValue(str_time);

    label_turn->setValue(std::to_string(count_turn));

    IWidget::show();
}


void ModalVictory::event(const SDL_Event &event) {
    IWidget::event(event);

    if (isVisible()) {
        button_replay->event(event);
        button_menu->event(event);
    }
}

void ModalVictory::setType(const std::string &type) {
    Locale *locale = Locale::getInstance();

    std::string filename = "victory_background_green.png";

    if (type=="normal"){
        filename="victory_background_blue.png";
    }
    if (type=="hard"){
        filename="victory_background_red.png";
    }

    image->setTexture(filename);

    button_replay->setType(type);
    button_menu->setType(type);

    label_turn->setType(type);
    label_time->setType(type);

    if (type=="easy"){
        txtNewRecord->setText(locale->tr("A new record!"),"inter.ttf",16,{0xFF,0xBB,0x00});
    }
    if (type=="normal"){
        txtNewRecord->setText(locale->tr("A new record!"),"inter.ttf",16,{0xFF,0xD7,0x00});
    }
    if (type=="hard"){
        txtNewRecord->setText(locale->tr("A new record!"),"inter.ttf",16,{0xFF,0xFF,0x29});
    }

    txtNewRecord->move(image->getFRect().x+(image->getFRect().w-txtNewRecord->getFRect().w)/2,txtNewRecord->getFRect().y);
}

void ModalVictory::onReplay(std::function<void()> func) {
    callbacks["onReplay"] = func;
}

void ModalVictory::onMenu(std::function<void()> func) {
    callbacks["onMenu"] = func;
}

void ModalVictory::showNewRecord(bool visible) {
    if (visible) {
        txtNewRecord->show();
    }else{
        txtNewRecord->hide();
    }
}
