// Copyright (C) 2019 ~ 2020 Uniontech Software Technology Co.,Ltd
// SPDX-FileCopyrightText: 2022 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later

#include "encodelistmodel.h"
#include "utils.h"

#include <DStandardItem>
#include <DLog>

#include <QTextCodec>
#include <QRegExp>

DWIDGET_USE_NAMESPACE

#ifdef QT_DEBUG
Q_LOGGING_CATEGORY(encodeplugin,"org.deepin.terminal.encodeplugin")
#else
Q_LOGGING_CATEGORY(encodeplugin,"org.deepin.terminal.encodeplugin",QtInfoMsg)
#endif

EncodeListModel::EncodeListModel(QObject *parent) : QStandardItemModel(parent)
{
    qCDebug(encodeplugin) << "EncodeListModel constructor enter";
    Utils::set_Object_Name(this);
    initEncodeData();
    qCDebug(encodeplugin) << "EncodeListModel constructor exit";
}

int EncodeListModel::rowCount(const QModelIndex &parent) const
{
    qCDebug(encodeplugin) << "rowCount enter";
    Q_UNUSED(parent);
    //
    int count = m_encodeData.count();
    qCDebug(encodeplugin) << "rowCount exit, count:" << count;
    return count;
}

QVariant EncodeListModel::data(const QModelIndex &index, int role) const
{
    qCDebug(encodeplugin) << "data enter, row:" << index.row() << "role:" << role;
    Q_UNUSED(role);
    const int row = index.row();
    QVariant result = m_encodeData[row];
    qCDebug(encodeplugin) << "data exit, result:" << result;
    return result;
}

QList<QByteArray> EncodeListModel::listData()
{
    qCDebug(encodeplugin) << "listData enter";
    QList<QByteArray> result = m_encodeData;
    qCDebug(encodeplugin) << "listData exit, count:" << result.count();
    return result;
}

void EncodeListModel::initEncodeData()
{
    QList<QByteArray> showEncodeList;
    showEncodeList << "UTF-8" << "GB18030" << "GB2312" << "GBK" /*简体中文*/
                   << "BIG5" << "BIG5-HKSCS" //<< "EUC-TW"      /*繁体中文*/
                   << "EUC-JP"  << "SHIFT_JIS"  //<< "ISO-2022-JP"/*日语*/
                   << "EUC-KR" //<< "ISO-2022-KR" //<< "UHC"      /*韩语*/
                   << "IBM864" << "ISO-8859-6" << "ARABIC" << "WINDOWS-1256"   /*阿拉伯语*/
                   //<< "ARMSCII-8"    /*美国语*/
                   << "ISO-8859-13" << "ISO-8859-4" << "WINDOWS-1257"  /*波罗的海各国语*/
                   << "ISO-8859-14"    /*凯尔特语*/
                   << "IBM-852" << "ISO-8859-2" << "x-mac-CE" << "WINDOWS-1250" /*中欧*/
                   //<< "x-mac-CROATIAN"  /*克罗地亚*/
                   << "IBM855" << "ISO-8859-5"  << "KOI8-R" << "MAC-CYRILLIC" << "WINDOWS-1251" //<< "ISO-IR-111" /*西里尔语*/
                   << "CP866" /*西里尔语或俄语*/
                   << "KOI8-U" << "x-MacUkraine" /*西里尔语或乌克兰语*/
                   //<< "GEORGIAN-PS"
                   << "ISO-8859-7" << "x-mac-GREEK" << "WINDOWS-1253"  /*希腊语*/
                   //<< "x-mac-GUJARATI"
                   //<< "x-mac-GURMUKHI"
                   << "IBM862" << "ISO-8859-8-I" << "WINDOWS-1255"//<< "x-mac-HEBREW"  /*希伯来语*/
                   << "ISO-8859-8" /*希伯来语*/
                   //<< "x-mac-DEVANAGARI"
                   //<< "x-mac-ICELANDIC" /*冰岛语*/
                   << "ISO-8859-10"     /*北欧语*/
                   //<< "x-mac-FARSI"     /*波斯语*/
                   //<< "x-mac-ROMANIAN" //<< "ISO-8859-16" /*罗马尼亚语*/
                   << "ISO-8859-3"      /*西欧语*/
                   << "TIS-620"         /*泰语*/
                   << "IBM857" << "ISO-8859-9" << "x-mac-TURKISH" << "WINDOWS-1254" /*土耳其语*/
                   << "WINDOWS-1258" //<< "TCVN" << "VISCII"  /*越南语*/
                   << "IBM850" << "ISO-8859-1" << "ISO-8859-15" << "x-ROMAN8" << "WINDOWS-1252"; /*西方国家*/

    // 自定义的名称，系统里不一定大小写完全一样，再同步一下。
    QList<QByteArray> all = QTextCodec::availableCodecs();
    for (const QByteArray &name : showEncodeList) {
        QString strname1 = name;
        bool bFind = false;
        QByteArray encodename;
        for (const QByteArray &name2 : all) {
            QString strname2 = name2;
            if (0 == strname1.compare(strname2, Qt::CaseInsensitive)) {
                bFind = true;
                encodename = name2;
                break;
            }
        }
        if (!bFind)
            qCWarning(encodeplugin) << "The encoding(" << name << ") in the terminal encoding list is not found in the list of supported encodeings!";
        else
            m_encodeData << encodename;
    }

    qCInfo(encodeplugin) << "The number("<<  m_encodeData.count() << ") of system encoding formats supported by the terminal.";
}
