#include "clipboarddata.h"
#include <QDebug>
#include <QBuffer>
#include <QMimeData>
#include <QFileInfo>
#include <QFileIconProvider>

clipboardData::clipboardData(QObject *parent)
    : QObject(parent), m_sourceText("")
{

}

QString clipboardData::id()
{
    return m_id;
}

void clipboardData::setId(QString str)
{
    m_id = str;
}

bool clipboardData::hasImage()
{
    return m_hasImage;
}

void clipboardData::setHasImage(bool value)
{
    m_hasImage = value;
    emit HasImageChanged();
}

bool clipboardData::isLinkMode()
{
    return m_isLinkMode;
}

void clipboardData::setIsLinkMode(bool value)
{
    m_isLinkMode = value;
    emit isLinkModeChanged();
}

QUrl clipboardData::imageUrl()
{
    return m_imageUrl;
}

void clipboardData::setImage(QImage ima)
{
    QByteArray bytes;
    QBuffer buffer(&bytes);
    buffer.open(QIODevice::WriteOnly);
    ima.save(&buffer, "PNG"); // saves the image into bytes in PNG format
    m_imageUrl = QString("data:image/png;base64,") + bytes.toBase64();
    emit imageUrlChanged();
}

QString clipboardData::sourceText()
{
    return m_sourceText;
}

void clipboardData::setSourceText(QString str)
{
    m_sourceText = str;
    emit sourceTextChanged();
}

QDateTime clipboardData::sourceTime()
{
    return m_sourceTime;
}

void clipboardData::setSourceTime(QDateTime dataTime)
{
    m_sourceTime = dataTime;
    emit sourceTimeChanged();
}

bool clipboardData::isCollected()
{
    return m_isCollected;
}

void clipboardData::setIsCollected(bool flag)
{
    qDebug()<<"flag="<<flag;
    if (m_isCollected != flag) {
        m_isCollected = flag;
        emit isCollectedChanged();
    }
}

void clipboardData::copyToClipboard()
{
    if (m_mineData == nullptr || m_dataClipboard == nullptr) {
        qWarning()<<"数据为空";
    } else {
        // 设置新的QMimeData到剪贴板
        m_dataClipboard->setMimeData(m_mineData);
        emit dataCopyed();
    }
}

//QClipboard::setMimeData(QMineData *mimeData)函数在执行后
//mimeData指针会出现异常，用该指针调用QMineData::hasUrls()等函数qt会报错
//再次将该指针用为setMimeData（）函数的参数qt也会报莫名错误
QMimeData* clipboardData::getMimedata(QMimeData* mData)
{
    QMimeData *newMimeData = new QMimeData();

    // 如果QMimeData包含URL列表，则也复制URL列表
    if (mData->hasUrls()) {
        QList<QUrl> urls = mData->urls();
        newMimeData->setUrls(urls);
    }

    // 遍历QMimeData支持的所有格式并复制数据
    foreach (QString format, mData->formats()) {
        QByteArray data = mData->data(format);
        newMimeData->setData(format, data);
    }

    // 如果QMimeData包含图像，则也复制图像
    if (mData->hasImage()) {
        QImage image = qvariant_cast<QImage>(mData->imageData());
        newMimeData->setImageData(QVariant::fromValue(image));
    }

    // 如果QMimeData包含HTML，则也复制HTML
    if (mData->hasHtml()) {
        QString html = mData->html();
        newMimeData->setHtml(html);
    }

    // 如果QMimeData包含文本，则也复制文本
    if (mData->hasText()) {
        QString text = mData->text();
        newMimeData->setText(text);
    }
    return newMimeData;
}

void clipboardData::sendNeedMoveToFirst() {
    if (m_mineData == nullptr || m_dataClipboard == nullptr) {
        qWarning()<<"数据为空";
    } else {
        // 设置新的QMimeData到剪贴板
        m_dataClipboard->setMimeData(getMimedata(m_mineData));
        emit needMoveToFirst();
    }
}

void clipboardData::updateImage()
{
    QString localFilePath = m_mineData->urls().at(0).toLocalFile();
    QFileInfo fileInfo(localFilePath);
    QFileIconProvider iconProvider;
    QIcon fileIcon = iconProvider.icon(fileInfo);
    int width = 48, height = 48;  // 你可以根据需要调整这个尺寸
    QPixmap pixmap = fileIcon.pixmap(width, height);
    QImage image = pixmap.toImage();
    setImage(image);
}
