#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(KWindowSystem::self(),&KWindowSystem::windowAdded,this,&TaskBarManager::appWindowOpen);
    connect(KWindowSystem::self(),&KWindowSystem::windowRemoved,this,&TaskBarManager::setAppClose);
}

TaskBarManager::~TaskBarManager()
{
}

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

void TaskBarManager::appWindowOpen(WId 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);
                KWindowSystem::activateWindow(wid);
                return;
            }
        }
    }
    KWindowInfo winInfo(wid, NET::WMDesktop, NET::WM2DesktopFileName);
    qDebug() << "---open pid--->" << winInfo.pid();

    QString desktopName = m_appManagerInterface->AppDesktopFileNameByPid(winInfo.pid());
    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);
        }
    }
    else
    {
        // 该应用在任务栏，设置为打开状态
        m_taskbarModelManager->setAppOpenState(desktopName, wid);
        m_isOpenWindows[desktopName].push_back(wid);
    }
}
void TaskBarManager::setAppClose(WId 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);

                    KWindowInfo winInfo(wid, NET::WMDesktop, NET::WM2DesktopFileName);
                    QString desktopName = m_appManagerInterface->AppDesktopFileNameByPid(winInfo.pid());
                    m_taskbarModelManager->changeAppWindowWid(desktopName, wid, widList->at(0));
                }
                return;
            }
        }
    }
}
