/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */

/*
    Rosegarden
    A MIDI and audio sequencer and musical notation editor.
    Copyright 2000-2026 the Rosegarden development team.

    Other copyrights also apply to some parts of this work.  Please
    see the AUTHORS file and individual file headers for details.

    This program 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.  See the file
    COPYING included with this distribution for more information.
*/

#define RG_MODULE_STRING "[IdentifyTextCodecDialog]"
#define RG_NO_DEBUG_PRINT

#include "IdentifyTextCodecDialog.h"

#include "misc/Strings.h"
#include "misc/Debug.h"
#include "base/NotationTypes.h"

#include <QComboBox>
#include <QDialog>
#include <QDialogButtonBox>
#include <QFont>
#include <QSet>
#include <QLabel>
#include <QString>
#include <QTextCodec>
#include <QWidget>
#include <QVBoxLayout>
#include <QGroupBox>


namespace Rosegarden
{


// This dialog used to use QTextCodec::codecForContent() to guess an
// appropriate codec for the string, and then call
// QTextCodec::heuristicContentMatch() to test each listed codec to
// determine whether to include it in the dialog (if it has a non-zero
// probability).

// Unfortunately, both of those functions have been removed from Qt4.
// Not that either was all that effective in the first place.

// QTextCodec::canEncode is not a replacement; it tests whether a
// given QString can be encoded using a certain codec, and our problem
// is that we haven't got a QString to start with.

// Let's try this:

static bool
codecPreserves(QTextCodec &codec, const std::string& encoded)
{
    QString u = codec.toUnicode(encoded.c_str(), encoded.length());
    QByteArray d = codec.fromUnicode(u);
    if (encoded == d.data()) return true;
    else return false; // not at all authoritative
}

IdentifyTextCodecDialog::IdentifyTextCodecDialog(QWidget *parent,
                                                 const std::string &text) :
    QDialog(parent),
    m_exampleText(text)
{
    setWindowTitle(tr("Rosegarden"));
    setModal(true);

    QVBoxLayout *vboxLayout = new QVBoxLayout;
    setLayout(vboxLayout);

    vboxLayout->addWidget(new QLabel(tr("<qt><h3>Choose Text Encoding</h3></qt>")));

    QGroupBox *groupBox = new QGroupBox;
    QVBoxLayout *groupBoxLayout = new QVBoxLayout;
    vboxLayout->addWidget(groupBox, 20);
    groupBox->setLayout(groupBoxLayout);

    QLabel *l = new QLabel(tr("<qt><p>This file contains text in an unknown language encoding.</p><p>Please select one of the following estimated text encodings for use with the text in this file:</p></qt>"));
    l->setWordWrap(true);
    groupBoxLayout->addWidget(l);

    QComboBox *codecs = new QComboBox;
    groupBoxLayout->addWidget(codecs);

    // QTextCodec *codec = 0;

    QMap<QString, QString> codecDescriptions;
    codecDescriptions["UTF-8"] = tr("Unicode variable-width");
    codecDescriptions["ISO-8859-1"] = tr("Western Europe");
    codecDescriptions["ISO-8859-15"] = tr("Western Europe + Euro");
    codecDescriptions["ISO-8859-2"] = tr("Eastern Europe");
    codecDescriptions["ISO-8859-3"] = tr("Southern Europe");
    codecDescriptions["ISO-8859-4"] = tr("Northern Europe");
    codecDescriptions["ISO-8859-5"] = tr("Cyrillic");
    codecDescriptions["ISO-8859-6"] = tr("Arabic");
    codecDescriptions["ISO-8859-7"] = tr("Greek");
    codecDescriptions["ISO-8859-8"] = tr("Hebrew");
    codecDescriptions["ISO-8859-9"] = tr("Turkish");
    codecDescriptions["ISO-8859-10"] = tr("Nordic");
    codecDescriptions["ISO-8859-11"] = tr("Thai");
    codecDescriptions["ISO-8859-13"] = tr("Baltic");
    codecDescriptions["ISO-8859-14"] = tr("Celtic");
    codecDescriptions["SJIS"] = tr("Japanese Shift-JIS");
    codecDescriptions["Big5"] = tr("Traditional Chinese");
    codecDescriptions["GB18030"] = tr("Simplified Chinese");
    codecDescriptions["KOI8-R"] = tr("Russian");
    codecDescriptions["KOI8-U"] = tr("Ukrainian");
    codecDescriptions["TSCII"] = tr("Tamil");

    QList<int> mibs = QTextCodec::availableMibs();

    m_codecs.clear();

    QSet<QTextCodec *> seen;

    QMap<QString, int> codecWeights;
    codecWeights["UTF-8"] = 20;
    codecWeights["ISO-8859-1"] = 14;
    codecWeights["ISO-8859-15"] = 13;
    codecWeights["ISO-8859-2"] = 12;
    codecWeights["SJIS"] = 10;
    codecWeights["Big5"] = 10;
    codecWeights["GB18030"] = 10;
    codecWeights["KOI8-R"] = 10;
    int currentWeight = -1;
    // Best codec based on codecWeights and codecPreserves().
    QTextCodec *bestCodec = nullptr;
    int bestCodecIndex = -1;

    // For each codec...
    for (int mibListIndex = 0; mibListIndex < mibs.size(); ++mibListIndex) {

        int mib = mibs[mibListIndex];

        QTextCodec *codec = QTextCodec::codecForMib(mib);
        if (!codec)
            continue;

        // Already seen?  Try the next.
        if (seen.contains(codec))
            continue;

        seen.insert(codec);

        bool preserves = codecPreserves(*codec, m_exampleText);

        //RG_DEBUG << "ctor:" << mibListIndex << "codec " << codec->name().data() << " mib " << mib << " preserves " << preserves;

        QStringList names;
        names.push_back(QString::fromLatin1(codec->name()));
        // Add the aliases.
        foreach (QByteArray ba, codec->aliases()) {
            names.push_back(QString::fromLatin1(ba));
        }

        QString goodName;
        QString description;

        // For each name and alias for the current codec...
        foreach (QString name, names) {

            if (codecDescriptions.contains(name)) {
                goodName = name;
                description = codecDescriptions[name];
                //RG_DEBUG << "ctor: have description " << description.toStdString() << " for name " << name.toStdString();
                if (description == "") {
                    if (name.left(3) == "windows-") {
                        description = tr("Microsoft Code Page %1")
                            .arg(name.right(name.length() - 8));
                    }
                }
            } else {
                //RG_DEBUG << "ctor: have no description for name " << name.toStdString();
            }

            if (preserves) {
                int weight = 0;
                if (codecWeights.contains(name))
                    weight = codecWeights[name];

                // If we don't have a best codec yet, or this one's weight is
                // the best we've seen so far, pick this as the best codec.
                if (!bestCodec  ||  currentWeight < weight) {
                    bestCodec = codec;
                    currentWeight = weight;
                }
            }

            if (name[0].isUpper() && goodName == "") {
                goodName = name;
            }
        }

        if (goodName == "") {
            goodName = QString::fromLatin1(codec->name());
        }

        if (description != "") {
            description = tr("%1 (%2)").arg(goodName).arg(description);
        } else {
            description = goodName;
        }

        m_codecs.push_back(goodName);
        codecs->addItem(description);

        if (bestCodec == codec)
            bestCodecIndex = codecs->count() - 1;
    }

    if (bestCodecIndex < 0)
        bestCodecIndex = 0;
    codecs->setCurrentIndex(bestCodecIndex);

    connect(codecs,
                static_cast<void(QComboBox::*)(int)>(&QComboBox::activated),
            this, &IdentifyTextCodecDialog::slotCodecSelected);

    groupBoxLayout->addWidget(new QLabel(tr("\nExample text from file:")));
    m_example = new QLabel;

    // background: #fff3c3; color: black;
    QPalette pal;
    pal.setColor(QPalette::Base, QColor(0xff, 0xf3, 0xc3));
    pal.setColor(QPalette::Text, Qt::black);
    m_example->setPalette(pal);

    groupBoxLayout->addWidget(m_example, 20);
    QFont font;
    font.setStyleHint(QFont::TypeWriter);
    m_example->setFont(font);
    // Update the example text.
    slotCodecSelected(bestCodecIndex);

    // cppcheck-suppress constVariablePointer
    QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok);
    vboxLayout->addWidget(buttonBox);
    connect(buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
    connect(buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);

}

void
IdentifyTextCodecDialog::slotCodecSelected(int i)
{
    if (i < 0 || i >= (int)m_codecs.size()) return;
    m_codec = m_codecs[i];
    m_example->setText(getExampleText());
}

QString
IdentifyTextCodecDialog::getExampleText()
{
    QTextCodec *codec = QTextCodec::codecForName(m_codec.toLatin1());
    if (!codec)
        return "";

    //RG_DEBUG << "getExampleText(): codec->name() returns " << codec->name().data();

    int offset = 0;
    for (; offset + 80 < (int)m_exampleText.length(); ++offset) {
        if (!isascii(m_exampleText[offset])) {
            for (int i = 0; i < 80; ++i) {
                if (offset == 0) {
                    break;
                }
                --offset;
                if (m_exampleText[offset] == '\n') {
                    break;
                }
            }
            break;
        }
    }

    if (offset < 20) offset = 0;

    QString outText = codec->toUnicode(m_exampleText.c_str(), m_exampleText.length());
    outText = outText.mid(offset, 160);
    return outText;
}


}
