/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
 *
 * Copyright (C) 2019 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 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; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */
#include "pictureunit.h"
#include <QApplication>
#include "maskwidget.h"
#include <QTouchEvent>
#include <QGSettings>
#include <QDebug>
#include <qmath.h>
#include <QPainter>
#include <QPainterPath>

PictureUnit::PictureUnit(QWidget *parent, QSize size)
    : QLabel(parent)
{
    setAttribute(Qt::WA_DeleteOnClose);
    setFixedSize(size);
    setScaledContents(true);
    setAttribute(Qt::WA_AcceptTouchEvents);
    installEventFilter(this);

    m_maskLabel = new MaskLabel(this, true);
    m_maskLabel->setGeometry(0, 0, this->width(), this->height());
    m_maskLabel->hide();
}

PictureUnit::~PictureUnit()
{
}

void PictureUnit::setFilenameText(QString fn){
    m_filename = fn;
}

void PictureUnit::setColorName(QString colorName) {
    m_colorName = colorName;
}

QString PictureUnit::getColorName() {
    return m_colorName;
}

QString PictureUnit::filenameText(){
    return m_filename;
}

void PictureUnit::setBackground(QPixmap background)
{
    m_background = background;
    repaint();
}

void PictureUnit::setColor(QColor color)
{
    m_color = color;
    m_isColorUnit = true;
    repaint();
}

void PictureUnit::mouseReleaseEvent(QMouseEvent *e)
{
    if (e->button() == Qt::LeftButton) {
        m_clickedFlag = true;
        m_maskLabel->show();
        Q_EMIT clicked(m_filename);
    }
}

bool PictureUnit::getClickedFlag()
{
    return m_clickedFlag;
}

void PictureUnit::changeClickedFlag(bool flag)
{   
    m_clickedFlag = flag;
    m_maskLabel->setVisible(m_clickedFlag);
}

bool PictureUnit::eventFilter(QObject *w, QEvent *e)
{
    if (e->type() == QEvent::Enter) {
        m_maskLabel->show();
    } else if (e->type() == QEvent::Leave) {
        if (!m_clickedFlag) {
            m_maskLabel->hide();
        }
    }
    if (w == this) {
        if (e->type() == QEvent::TouchBegin) {
            m_isTouchControl = true;
            QTouchEvent *touchE = static_cast<QTouchEvent*>(e);
            m_touchBeginPoint = touchE->touchPoints().at(0).screenPos();
        }
    } else if (e->type() == QEvent::TouchEnd && m_isTouchControl) {
        m_isTouchControl = false;
        QTouchEvent *touchE = static_cast<QTouchEvent*>(e);
        m_touchEndPoint = touchE->touchPoints().at(0).screenPos();
        if (qFabs(m_touchEndPoint.x() - m_touchBeginPoint.x()) < 1.0 &&
            qFabs(m_touchEndPoint.y() - m_touchBeginPoint.y()) < 1.0) {
            emit clicked(m_filename);
        }
    }
    return QWidget::eventFilter(w, e);
}

void PictureUnit::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);
    painter.setRenderHint(QPainter::SmoothPixmapTransform);
    if (m_isColorUnit) {
        QPainterPath path;
        const QRect bgRect(1, 1, width() - 2, height() - 2);
        path.addRoundedRect(bgRect, 8, 8);
        painter.fillRect(rect(), palette().color(QPalette::Base));
        painter.setClipPath(path);
        painter.fillPath(path, m_color);
        painter.drawPath(path);
    } else {
        painter.fillRect(rect(), palette().color(QPalette::Base));
        const QRect bgRect(4, 4, width() - 8, height() - 8);
        QPainterPath path;
        path.addRoundedRect(bgRect, 8, 8);
        painter.setClipPath(path);
        painter.drawPixmap(bgRect, m_background, m_background.rect());
    }
}
