#ifndef REPLACEBACKGROUNDWIDGET_H
#define REPLACEBACKGROUNDWIDGET_H

#include <QWidget>
#include <QtDebug>
#include <QImage>
#include <QMessageBox>
#include <QColorDialog>
#include <QPushButton>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QFrame>
#include <QLabel>
#include <QAction>
#include <QPalette>
#include <QApplication>
#include <QFileDialog>
#include <QGraphicsDropShadowEffect>
#include <QtQuickWidgets/QQuickWidget>
#include <QQmlEngine>
#include <QStackedWidget>
#include <QListWidget>
#include <kborderlessbutton.h>
#include <kpushbutton.h>
#include "controller/interaction.h"
#include "view/sizedate.h"
#include "global/variable.h"
#include "colordialog.h"
#include "windowmanager/windowmanager.h"

class ReplaceBackgroundWidget : public QWidget
{
    Q_OBJECT
public:
    explicit ReplaceBackgroundWidget(QWidget *parent = nullptr);

    QLabel *m_showImage;
    QQuickWidget *m_showImageQml = nullptr;

    QStackedWidget *m_rightStackWidget = nullptr; //右侧抠图+裁剪堆叠布局
    QStackedWidget *m_leftStackWidget = nullptr;  //左侧抠图裁剪工具栏堆叠布局

    QWidget *m_leftWidget = nullptr;
    QWidget *m_cutWidget = nullptr;
    QWidget *m_mattingWidget = nullptr;
    QWidget *m_mattingTopWidget = nullptr;
    QWidget *m_mattingMidWidget = nullptr;
    QWidget *m_mattingBottomWidget = nullptr;
    QWidget *m_maskWidget  = nullptr;
    QLabel *m_loadingLabel;

    QWidget *m_toolWid = nullptr;             //对比+工具栏
    QWidget *m_toolBtnWid = nullptr;          //工具栏按钮
    QWidget *m_zoomWid = nullptr;             //缩、放按钮以及百分比

    QHBoxLayout *m_toolLayout;                //对比+工具栏布局
    QHBoxLayout *m_toolBtnLayout;             //工具栏布局
    QVBoxLayout *m_leftLayout;                //界面左侧布局
    QVBoxLayout *m_mattingLayout;             //抠图布局
    QHBoxLayout *m_mattingTopLayout;          //抠图上面背景更换布局
    QVBoxLayout *m_mattingMidLayout;          //抠图中间纯色/图片按钮布局
    QHBoxLayout *m_mattingBottomLayout;       //抠图下面保存取消布局

    QHBoxLayout *m_colorCustomLayout;         //颜色label + 自定义布局
    QGridLayout *m_colorBtnLayout;            //颜色按钮布局
    QGridLayout *m_picBtnLayout;              //图片按钮布局
    QHBoxLayout *m_layout;                    //界面整体布局
    QHBoxLayout *m_customBtnLayout;

    kdk::KPushButton *m_compare;              //对比
    kdk::KBorderlessButton *m_reduce;         //缩小
    QLabel *m_percentage;                     //百分比
    kdk::KBorderlessButton *m_enlarge;        //放大
    kdk::KBorderlessButton *m_originalSize;   //原尺寸
    kdk::KBorderlessButton *m_adaptiveWidget; //适应窗口
    kdk::KBorderlessButton *m_rotate;         //旋转
    kdk::KBorderlessButton *m_flipH;          //水平翻转
    kdk::KBorderlessButton *m_flipV;          //垂直翻转
    kdk::KBorderlessButton *m_cutImage;       //裁剪

    QLabel *m_bg;                             //背景更换label
    QLabel *m_bgLabel1;                       //背景更换图片
    QLabel *m_bgLabel2;                       //背景更换文字
    QLabel *m_pureColor;                      //纯色label
    QLabel *m_pic;                            //图片label
    QPushButton *m_pullDownBtnBg;             //背景更换下拉按钮
    QPushButton *m_customBtn;                 //自定义颜色按钮
    ColorDialog *m_colorDialog;               //颜色选择器
    QLabel *m_customBtnLeft;
    QLabel *m_customBtnRight;
    QPushButton *m_transparent;               //透明
    QPushButton *m_white;                     //白
    QPushButton *m_grey;                      //灰
    QPushButton *m_dimGrey;                   //深灰
    QPushButton *m_dark;                      //黑
    QPushButton *m_blue;                      //蓝
    QPushButton *m_red;                       //红
    QPushButton *m_darkOrange;                //深橙
    QPushButton *m_orange;                    //橙
    QPushButton *m_green;                     //绿
    QPushButton *m_purple;                    //紫
    QPushButton *m_pink;                      //粉
    kdk::KPushButton *m_addPicBtn;            //添加图片按钮
    QPushButton *m_pic1;                      //默认图片1
    QPushButton *m_pic2;
    QPushButton *m_pic3;
    QPushButton *m_pic4;
    QPushButton *m_pic5;
    QList<QPushButton*> m_list;
    QPushButton *m_saveBtn;                   //保存按钮
    QPushButton *m_cancelBtn;                 //取消按钮

    QHBoxLayout *m_cutToolBtnLayout;
    QHBoxLayout *m_cutBtnLayout;
    QVBoxLayout *m_cutLayout;                 //尺寸裁剪布局
    QHBoxLayout *m_cutTopLayout;              //尺寸裁剪顶部布局
    QVBoxLayout *m_cutMidLayout;
    QVBoxLayout *m_sizeLayout;
    QVBoxLayout *m_cutBottomLayout;
    QVBoxLayout *m_scaleLayout;

    QWidget *m_cutToolBtnWid;
    QWidget *m_cutBtnWid = nullptr;
    QPushButton *m_cutCancel;
    QPushButton *m_cutSave;

    QWidget *m_cutTopWid = nullptr;           //尺寸裁剪顶部wid
    QWidget *m_cutMidWid = nullptr;
    QWidget *m_cutBottomWid = nullptr;
    QLabel *m_cutLabel1;                      //尺寸裁剪图片
    QLabel *m_cutLabel2;                      //尺寸裁剪文字
    QPushButton *m_pullDownBtnCut;             //尺寸裁剪下拉按钮
    QLabel *m_sizeLabel;                      //尺寸label
    QListWidget *m_sizeWid = nullptr;         //尺寸wid
//    QWidget *m_sizeWid = nullptr;
    QListWidgetItem *m_lifeSize;              //原图尺寸
    QListWidgetItem *m_freeSize;              //自由尺寸
    QListWidgetItem *m_standardOneInch;       //标准一寸
    QListWidgetItem *m_standardTwoInch;       //标准二寸
    QListWidgetItem *m_smallOneInch;          //小一寸
    QListWidgetItem *m_smallTwoInch;          //小二寸
    QListWidgetItem *m_idCardSize;            //身份证照尺寸
    QLabel *m_scaleLabel;                     //比例label
    QListWidget *m_scaleWid = nullptr;        //比例wid
//    QWidget *m_scaleWid = nullptr;
    QListWidgetItem *m_originalScale;         //原比例
    QListWidgetItem *m_fourToThree;           //4:3标准横屏
    QListWidgetItem *m_threeTofour;           //3:4标准竖屏
    QListWidgetItem *m_sixteenToNine;         //16:9视频横屏
    QListWidgetItem *m_nineToSixteen;         //9:16视屏竖屏
    QListWidgetItem *m_oneToOne;              //1:1正方形

    QString m_filePath;
    QString widRadius = QString("6px");
    QString normalIconPath = QString("sp1");
    QString nowThemeStyle;
    QPixmap m_replaceImage;                   //替换背景之后的图片
    QPixmap m_origImage;                      //原图
    QColor m_color;                           //当前颜色选择器选取的颜色

    void initLayout();                        //初始化布局
    void initConnect();                       //初始化信号连接
    void setColorBtnStyle();                  //设置纯色按钮样式
    void setBtnIcon();                        //设置按钮图标
    void toolBtnIconSize(QSize size);         //工具栏按钮图标尺寸设置
    void toolButtonSize(QSize size);          //工具栏按钮尺寸设置
    void colorButtonSize(QSize size);         //颜色按钮尺寸设置
    void pictureBtnSize(QSize size);          //图片按钮尺寸设置
    void pictureBtnIconSize(QSize size);      //图片按钮图标尺寸设置
    void platformType();
    void initGsetting();                      //监听主题
    void changeStyle();                       //根据主题改变样式表
    void initQmlObject();                     //初始化qml
    void setFormatedText();                   //智能抠图中

    void initMattingWidget();                 //初始化抠图wid
    void initCutWidget();                     //初始化裁剪wid

protected:
    bool eventFilter(QObject *obj, QEvent *event);
    void resizeEvent(QResizeEvent *event);

public Q_SLOTS:
    void startReplaceBackground();                      //开始背景更换
    void exitReplaceBackground();                       //退出背景更换
    void getReplaceAndOrigImage(QImage replaceImage, QImage origImage);
    void needSave();                                    //备份保存图片
    void reduceImage();                                 //缩小图片
    void enlargeImage();                                //放大图片
    void originalSize();                                //原始尺寸
    void adaptiveWidget();                              //适应窗口
    void rotate();                                      //逆时针旋转
    void flipH();                                       //水平翻转
    void flipV();                                       //垂直翻转
    void cutImage();                                    //裁剪
    void toHideOrShow();                                //隐藏或显示背景切换按钮
    void colorSelected(QColor color);                   //颜色选择器选择颜色
    void addPicture();                                  //添加背景图片
    void onSizeItemClicked(QListWidgetItem *item);      //尺寸item槽函数
    void onScaleItemClicked(QListWidgetItem *item);     //比例item槽函数
    void toMattingPage();

Q_SIGNALS:
    void toBackShowImageWid();
    void backgroundColor(unsigned b, unsigned g, unsigned r);

};

#endif // REPLACEBACKGROUNDWIDGET_H
