/*
 * Chronometre.cpp
 * This file is part of ChronoCheckMate
 *
 * Copyright (C) 2010 - François Drouhard
 *
 * ChronoCheckMate is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * ChronoCheckMate is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with ChronoCheckMate; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, 
 * Boston, MA  02110-1301  USA
 */
 
#include "Chronometre.h"
#include <QTimer>
#include <QtWidgets>
#include <QLabel>
#include <QObject>
#include <QHBoxLayout>
#include <QDebug>
#include <QFont>
#include <QTime>
#include <QGroupBox>
#include <QDebug>

Chronometre::Chronometre (const QString & nom, Couleur unecouleur, QTime const& tempsDepart, QWidget * parent = nullptr) :
    QWidget (parent),
    couleur(unecouleur),
    temps(tempsDepart),
    msTempsDepart(tempsDepart)
{
    QHBoxLayout *layoutPrincipal = new QHBoxLayout;
    labelPion = new QLabel;
    pion = new QPixmap();
    setCouleur(couleur);
    labelPion->setPixmap(*pion);

    groupBox = new QGroupBox(nom);
    groupBox->setFlat(false);
    groupBox->setFont(QFont(groupBox->font().family(), 15));

    QFont font("Arial" , 25, 75);

    ecranTemps = new QLabel(temps.toString("hh:mm:ss:zzz"));
    ecranTemps->setMinimumWidth(200);
    ecranTemps->setFont(font);

    ecranLayout = new QHBoxLayout;
    ecranLayout->addSpacing(40);
    ecranLayout->addStretch(1);
    ecranLayout->addWidget(ecranTemps, 0);
    ecranLayout->addStretch(1);
    ecranLayout->addSpacing(40);

    layoutPrincipal->addWidget(labelPion,0);
    layoutPrincipal->addWidget(groupBox,1);

    groupBox->setLayout (ecranLayout);
    setLayout(layoutPrincipal);
	
    timer = new QTimer  (this);
    timer->setInterval (INTERVAL);
	QObject::connect (timer , SIGNAL (timeout() ) , this , SLOT (refresh()));
}

void Chronometre::modifierNomJoueur(QString joueur) {
    groupBox->setTitle(joueur);
}

void Chronometre::definirTemps(QTime const& tempsDepart) {
    msTempsDepart = tempsDepart;
}

void Chronometre::echangerCouleur(Chronometre & chrono) {
    Couleur temp = couleur;
    setCouleur(chrono.couleur);
    chrono.setCouleur(temp);
}

void Chronometre::setCouleur (Couleur color) {
    couleur = color;
    QString stringCouleur;
    if (couleur == Blanc)
        stringCouleur = "white";
    else
        stringCouleur = "black";
    pion->load(QString (":/ressources/pawn_%1.svg").arg(stringCouleur));
    labelPion->setPixmap(*pion);
}

Couleur Chronometre::getCouleur() const {
    return couleur;
}

void Chronometre::refresh () {
    temps = temps.addMSecs(-INTERVAL);
    affichage ();
    if (temps <= QTime (0,0,0,0)) {
        emit fin();
    }
}

void Chronometre::start () {
	timer->start ();
}

void Chronometre::stop () {
    timer->stop();
}


void Chronometre::affichage () {
    QString chaineTemps = temps.toString("hh:mm:ss:zzz");
    ecranTemps->setText(chaineTemps);
    //qDebug() << "Taille : " << ecranTemps->width();
}

void Chronometre::reinit () {
	if (timer->isActive()) {
		timer->stop();
	}
    temps  = msTempsDepart;
	affichage();
}

void Chronometre::basculer () {
	if (timer->isActive()) {
        stop();
	}
	else {
        start ();
	}
}

void Chronometre::basculer(Chronometre & Chrono) {
    basculer();
    Chrono.basculer();
}

QString Chronometre::operator+=(Chronometre const& joueur) const {
    if (temps <= QTime (0,0,0,0)) {
        return joueur.groupBox->title();
    } else if (joueur.temps <= QTime(0,0,0,0)) {
        return groupBox->title();
    }
    return "Nope";
}