/*
 * Stutter Juice Plugin
 * Copyright (C) 2014 Andre Sklenar <andre.sklenar@gmail.com>, www.juicelab.cz
 *
 * 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 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.
 *
 * For a full copy of the GNU General Public License see the doc/GPL.txt file.
 */

#include "StutterJuiceUI.hpp"

#include "StutterJuiceArtwork.hpp"

using DGL::Point;

START_NAMESPACE_DISTRHO

// -----------------------------------------------------------------------

StutterJuiceUI::StutterJuiceUI()
    : UI(StutterJuiceArtwork::backgroundWidth, StutterJuiceArtwork::backgroundHeight),
      fAboutWindow(this)
{
	// background
	fImgBackground = Image(StutterJuiceArtwork::backgroundData, StutterJuiceArtwork::backgroundWidth, StutterJuiceArtwork::backgroundHeight, GL_BGR);

	// overlay
	fImgOverlay = Image(StutterJuiceArtwork::overlayData, StutterJuiceArtwork::overlayWidth, StutterJuiceArtwork::overlayHeight, GL_BGRA);

	// about
	Image imageAbout(StutterJuiceArtwork::aboutData, StutterJuiceArtwork::aboutWidth, StutterJuiceArtwork::aboutHeight, GL_BGRA);
	fAboutWindow.setImage(imageAbout);

	// about button
	Image aboutImageNormal(StutterJuiceArtwork::aboutButtonNormalData, StutterJuiceArtwork::aboutButtonNormalWidth, StutterJuiceArtwork::aboutButtonNormalHeight);
	Image aboutImageHover(StutterJuiceArtwork::aboutButtonHoverData, StutterJuiceArtwork::aboutButtonHoverWidth, StutterJuiceArtwork::aboutButtonHoverHeight);
	fButtonAbout = new ImageButton(this, aboutImageNormal, aboutImageHover, aboutImageHover);
	fButtonAbout->setAbsolutePos(358, 17);
	fButtonAbout->setCallback(this);

	// sliders
	Image sliderImage(StutterJuiceArtwork::sliderData, StutterJuiceArtwork::sliderWidth, StutterJuiceArtwork::sliderHeight);
	Point<int> sliderPosStart(293, 74);
	Point<int> sliderPosEnd(293+11, 74);

	int oX = 130;
	int oY = 93;
	int mX = 357 - oX;
	int mY = 120-oY;
	int mMY = 208 - oY;
	int w = 235 - oX - StutterJuiceArtwork::sliderWidth;
	
	for (int module=0; module<9; module++) {
		for (int param=0; param<3; param++) {
			sliderPosStart.setX(oX+(module%3)*mX);
			sliderPosStart.setY(oY+(param*mY)  +  (int) (floor(module/3)*mMY));
			sliderPosEnd.setX(sliderPosStart.getX() + w);
			sliderPosEnd.setY(sliderPosStart.getY());
			
			fSliders[module][param]= new ImageSlider(this, sliderImage);
			fSliders[module][param]->setStartPos(sliderPosStart);
			fSliders[module][param]->setEndPos(sliderPosEnd);
			fSliders[module][param]->setRange(0.0f, 1.0f);
			fSliders[module][param]->setValue(0.0f);
			fSliders[module][param]->setStep(0.125f);
			fSliders[module][param]->setId(module*3+param);
			fSliders[module][param]->setCallback(this);
			
		}
		
		outputParams[module] = 0;
	}

    // set default values
    programLoaded(0);
}

// -----------------------------------------------------------------------
// DSP Callbacks

void StutterJuiceUI::parameterChanged(uint32_t index, float value)
{
	if (index<26) {
		int module = index/3;
		int param = index%3;
		fSliders[module][param]->setValue(value);
	} else {
		outputParams[index-26] = value;
		repaint();
	}
}

void StutterJuiceUI::programLoaded(uint32_t index)
{
	if (index != 0)
		return;
}

// -----------------------------------------------------------------------
// Widget Callbacks

void StutterJuiceUI::imageButtonClicked(ImageButton* button, int)
{
    if (button != fButtonAbout)
        return;

    fAboutWindow.exec();
}

void StutterJuiceUI::imageSliderDragStarted(ImageSlider* slider)
{
	editParameter(slider->getId(), true);
}

void StutterJuiceUI::imageSliderDragFinished(ImageSlider* slider)
{
	editParameter(slider->getId(), false);
}

void StutterJuiceUI::imageSliderValueChanged(ImageSlider* slider, float value)
{
	setParameterValue(slider->getId(), value);
}

void StutterJuiceUI::onDisplay()
{
    fImgBackground.draw();

    drawLFOs();
    fImgOverlay.draw();
}

// -----------------------------------------------------------------------

UI* createUI()
{
    return new StutterJuiceUI();
}

// -----------------------------------------------------------------------

END_NAMESPACE_DISTRHO
