/*
File: swSinusoid.js
Author: klp
Description: SWSinusoid class for modeling sinusoidal behavior
Date: 2026-01-30
Workspace: SWaveJS2026-01-30-StgC

Notes:
- Models y = A * sin(B * (x - D)) + C
- A = Amplitude
- B = Frequency
- C = Vertical shift
- D = Phase shift

Used for 'breathing' effects and other sinusoidal motions

We are using etime in SWJS to get elapsed time in seconds for 
the sinusoid calculations rather than frameCount 
which is tied to frame rate.
*/


console.log("[swSinusoid.js] SWSinusoid class loaded");

class SWSinusoid {
    constructor(A = 1, B = 1, C = 0, D = 0) {
        this.A = A; // Amplitude
        this.B = B; // Frequency
        this.C = C; // Vertical shift
        this.D = D; // Phase shift
        this.period = (2 * Math.PI) / B; // Period of the sinusoid
    }//end constructor

    setPeriod(newPeriod) {
        this.period = newPeriod;
        this.B = 2 * Math.PI / newPeriod;
    }//end setPeriod

    adjustWaveUsingExtrema(minY, maxY) {
        this.A = (maxY - minY) / 2;
        this.C = (maxY + minY) / 2;
    }//end adjustWaveUsingExtrema

    startACycleAtTime(tStart) {
        this.D = tStart;
    }//end startACycleAtTime

    // Returns y for a given x (or time)
    getValue(x) {
        return this.A * Math.sin(this.B * (x - this.D)) + this.C;
    }//end getValue

    toString() {
        return `y = ${this.A} * sin(${this.B} * (x - ${this.D})) + ${this.C} with period ${this.period} `;
    }//end toString

    static copy(sinusoid) {
        //console.log("...SWSinusoid.copy...");
        if (!(sinusoid instanceof SWSinusoid)) {
            throw new Error('Argument to SWSinusoid.copy must be an SWSinusoid instance');
        }   
        return new SWSinusoid(sinusoid.A, sinusoid.B, sinusoid.C, sinusoid.D);
    }//end copy

}//end SWSinusoid class

//DWR! generic sinusoid instances for copying; must be initialized after class definition
const UNIT_SINUSOID = new SWSinusoid(1, 1, 0, 0); // y = sin(x)
const UNIT_COSINE = new SWSinusoid(1, 1, 0, -Math.PI/2); // y = cos(x) = sin(x + π/2)
const GENERIC_SINUSOID = new SWSinusoid(1, 1, 0, 0); // y = sin(x)

// Example usage:
// let wave = new SWSinusoid(2, 0.5, 1, Math.PI/4);
// let y = wave.getValue(0);

// Export for module use (if needed)
// export default SWSinusoid;
// Note: 
