/*
 *   Copyright © 2015 Robert Metsäranta <therealestrob@gmail.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 2 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; see the file COPYING.  if not, write to
 *   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 *   Boston, MA 02110-1301, USA.
 */

#include "ShapeCornersEffect.h"
#include <KX11Extras>
#include <QDBusError>

#include <kwineffects.h>
#include <kwinglutils.h>

namespace KWin {

ShapeCornersEffect::ShapeCornersEffect()
    :OffscreenEffect()
{
    reconfigure(ReconfigureAll);

    if (m_shaderManager.IsValid()) {
        for (const auto &win : effects->stackingOrder())
            windowAdded(win);
        connect(effects, &EffectsHandler::windowAdded, this, &ShapeCornersEffect::windowAdded);
        connect(effects, &EffectsHandler::windowDeleted, this, &ShapeCornersEffect::windowRemoved);
    }
}

ShapeCornersEffect::~ShapeCornersEffect() = default;

void ShapeCornersEffect::windowAdded(EffectWindow *w)
{
    if (!hasRoundCorners(w))
        return;

    connect(effects, &EffectsHandler::windowFrameGeometryChanged, this, &ShapeCornersEffect::windowResized);
    redirect(w);
    setShader(w, m_shaderManager.GetShader().get());
}

void ShapeCornersEffect::windowRemoved(EffectWindow *w)
{
}

void ShapeCornersEffect::reconfigure(const ReconfigureFlags flags)
{
    Q_UNUSED(flags)
}

bool ShapeCornersEffect::hasRoundCorners(EffectWindow *w)
{
    if (w->noBorder() || !w->isManaged()) {
        return false;
    }
    return true;
}

void ShapeCornersEffect::prePaintWindow(EffectWindow *w, WindowPrePaintData &data, std::chrono::milliseconds time)
{
    if (hasRoundCorners(w)) {
        const auto geo = w->frameGeometry() * effects->renderTargetScale();
        const auto size = w->windowRadius() >= 0 ? w->windowRadius() * effects->renderTargetScale() : m_cornerSize * effects->renderTargetScale();

        QRegion reg{};
        reg += QRect(geo.x(), geo.y(), size, size);
        reg += QRect(geo.x() + geo.width() - size, geo.y(), size, size);
        reg += QRect(geo.x(), geo.y() + geo.height() - size, size, size);
        reg += QRect(geo.x() + geo.width() - size, geo.y() + geo.height() - size, size, size);
        data.opaque -= reg;
        data.paint += reg;
        data.setTranslucent();
    }

    OffscreenEffect::prePaintWindow(w, data, time);
}

bool ShapeCornersEffect::supported()
{
    return effects->isOpenGLCompositing();
}

void ShapeCornersEffect::drawWindow(EffectWindow *w, int mask, const QRegion &region,
                                    WindowPaintData &data)
{
    if (!hasRoundCorners(w)) {
        unredirect(w);
        OffscreenEffect::drawWindow(w, mask, region, data);
        return;
    }

    const auto scale = effects->renderTargetScale();

    redirect(w);
    setShader(w, m_shaderManager.GetShader().get());
    m_shaderManager.Bind(w, scale);
    glActiveTexture(GL_TEXTURE0);

    OffscreenEffect::drawWindow(w, mask, region, data);
    m_shaderManager.Unbind();
}

void ShapeCornersEffect::windowResized(EffectWindow *window, const QRectF &)
{
}

}