/*
* Copyright (C) 2021, 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 3, 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, see <http://www.gnu.org/licenses/&gt;.
*
*/

#include "imageoperationwatermark.h"
#include <QPixmap>
#include <QFont>
#include <QPainter>
#include <cmath>
#include "../include/common.h"
#include <QDebug>

imageOperationWaterMark::imageOperationWaterMark(QImage *origin, QStack<QImage> *stackImage)
{
    m_origin = origin;
    m_stackImage = stackImage;
}
void imageOperationWaterMark::ImageOP(){
    QImage opImage = imageCopy(m_origin,m_stackImage);
    waterMark(&opImage);
}
void imageOperationWaterMark::waterMark(QImage *opImage){
    QPixmap pix;
    pix = QPixmap::fromImage(*opImage);

    qDebug() << "pix size: " << pix.size();


    int spacing = 5;
    int fontSize = pix.width()/30;

    QFont font;
    font.setFamily("NotoSansCJKsc-Regular, NotoSansCJKsc;");
    font.setPointSize(fontSize);
    font.setWeight(400);
    font.setLetterSpacing(QFont::AbsoluteSpacing, spacing);

    QColor color;
    color.setRed(0);
    color.setGreen(0);
    color.setBlue(0);
    color.setAlphaF(0.2);

    QPainter painter(&pix);
    painter.setFont(font);
    painter.setPen(color);
    painter.rotate(15);

    int squareEdgeSize = opImage->width() * sin(45) + opImage->height() * sin( 45);
    int hCount = squareEdgeSize / ((fontSize + spacing) * (m_text.size() + 1)) + 1;
    int x = squareEdgeSize / hCount + (fontSize + spacing) * 3;
    int y = x / 2;

    qDebug() << "hcount = " << hCount
             << "squareEdgeSize = " << squareEdgeSize
             << "(x,y) = " << x << y;

    for (int i = 0; i < hCount; ++i) {
        for (int j = 0; j < hCount * 2; ++j) {
            painter.drawText(x * i, y * j, m_text);
//                KyInfo() << "drawtext: " << text;
        }
    }

    QImage transformImg = pix.toImage();
    transformImg.save(ScanningPicture);
    painter.end();
}
void imageOperationWaterMark::setText(QString text){
    m_text = text;
}
