#include "taskbarmanager.h"
#include <QDateTime>
#include <QPixmap>
#include <QFile>
#include <kdesktopfile.h>
#include <QList>

TaskBarManager::TaskBarManager(ModelManager *parent)
    : QObject(parent)
    , m_taskbarModelManager(parent)
    , m_appManagerInterface(new AppManagerInterface("com.kylin.AppManager",
                                                    "/com/kylin/AppManager",
                                                    QDBusConnection::sessionBus()))

{
    if (!m_appManagerInterface->isValid()) {
        qWarning() << "Connect to com.kylin.AppManager dbus service failed!"
                   << m_appManagerInterface->lastError();
    }
    connect(kdk::WindowManager::self(),&kdk::WindowManager::windowAdded,this,&TaskBarManager::appWindowOpen);
    connect(kdk::WindowManager::self(),&kdk::WindowManager::windowRemoved,this,&TaskBarManager::setAppClose);
}

TaskBarManager::~TaskBarManager()
{
}

void TaskBarManager::setWindowDesktopFileName(quint32 wid, const QString &fileName)
{
    m_isOpenWindows[fileName].push_back(wid);
}

void TaskBarManager::appWindowOpen(kdk::WindowId wid)
{
    qDebug() << "open----" <<wid;
    for(auto widList = m_isOpenWindows.begin(); widList != m_isOpenWindows.end(); widList++) {
        for(auto j = 0; j < widList->count(); j++) {
            if (widList->at(j) == wid) {
                qDebug() << "avtivatewindow wid->" << widList->at(j);
                kdk::WindowManager::activateWindow(wid);
                return;
            }
        }
    }
    QString desktopName = m_appManagerInterface->AppDesktopFileNameByPid(kdk::WindowManager::getPid(wid));
    qDebug() << "desktopName---->" <<desktopName;
    if (desktopName.isEmpty()) {
        qDebug() << "desktop is null";
        return;
    }

    QString desktopFileName = m_taskbarModelManager->findDesktopNameFromTaskbar(desktopName);
    if (desktopFileName.isEmpty())
    {
        desktopFileName = m_taskbarModelManager->findDesktopName(desktopName);
        if (desktopFileName.isEmpty())
        {
            return;
        }
        else
        {
            QString iconFileName = m_taskbarModelManager->findIconPath(desktopFileName);
            QString appName = m_taskbarModelManager->findAppName(desktopFileName);
            m_isOpenWindows[desktopName].push_back(wid);
            m_taskbarModelManager->addWindowIconToTaskbar(iconFileName, desktopFileName, appName, wid.toULongLong());
        }
    }
    else
    {
        // 该应用在任务栏，设置为打开状态
        m_taskbarModelManager->setAppOpenState(desktopName, wid.toULongLong());
        m_isOpenWindows[desktopName].push_back(wid);
    }
}
void TaskBarManager::setAppClose(kdk::WindowId wid)
{
    qDebug() << "app close wid:" << wid;
    for(auto widList = m_isOpenWindows.begin(); widList != m_isOpenWindows.end(); widList++) {
        for(auto j = 0; j < widList->count(); j++) {
            if (widList->at(j) == wid) {
                if (widList->count() == 1) {
                    QString desktopName = widList.key();
                    if (m_taskbarModelManager->findDesktopName(desktopName).isEmpty()) {
                        m_taskbarModelManager->setAppCloseState(desktopName);
                    } else {
                        m_taskbarModelManager->removeAppIconFromTaskBar(desktopName);
                    }
                    m_isOpenWindows.erase(widList);
                } else {
                    qDebug() << "remove window" <<widList->at(j);
                    widList->removeAt(j);

                    QString desktopName = m_appManagerInterface->AppDesktopFileNameByPid(kdk::WindowManager::getPid(wid));
                    m_taskbarModelManager->changeAppWindowWid(desktopName, wid.toULongLong(), widList->at(0).toULongLong());
                }
                return;
            }
        }
    }
}
