import { MovingHeadState } from 'rust_native_module';
import { Pattern, PatternOutput, RenderUpdate } from './proto';
import { Tuple4 } from './types';

const template: MovingHeadState = {
    startAddress: 0,
    pan: 0,
    tilt: 0,
    brightness: {
        type: 'dimmer',
        value: 0.2,
    },
    rgbw: [255, 0, 0, 0],
    speed: 1,
    reset: false,
}

export class ChaserPattern implements Pattern {

    render(update: RenderUpdate): PatternOutput {

        if (update.beatRelative === null) {
            return null;
        }

        let t = update.beatRelative;

        let head_number = Math.floor(t % 4);

        let result: Tuple4<MovingHeadState> = [{ ...template }, { ...template }, { ...template }, { ...template }];

        [1, 15, 29, 43].forEach((startAddress, i) => {
            result[i].startAddress = startAddress;

            if (i === head_number) {
                result[i].brightness = { type: 'dimmer', value: 1 };
            }
        });

        return result;
    }
}
