/*
  Copyright (c) 2021 Alex Diener
  
  This software is provided 'as-is', without any express or implied
  warranty. In no event will the authors be held liable for any damages
  arising from the use of this software.
  
  Permission is granted to anyone to use this software for any purpose,
  including commercial applications, and to alter it and redistribute it
  freely, subject to the following restrictions:
  
  1. The origin of this software must not be misrepresented; you must not
     claim that you wrote the original software. If you use this software
     in a product, an acknowledgment in the product documentation would be
     appreciated but is not required.
  2. Altered source versions must be plainly marked as such, and must not be
     misrepresented as being the original software.
  3. This notice may not be removed or altered from any source distribution.
  
  Alex Diener alex@ludobloom.com
*/

#ifndef __SynthControls_H__
#define __SynthControls_H__

typedef struct SynthControls SynthControls;
#define SynthControls_superclass UIContainer

typedef void (* SynthControls_liveUpdateCallback)(void * context);
typedef void (* SynthControls_updateCompleteCallback)(void * context, const char * description);

#include "audiolab/DrawingMacros.h"
#include "audiolab/Utilities.h"
#include "shell/ShellKeyCodes.h"
#include "uielementcollection/UIFloatEditText.h"
#include "uitoolkit/UICheckbox.h"
#include "uitoolkit/UIContainer.h"
#include "uitoolkit/UILabel.h"
#include "uitoolkit/UISlider.h"
#include "uitoolkit/UIStepper.h"
#include "uitoolkit/UIWindowRoot.h"

#define parameterControlIVars(struct_field) \
	UILabel * label_##struct_field; \
	UISlider * slider_##struct_field; \
	UIStepper * stepper_##struct_field; \
	UIFloatEditText * editText_##struct_field;

#define updateSynthControlSlider(function_typecast, function_field, struct_field, callback_prefix) { \
	UIFloatEditText_setValue(self->editText_##callback_prefix, ((function_typecast *) function_field)->struct_field); \
	UISlider_setValue(self->slider_##callback_prefix, ((function_typecast *) function_field)->struct_field); \
}

#define defineSynthControlSliderCallback_extended(function_typecast, function_field, struct_field, callback_prefix, undo_description, step_size, live_update_function, update_complete_function, callback_context) \
static void callback_prefix##_sliderChange(UISlider * slider, float value, unsigned int modifiers, double referenceTime, void * context) { \
	stemobject_implementation * self = context; \
	((function_typecast *) function_field)->struct_field = value; \
	UIFloatEditText_setValue(self->editText_##callback_prefix, ((function_typecast *) function_field)->struct_field); \
	live_update_function(callback_context); \
} \
\
static void callback_prefix##_sliderComplete(UISlider * slider, float value, unsigned int modifiers, double referenceTime, void * context) { \
	stemobject_implementation * self = context; \
	update_complete_function(callback_context, "change " undo_description); \
} \
\
static void callback_prefix##_stepperAction(UIStepper * stepper, int change, unsigned int modifiers, double referenceTime, void * context) { \
	stemobject_implementation * self = context; \
	float stepSize = step_size; \
	if (modifiers & MODIFIER_SHIFT_BIT) { \
		if (modifiers & MODIFIER_CONTROL_BIT) { \
			stepSize *= 12; \
		} else { \
			stepSize *= 4; \
		} \
	} else if (modifiers & MODIFIER_CONTROL_BIT) { \
		stepSize *= 0.25f; \
	} \
	float newValue = ((function_typecast *) function_field)->struct_field; \
	if (change > 0) { \
		newValue = (floorf(newValue / stepSize + 0.0001f) + 1) * stepSize; \
	} else { \
		newValue = (ceilf(newValue / stepSize - 0.0001f) - 1) * stepSize; \
	} \
	if (!(modifiers & MODIFIER_ANY_ALT_BIT)) { \
		newValue = fmaxf(fminf(newValue, self->slider_##callback_prefix->maxValue), self->slider_##callback_prefix->minValue); \
	} \
	((function_typecast *) function_field)->struct_field = newValue; \
	updateSynthControlSlider(function_typecast, function_field, struct_field, callback_prefix); \
	live_update_function(callback_context); \
} \
\
static void callback_prefix##_stepperUpdateComplete(UIStepper * stepper, double referenceTime, void * context) { \
	stemobject_implementation * self = context; \
	update_complete_function(callback_context, "change " undo_description); \
} \
\
static bool callback_prefix##_valueChanged(UIFloatEditText * editText, float value, unsigned int modifiers, double referenceTime, void * context) { \
	stemobject_implementation * self = context; \
	if (((function_typecast *) function_field)->struct_field != value) { \
		((function_typecast *) function_field)->struct_field = value; \
		UISlider_setValue(self->slider_##callback_prefix, ((function_typecast *) function_field)->struct_field); \
		live_update_function(callback_context); \
		update_complete_function(callback_context, "change " undo_description); \
	} \
	return false; \
}

#define defineSynthControlSliderCallback(function_typecast, function_field, struct_field, undo_description, step_size) \
	defineSynthControlSliderCallback_extended(function_typecast, function_field, struct_field, struct_field, undo_description, step_size, self->liveUpdateCallback, self->updateCompleteCallback, self->callbackContext)

#define addSynthControlSlider_extended(container, control_x, initial_value, label_string, callback_prefix, min_value, max_value, default_value) { \
	call_virtual(addElement, container, self->label_##callback_prefix = UILabel_create(STR(label_string), VECTOR2f((control_x) - CONTROL_SLIDER_LABEL_PADDING, controlY), VECTOR2f(0.0f, 0.0f), VECTOR2f(1.0f, 0.5f), ALIGN_RIGHT, 0, OVERFLOW_RESIZE, OVERFLOW_RESIZE, g_uiAppearance), true); \
	call_virtual(addElement, container, self->slider_##callback_prefix = UISlider_create(min_value, max_value, initial_value, VECTOR2f((control_x), controlY), VECTOR2f(0.0f, 0.5f), SLIDER_OPTION_HORIZONTAL | SLIDER_OPTION_ALLOW_UNCLAMPED_DRAG | SLIDER_OPTION_ALLOW_PRECISE_DRAG | SLIDER_OPTION_RESET_ON_CONTROL_CLICK, CONTROL_SLIDER_LENGTH, callback_prefix##_sliderChange, callback_prefix##_sliderComplete, self, g_uiAppearance), true); \
	self->slider_##callback_prefix->defaultValue = default_value; \
	call_virtual(addElement, container, self->stepper_##callback_prefix = UIStepper_create(VECTOR2f((control_x) + CONTROL_SLIDER_LENGTH + CONTROL_SLIDER_LABEL_PADDING, controlY), VECTOR2f(0.0f, 0.5f), STEPPER_VERTICAL, true, false, callback_prefix##_stepperAction, callback_prefix##_stepperUpdateComplete, self, g_uiAppearance), true); \
	call_virtual(addElement, container, self->editText_##callback_prefix = UIFloatEditText_create(initial_value, 3, VECTOR2f((control_x) + CONTROL_SLIDER_LENGTH + CONTROL_SLIDER_LABEL_PADDING + CONTROL_STEPPER_WIDTH + 4, controlY), VECTOR2f(0.0f, 0.5f), CONTROL_PARAM_FIELD_WIDTH, &g_floatClipboard, floatArithmeticInterpreterCallback, callback_prefix##_valueChanged, self, g_uiAppearance), true); \
	call_virtual(setWhitelist, self->editText_##callback_prefix, STRL(FLOAT_ARTIHMETIC_CHAR_WHITELIST)); \
	controlY -= CONTROL_OFFSET_Y; \
}

#define addSynthControlSlider(container, control_x, initial_value, label_string, struct_field, min_value, max_value, default_value) \
	addSynthControlSlider_extended(container, control_x, initial_value, label_string, struct_field, min_value, max_value, default_value)

#define FREQUENCY_CONTROL_SLIDER_LENGTH 153.0f
#define FREQUENCY_STEP_SIZE (1.0f / 12.0f)

#define frequencyControlIVars(struct_field) \
	UILabel * label_##struct_field; \
	UISlider * slider_##struct_field; \
	UIStepper * stepper_##struct_field; \
	UIFloatEditText * editText_##struct_field; \
	UICheckbox * hertzCheckbox_##struct_field;

#define defineFrequencyControlSliderCallback(function_typecast, function_field, struct_field, undo_description) \
static void struct_field##_updateEditText(stemobject_implementation * self) { \
	float value = ((function_typecast *) function_field)->struct_field; \
	if (self->hertzCheckbox_##struct_field->checked) { \
		value = octaveToHertz(value); \
	} \
	UIFloatEditText_setValue(self->editText_##struct_field, value); \
} \
\
static void struct_field##_sliderChange(UISlider * slider, float value, unsigned int modifiers, double referenceTime, void * context) { \
	stemobject_implementation * self = context; \
	((function_typecast *) function_field)->struct_field = value; \
	struct_field##_updateEditText(context); \
	self->liveUpdateCallback(self->callbackContext); \
} \
\
static void struct_field##_sliderComplete(UISlider * slider, float value, unsigned int modifiers, double referenceTime, void * context) { \
	stemobject_implementation * self = context; \
	self->updateCompleteCallback(self->callbackContext, "change " undo_description); \
} \
\
static void struct_field##_stepperAction(UIStepper * stepper, int change, unsigned int modifiers, double referenceTime, void * context) { \
	stemobject_implementation * self = context; \
	float stepSize = FREQUENCY_STEP_SIZE; \
	if (modifiers & MODIFIER_SHIFT_BIT) { \
		if (modifiers & MODIFIER_CONTROL_BIT) { \
			stepSize *= 12; \
		} else { \
			stepSize *= 4; \
		} \
	} else if (modifiers & MODIFIER_CONTROL_BIT) { \
		stepSize *= 0.25f; \
	} \
	float newValue = ((function_typecast *) function_field)->struct_field; \
	if (change > 0) { \
		newValue = (floorf(newValue / stepSize + 0.0001f) + 1) * stepSize; \
	} else { \
		newValue = (ceilf(newValue / stepSize - 0.0001f) - 1) * stepSize; \
	} \
	if (!(modifiers & MODIFIER_ANY_ALT_BIT)) { \
		newValue = fmaxf(fminf(newValue, self->slider_##struct_field->maxValue), self->slider_##struct_field->minValue); \
	} \
	((function_typecast *) function_field)->struct_field = newValue; \
	struct_field##_updateEditText(self); \
	UISlider_setValue(self->slider_##struct_field, ((function_typecast *) function_field)->struct_field); \
	self->liveUpdateCallback(self->callbackContext); \
} \
\
static void struct_field##_stepperUpdateComplete(UIStepper * stepper, double referenceTime, void * context) { \
	stemobject_implementation * self = context; \
	self->updateCompleteCallback(self->callbackContext, "change " undo_description); \
} \
\
static bool struct_field##_valueChanged(UIFloatEditText * editText, float value, unsigned int modifiers, double referenceTime, void * context) { \
	stemobject_implementation * self = context; \
	if (self->hertzCheckbox_##struct_field->checked) { \
		value = hertzToOctave(value); \
	} \
	if (((function_typecast *) function_field)->struct_field != value) { \
		((function_typecast *) function_field)->struct_field = value; \
		UISlider_setValue(self->slider_##struct_field, value); \
		self->liveUpdateCallback(self->callbackContext); \
		self->updateCompleteCallback(self->callbackContext, "change " undo_description); \
	} \
	return false; \
} \
\
static void struct_field##_hertzCheckboxCallback(UICheckbox * checkbox, bool checked, unsigned int modifiers, double referenceTime, void * context) { \
	struct_field##_updateEditText(context); \
}

#define addFrequencyControlSlider(container, control_x, initial_value, label_string, struct_field, min_value, max_value, default_value) { \
	call_virtual(addElement, container, self->label_##struct_field = UILabel_create(STR(label_string), VECTOR2f((control_x) - CONTROL_SLIDER_LABEL_PADDING, controlY), VECTOR2f(0.0f, 0.0f), VECTOR2f(1.0f, 0.5f), ALIGN_RIGHT, 0, OVERFLOW_RESIZE, OVERFLOW_RESIZE, g_uiAppearance), true); \
	call_virtual(addElement, container, self->slider_##struct_field = UISlider_create(min_value, max_value, initial_value, VECTOR2f((control_x), controlY), VECTOR2f(0.0f, 0.5f), SLIDER_OPTION_HORIZONTAL | SLIDER_OPTION_ALLOW_UNCLAMPED_DRAG | SLIDER_OPTION_ALLOW_PRECISE_DRAG | SLIDER_OPTION_RESET_ON_CONTROL_CLICK, FREQUENCY_CONTROL_SLIDER_LENGTH, struct_field##_sliderChange, struct_field##_sliderComplete, self, g_uiAppearance), true); \
	self->slider_##struct_field->defaultValue = default_value; \
	call_virtual(addElement, container, self->stepper_##struct_field = UIStepper_create(VECTOR2f((control_x) + FREQUENCY_CONTROL_SLIDER_LENGTH + CONTROL_SLIDER_LABEL_PADDING, controlY), VECTOR2f(0.0f, 0.5f), STEPPER_VERTICAL, true, false, struct_field##_stepperAction, struct_field##_stepperUpdateComplete, self, g_uiAppearance), true); \
	call_virtual(addElement, container, self->editText_##struct_field = UIFloatEditText_create(initial_value, 3, VECTOR2f((control_x) + FREQUENCY_CONTROL_SLIDER_LENGTH + CONTROL_SLIDER_LABEL_PADDING + CONTROL_STEPPER_WIDTH + 4, controlY), VECTOR2f(0.0f, 0.5f), CONTROL_PARAM_FIELD_WIDTH, &g_floatClipboard, floatArithmeticInterpreterCallback, struct_field##_valueChanged, self, g_uiAppearance), true); \
	call_virtual(setWhitelist, self->editText_##struct_field, STRL(FLOAT_ARTIHMETIC_CHAR_WHITELIST)); \
	call_virtual(addElement, container, self->hertzCheckbox_##struct_field = UICheckbox_create(STRL("hz"), VECTOR2f((control_x) + FREQUENCY_CONTROL_SLIDER_LENGTH + CONTROL_SLIDER_LABEL_PADDING + CONTROL_STEPPER_WIDTH + 4 + CONTROL_PARAM_FIELD_WIDTH + 4, controlY), VECTOR2f(0.0f, 0.5f), 0, OVERFLOW_RESIZE, false, struct_field##_hertzCheckboxCallback, self, g_uiAppearance), true); \
	controlY -= CONTROL_OFFSET_Y; \
}

#define SynthControls_ivars \
	UIContainer_ivars \
	\
	SynthControls_liveUpdateCallback liveUpdateCallback; \
	SynthControls_updateCompleteCallback updateCompleteCallback; \
	void * callbackContext; \
	UIWindowRoot * windowRoot;

#define SynthControls_vtable(self_type) \
	UIContainer_vtable(self_type) \
	\
	void (* updateControls)(self_type * self); \
	void (* setDisplayedValueRange)(self_type * self, float minValue, float maxValue, float defaultValue);

stemobject_declare(SynthControls)

bool SynthControls_init(SynthControls * self, Vector2f position, SynthControls_liveUpdateCallback liveUpdateCallback, SynthControls_updateCompleteCallback updateCompleteCallback, void * callbackContext, UIWindowRoot * windowRoot, UIAppearance appearance);
void SynthControls_dispose(SynthControls * self);
void SynthControls_updateControls(SynthControls * self);
void SynthControls_setDisplayedValueRange(SynthControls * self, float minValue, float maxValue, float defaultValue);

#endif
