import { MovingHeadState } from "rust_native_module";
import { Pattern, PatternOutput, RenderUpdate } from "./proto";
import { down, panLeft, panRight, startAddresses, tiltUp } from "./stage";
import { Tuple4 } from "./types";

const SNOOPY_COLOR: Tuple4<number> = [
  0, 255, 0, 0
]

const LEFT_RIGHT_TIME: number = 5 // seconds;

export class SnoopDoggPattern implements Pattern {
  lastUpdate: number;
  left: boolean;


  constructor() {
    this.lastUpdate = 0;
    this.left = true;
  }

  render(update: RenderUpdate): PatternOutput {
    const t = update.absolute;
    if (t - this.lastUpdate > LEFT_RIGHT_TIME) {
      this.left = !this.left;
      this.lastUpdate = t;
    }

    const tiltTarget = tiltUp + 0.25 * Math.PI * down;

    const target = this.left
      ? [panLeft, tiltTarget]
      : [panRight, tiltTarget];

    const [pan, tilt] = target;

    return (startAddresses.map((startAddress) => {
      const state: MovingHeadState = {
        startAddress: startAddress,
        pan: pan,
        tilt: tilt,
        brightness: {
          type: "dimmer",
          value: 1,
        },
        rgbw: SNOOPY_COLOR,
        speed: 0.1,
        reset: false
      }

      return state;
    })) as PatternOutput
  }

}
