/*
  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
*/

#include "audiosynth/WaveModifier.h"

#define stemobject_implementation WaveModifier

v_begin();
v_func(dispose);
v_func(isEqual);
v_func(initState);
v_func(disposeState);
v_func(getPropertyCount);
v_func(getPropertyAtIndex);
v_func(getPropertyValueStateless);
v_func(setPropertyValue);
v_func(setUpstream);
v_func(getOwnPropertyCount);
v_func(getOwnProperties);
v_end();

bool WaveModifier_init(WaveModifier * self) {
	call_super(init, self);
	self->upstream = NULL;
	self->ownProperties = call_virtual(getOwnProperties, self);
	self->blend = 1.0f;
	self->mute = false;
	self->solo = false;
	return true;
}

void WaveModifier_dispose(WaveModifier * self) {
	call_super_virtual(dispose, self);
}

bool WaveModifier_isEqual(WaveModifier * self, compat_type(WaveModifier *) compareUntyped) {
	WaveModifier * compare = compareUntyped;
	return StemObject_isExactClass(compare, self->vtable) &&
	       compare->blend == self->blend &&
	       compare->mute == self->mute &&
	       compare->solo == self->solo;
}

void WaveModifier_initCopy(WaveModifier * self, WaveModifier * original) {
	call_super(initCopy, self, (WaveSampler *) original);
	self->ownProperties = call_virtual(getOwnProperties, self);
	self->blend = original->blend;
	self->mute = original->mute;
	self->solo = original->solo;
}

SamplerObject_state * WaveModifier_initState(WaveModifier * self) {
	unsigned int ownPropertyCount = call_virtual(getOwnPropertyCount, self);
	struct WaveModifier_state * stateStruct = malloc(sizeof(*stateStruct) - sizeof(float) + sizeof(float) * ownPropertyCount);
	stateStruct->upstreamState = call_virtual(initState, self->upstream);
	for (unsigned int propertyIndex = 0; propertyIndex < ownPropertyCount; propertyIndex++) {
		stateStruct->properties[propertyIndex] = *(float *) ((void *) self + self->ownProperties[propertyIndex].offset);
	}
	return stateStruct;
}

void WaveModifier_disposeState(WaveModifier * self, SamplerObject_state * state) {
	struct WaveModifier_state * stateStruct = state;
	call_virtual(disposeState, self->upstream, stateStruct->upstreamState);
	free(stateStruct);
}

unsigned int WaveModifier_getPropertyCount(WaveModifier * self) {
	return call_virtual(getOwnPropertyCount, self) + call_virtual(getPropertyCount, self->upstream);
}

SynthProperty WaveModifier_getPropertyAtIndex(WaveModifier * self, unsigned int index) {
	unsigned int ownPropertyCount = call_virtual(getOwnPropertyCount, self);
	if (index < ownPropertyCount) {
		return self->ownProperties[index];
	}
	SynthProperty upstreamProperty = call_virtual(getPropertyAtIndex, self->upstream, index - ownPropertyCount);
	upstreamProperty.propertyIndex = index;
	return upstreamProperty;
}

float WaveModifier_getPropertyValueStateless(WaveModifier * self, SynthPropertyIdentifier propertyIdentifier) {
	unsigned int ownPropertyCount = call_virtual(getOwnPropertyCount, self);
	if (propertyIdentifier.propertyIndex < ownPropertyCount) {
		return *(float *) ((void *) self + self->ownProperties[propertyIdentifier.propertyIndex].offset);
	}
	propertyIdentifier.propertyIndex -= ownPropertyCount;
	return call_virtual(getPropertyValueStateless, self->upstream, propertyIdentifier);
}

void WaveModifier_setPropertyValue(WaveModifier * self, SamplerObject_state * state, SynthPropertyIdentifier propertyIdentifier, float value) {
	struct WaveModifier_state * stateStruct = state;
	unsigned int ownPropertyCount = call_virtual(getOwnPropertyCount, self);
	if (propertyIdentifier.propertyIndex < ownPropertyCount) {
		stateStruct->properties[propertyIdentifier.propertyIndex] = value;
	} else {
		propertyIdentifier.propertyIndex -= ownPropertyCount;
		call_virtual(setPropertyValue, self->upstream, stateStruct->upstreamState, propertyIdentifier, value);
	}
}

void WaveModifier_setUpstream(WaveModifier * self, compat_type(WaveSampler *) upstream) {
	self->upstream = upstream;
}

unsigned int WaveModifier_getOwnPropertyCount(WaveModifier * self) {
	return 1;
}

SynthProperty * WaveModifier_getOwnProperties(WaveModifier * self) {
	static SynthProperty properties[] = {
		{.propertyIndex = 0, .description = "Blend", .offset = offsetof(WaveModifier, blend), .minValue = 0.0f, .maxValue = 1.0f, .defaultValue = 1.0f, .stepIncrement = 0.05f}
	};
	return properties;
}
