/*
 * Copyright (C) 2020, KylinSoft Co., Ltd.
 *
 * 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 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */
#include "aiassistant-menu-plugin.h"

using namespace Peony;

AiassistantMenuPlugin::AiassistantMenuPlugin(QObject *parent) : QObject (parent)
{
    QTranslator *t = new QTranslator(this);
    qDebug()<<"system().name:"<<QLocale::system().name();
    qDebug()<<"\n\n\n\n\n\n\ntranslate:"<<t->load("/usr/share/kylin-aiassistant/translations/peony-menu-plugin-kylin-aiassistant_"+QLocale::system().name());
    QApplication::installTranslator(t);
}

QList<QAction *> AiassistantMenuPlugin::menuActions(MenuPluginInterface::Types types, const QString &uri, const QStringList &selectionUris)
{
    QList<QAction*> actions;
    if (types == MenuPluginInterface::DirectoryView || types == MenuPluginInterface::DesktopWindow)
    {
        if (selectionUris.isEmpty() || selectionUris.size() > 1) {
            return actions;
        }

        auto info = FileInfo::fromUri(selectionUris.first());
        if (info->isDir()){
            return actions;
        }
        //special type mountable, return
        qDebug()<<"info isVirtual:"<<info->isVirtual()<<info->mimeType();
        if (selectionUris.first().startsWith("computer:///") ||
            selectionUris.first().startsWith("trash:///") ||
            selectionUris.first().startsWith("recent:///") ||
            selectionUris.first().startsWith("filesafe:///") ||
            info->isVirtual())
              return actions;

        QFileInfo file(selectionUris.first());
        QIcon icon(":/res/AI-Writing.svg");
        QAction *sendToAI = new QAction(icon, tr("send to AI assistant"), nullptr);
        actions<<sendToAI;
        connect(sendToAI, &QAction::triggered, [=](){
            qDebug()<<"send file:"<<uri<<selectionUris;
            QProcess p;
            p.setProgram("kylin-aiassistant");
            p.setArguments(QStringList()<<"-f"<<selectionUris.first());
            p.startDetached(p.program(), p.arguments());
            p.waitForFinished(-1);
        });

    }


    return actions;
}
