#include "notepad.h"
#include "ui_notepad.h"

#include <QInputDialog>
#include <QFontDialog>
#include <QColorDialog>

using namespace std;

NotePad::NotePad(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::NotePad)
{
    ui->setupUi(this);
}

NotePad::~NotePad(){

}

void NotePad::on_actionFontSize_triggered(){
    QWidget *focused = QApplication::focusWidget();
    QPlainTextEdit* plainTextEdit = qobject_cast<QPlainTextEdit*>(focused);

    if (plainTextEdit) {
        QTextCursor cursor = plainTextEdit->textCursor();
        bool ok;
        int size = QInputDialog::getInt(this, "Taille de la police", "Entrez la taille de la police:", cursor.charFormat().font().pointSize(), 1, 100, 1, &ok);

        if (ok) {
            QTextCharFormat format;
            QFont font = cursor.charFormat().font();
            font.setPointSize(size);
            format.setFont(font);
            plainTextEdit->mergeCurrentCharFormat(format);
        }
    }
}


void NotePad::on_actionFontColor_triggered(){
    QWidget *focused = QApplication::focusWidget();
    QPlainTextEdit* plainTextEdit = qobject_cast<QPlainTextEdit*>(focused);

    if (plainTextEdit) {
        QColor color = QColorDialog::getColor(Qt::black, this, "Choisir une couleur");
        QTextCursor cursor = plainTextEdit->textCursor();
        if (color.isValid()) {
            QTextCharFormat format;
            format.setForeground(color);
            plainTextEdit->mergeCurrentCharFormat(format);
        }
    }
}


void NotePad::on_actionFont_triggered(){
    QWidget *focused = QApplication::focusWidget();
    QPlainTextEdit* plainTextEdit = qobject_cast<QPlainTextEdit*>(focused);

    if (plainTextEdit) {
        QTextCursor cursor = plainTextEdit->textCursor();
        bool ok;
        QFont font = QFontDialog::getFont(&ok, cursor.charFormat().font(), this, "Choisir une police");
        if (ok) {
            QTextCharFormat format;
            format.setFont(font);
            plainTextEdit->mergeCurrentCharFormat(format);
        }
    }
}


void NotePad::on_actionBold_triggered(){
    QWidget *focused = QApplication::focusWidget();
    QPlainTextEdit* plainTextEdit = qobject_cast<QPlainTextEdit*>(focused);

    if (plainTextEdit) {
        QTextCursor cursor = plainTextEdit->textCursor();
        QTextCharFormat format;
        QFont font = cursor.charFormat().font();
        font.setBold(!font.bold());
        format.setFont(font);
        plainTextEdit->mergeCurrentCharFormat(format);
    }
}


void NotePad::on_actionItalic_triggered(){
    QWidget *focused = QApplication::focusWidget();
    QPlainTextEdit* plainTextEdit = qobject_cast<QPlainTextEdit*>(focused);

    if (plainTextEdit) {
        QTextCursor cursor = plainTextEdit->textCursor();
        QTextCharFormat format;
        QFont font = cursor.charFormat().font();
        font.setItalic(!font.italic());
        format.setFont(font);
        plainTextEdit->mergeCurrentCharFormat(format);
    }
}


void NotePad::on_actionOverline_triggered(){
    QWidget *focused = QApplication::focusWidget();
    QPlainTextEdit* plainTextEdit = qobject_cast<QPlainTextEdit*>(focused);

    if (plainTextEdit) {
        QTextCursor cursor = plainTextEdit->textCursor();
        QTextCharFormat format;
        QFont font = cursor.charFormat().font();
        font.setOverline(!font.overline());
        format.setFont(font);
        plainTextEdit->mergeCurrentCharFormat(format);
    }
}


void NotePad::on_actionUnderline_triggered(){
    QWidget *focused = QApplication::focusWidget();
    QPlainTextEdit* plainTextEdit = qobject_cast<QPlainTextEdit*>(focused);

    if (plainTextEdit) {
        QTextCursor cursor = plainTextEdit->textCursor();
        QTextCharFormat format;
        QFont font = cursor.charFormat().font();
        font.setUnderline(!font.underline());
        format.setFont(font);
        plainTextEdit->mergeCurrentCharFormat(format);
    }
}

void NotePad::setTexte(QString t){
    texte = t;
    setUi(t);
}
void NotePad::setUi(QString t){ui->plainTextEdit->setPlainText(t);}
void NotePad::getUi(){texte = ui->plainTextEdit->toPlainText();}
void NotePad::on_actionSauvegarder_triggered(){emit demandeSauvegarde(ui->plainTextEdit->toPlainText());}

