#include "pch.h"
#include "GraphViewSettingItem.h"
#include <QColorDialog>

GraphViewSettingItem::GraphViewSettingItem(GraphView* view, GraphSeries* series, QWidget *parent) : QWidget(parent), series(series), view(view)
{
	ui.setupUi(this);
	// Init
	switch (series->type) {
	case GraphSeries::SeriesType::Dot:
		ui.SytleComboBox->setCurrentIndex(1);
		break;
	case GraphSeries::SeriesType::LineDot:
		ui.SytleComboBox->setCurrentIndex(2);
		break;
	case GraphSeries::SeriesType::Line:
	default:
		ui.SytleComboBox->setCurrentIndex(0);
		break;
	}
	ui.LineSlider->setValue(series->lineWidth);
	ui.CircleSlider->setValue(series->circleRadius);
	dissableSlidersOnType();
	updateGroupBoxTitle();


	
	//Connect
	connect(ui.LineSlider, &QSlider::valueChanged, [=](const int& newValue) { series->lineWidth = newValue; view->update(); });
	connect(ui.CircleSlider, &QSlider::valueChanged, [=](const int& newValue) { series->circleRadius = newValue; view->update(); });
	connect(ui.SytleComboBox, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), [=](const int& newValue) { setType(newValue);dissableSlidersOnType();view->update(); });
	connect(ui.HideCheckbox, &QCheckBox::stateChanged, [=](const int& newValue) { series->hide = (newValue==2); view->update(); });
	//connect(ui.SetColorButton, &QPushButton::pressed, this, &GraphViewSettingItem::setColor);
	connect(ui.NameEdit, &QLineEdit::textChanged, [=](const QString& text) { series->name = text; updateGroupBoxTitle(); });
}

GraphViewSettingItem::~GraphViewSettingItem()
{
}

void GraphViewSettingItem::dissableSlidersOnType()
{
	switch (series->type) {
	case GraphSeries::SeriesType::Line:
		ui.LineSlider->setEnabled(true);
		ui.CircleSlider->setEnabled(false);
		break;
	case GraphSeries::SeriesType::Dot:
		ui.LineSlider->setEnabled(false);
		ui.CircleSlider->setEnabled(true);
		break;
	case GraphSeries::SeriesType::LineDot:
		ui.LineSlider->setEnabled(true);
		ui.CircleSlider->setEnabled(true);
		break;
	default:
		break;
	}
}

void GraphViewSettingItem::setType(int type)
{
	switch (type) {
	case 0:
		series->type = GraphSeries::SeriesType::Line;
		break;
	case 1:
		series->type = GraphSeries::SeriesType::Dot;
		break;
	case 2:
		series->type = GraphSeries::SeriesType::LineDot;
		break;
	default:
		break;
	}
}

void GraphViewSettingItem::setColor()
{
	QColor color = QColorDialog::getColor(series->color, this, "Set Series Color", QColorDialog::ShowAlphaChannel);
	if (!color.isValid()) return;
	series->color = color;
	updateGroupBoxTitle();
	view->update();
}

void GraphViewSettingItem::updateGroupBoxTitle()
{
	ui.GroupBox->setTitle("Series: " + series->name);
	QString style = "QGroupBox { color: rgb(%1, %2, %3); };";
	ui.GroupBox->setStyleSheet(style.arg(series->color.red()).arg(series->color.green()).arg(series->color.blue()));
}
