#include "TextFormatUtils.h"
#include <QApplication>


QPlainTextEdit* TextFormatUtils::getFocusedPlainTextEdit() {
    QWidget* focused = QApplication::focusWidget();
    return qobject_cast<QPlainTextEdit*>(focused);
}

void TextFormatUtils::changeFontSize(QWidget* parent) {
    QPlainTextEdit* edit = getFocusedPlainTextEdit();
    if (edit) {
        QTextCursor cursor = edit->textCursor();
        bool ok;
        int size = QInputDialog::getInt(parent, "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);
            edit->mergeCurrentCharFormat(format);
        }
    }
}

void TextFormatUtils::changeFontColor(QWidget* parent) {
    QPlainTextEdit* edit = getFocusedPlainTextEdit();
    if (edit) {
        QColor color = QColorDialog::getColor(Qt::black, parent, "Choisir une couleur");
        if (color.isValid()) {
            QTextCharFormat format;
            format.setForeground(color);
            edit->mergeCurrentCharFormat(format);
        }
    }
}

void TextFormatUtils::changeFont(QWidget* parent) {
    QPlainTextEdit* edit = getFocusedPlainTextEdit();
    if (edit) {
        QTextCursor cursor = edit->textCursor();
        bool ok;
        QFont font = QFontDialog::getFont(&ok, cursor.charFormat().font(), parent, "Choisir une police");
        if (ok) {
            QTextCharFormat format;
            format.setFont(font);
            edit->mergeCurrentCharFormat(format);
        }
    }
}

void TextFormatUtils::toggleBold() {
    QPlainTextEdit* edit = getFocusedPlainTextEdit();
    if (edit) {
        QTextCursor cursor = edit->textCursor();
        QTextCharFormat format;
        QFont font = cursor.charFormat().font();
        font.setBold(!font.bold());
        format.setFont(font);
        edit->mergeCurrentCharFormat(format);
    }
}

void TextFormatUtils::toggleItalic() {
    QPlainTextEdit* edit = getFocusedPlainTextEdit();
    if (edit) {
        QTextCursor cursor = edit->textCursor();
        QTextCharFormat format;
        QFont font = cursor.charFormat().font();
        font.setItalic(!font.italic());
        format.setFont(font);
        edit->mergeCurrentCharFormat(format);
    }
}

void TextFormatUtils::toggleUnderline() {
    QPlainTextEdit* edit = getFocusedPlainTextEdit();
    if (edit) {
        QTextCursor cursor = edit->textCursor();
        QTextCharFormat format;
        QFont font = cursor.charFormat().font();
        font.setUnderline(!font.underline());
        format.setFont(font);
        edit->mergeCurrentCharFormat(format);
    }
}

void TextFormatUtils::toggleOverline() {
    QPlainTextEdit* edit = getFocusedPlainTextEdit();
    if (edit) {
        QTextCursor cursor = edit->textCursor();
        QTextCharFormat format;
        QFont font = cursor.charFormat().font();
        font.setOverline(!font.overline());
        format.setFont(font);
        edit->mergeCurrentCharFormat(format);
    }
}
