/*
 * Copyright (C) 2023 ~ 2023 Deepin Technology Co., Ltd.
 *
 * Author:     donghualin <donghualin@uniontech.com>
 *
 * Maintainer: donghualin <donghualin@uniontech.com>
 *
 * 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
 * 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 <http://www.gnu.org/licenses/>.
 */
#include "iconmanager.h"
#include "dockplugincontroller.h"
#include "pluginsiteminterface.h"

#include <DDciIcon>
#include <DWindowManagerHelper>
#include <DSysInfo>
#include <DPlatformTheme>

#include <QPainter>
#include <QPainterPath>

#define ITEMSPACE 6
#define IMAGESIZE 12
#define ITEMSIZE 18
#define MINISIZE 1
#define STARTPOS 2

DGUI_USE_NAMESPACE

IconManager::IconManager(DockPluginController *pluginController, QObject *parent)
    : QObject{parent}
    , m_pluginController(pluginController)
    , m_position(Dock::Position::Bottom)
    , m_displayMode(Dock::DisplayMode::Efficient)
{
}

void IconManager::setPosition(Dock::Position position)
{
    m_position = position;
}

void IconManager::setDisplayMode(Dock::DisplayMode displayMode)
{
    m_displayMode = displayMode;
}

QPixmap IconManager::pixmap(DGuiApplicationHelper::ColorType colorType) const
{
    // 缺省图标
    QPixmap pixmap = QIcon::fromTheme("dock-control-panel").pixmap(ITEMSIZE, ITEMSIZE);
    QColor foreColor = (colorType == DGuiApplicationHelper::ColorType::DarkType ? Qt::white : Qt::black);
    foreColor.setAlphaF(0.8);
    QPainter pa(&pixmap);
    pa.setCompositionMode(QPainter::CompositionMode_SourceIn);
    pa.fillRect(pixmap.rect(), foreColor);
    return pixmap.scaled(pixmap.size() / qApp->devicePixelRatio());
}

PluginsItemInterface *IconManager::findPlugin(const QString &pluginName) const
{
    QList<PluginsItemInterface *> plugins = m_pluginController->currentPlugins();
    for (PluginsItemInterface *plugin : plugins) {
        if (plugin->pluginName() == pluginName)
            return plugin;
    }

    return nullptr;
}
