#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QBoxLayout>
#include <QColorDialog>
#include <QActionGroup>
#include <QButtonGroup>

namespace {
    Viewer* view;
}

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
	ui(new Ui::MainWindow),
    colordialog(new QColorDialog)
{
    ui->setupUi(this);

    lightTypeGroup = new QActionGroup(this);
    lightTypeGroup->addAction(ui->actionGouraud);
    lightTypeGroup->addAction(ui->actionPhong);

    minFilterGroup = new QActionGroup(this);
    minFilterGroup->addAction(ui->actionMinLin);
    minFilterGroup->addAction(ui->actionMinNear);

    magFilterGroup = new QActionGroup(this);
    magFilterGroup->addAction(ui->actionMagLinear);
    magFilterGroup->addAction(ui->actionMagNear);

    controlGroup = new QButtonGroup(this);
    controlGroup->addButton(ui->action_delete);
    controlGroup->addButton(ui->ccw_x);
    controlGroup->addButton(ui->cw_x);
    controlGroup->addButton(ui->ccw_y);
    controlGroup->addButton(ui->cw_y);
    controlGroup->addButton(ui->ccw_z);
    controlGroup->addButton(ui->cw_z);


	connect(ui->pushButton, SIGNAL(clicked(bool)), this, SLOT(onColorPickerActivate()));
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::addViewer(Viewer* viewer)
{
    QLayout *layout = new QHBoxLayout;
    layout->addWidget(viewer);
    ui->frame->setLayout(layout);

    // actionGouraud shouldn't need one, since they are mutual
    connect(ui->actionPhong, SIGNAL(toggled(bool)), viewer, SLOT(setPhong(bool)));
    connect(ui->actionMagLinear, SIGNAL(toggled(bool)), viewer, SLOT(setMagLinear(bool)));
    connect(ui->actionMinLin, SIGNAL(toggled(bool)), viewer, SLOT(setMinLinear(bool)));

    connect(ui->actionNormal_Maps, SIGNAL(toggled(bool)), viewer, SLOT(toggleNormalMaps(bool)));

    connect(ui->action_delete, SIGNAL(clicked(bool)), viewer, SLOT(deleteSelected()));

    connect(viewer, SIGNAL(cubeSelected(bool)), ui->action_delete, SLOT(setEnabled(bool)));
    connect(viewer, SIGNAL(cubeSelected(bool)), ui->ccw_x, SLOT(setEnabled(bool)));
    connect(viewer, SIGNAL(cubeSelected(bool)), ui->cw_x, SLOT(setEnabled(bool)));
    connect(viewer, SIGNAL(cubeSelected(bool)), ui->ccw_y, SLOT(setEnabled(bool)));
    connect(viewer, SIGNAL(cubeSelected(bool)), ui->cw_y, SLOT(setEnabled(bool)));
    connect(viewer, SIGNAL(cubeSelected(bool)), ui->ccw_z, SLOT(setEnabled(bool)));
    connect(viewer, SIGNAL(cubeSelected(bool)), ui->cw_z, SLOT(setEnabled(bool)));

    connect(ui->ccw_x, SIGNAL(clicked(bool)), viewer, SLOT(rotateSelectedObjXCCW()));
    connect(ui->cw_x, SIGNAL(clicked(bool)), viewer, SLOT(rotateSelectedObjXCW()));
    connect(ui->ccw_y, SIGNAL(clicked(bool)), viewer, SLOT(rotateSelectedObjYCCW()));
    connect(ui->cw_y, SIGNAL(clicked(bool)), viewer, SLOT(rotateSelectedObjYCW()));
    connect(ui->ccw_z, SIGNAL(clicked(bool)), viewer, SLOT(rotateSelectedObjZCCW()));
    connect(ui->cw_z, SIGNAL(clicked(bool)), viewer, SLOT(rotateSelectedObjZCW()));

    connect(ui->speedSlider, SIGNAL(valueChanged(int)), viewer, SLOT(animationSpeedChange(int)));
    connect(ui->play_button, SIGNAL(clicked(bool)), viewer, SLOT(toggleAnimation()));

}

void MainWindow::onColorPickerActivate(){
	colordialog->open();
}
