/*
 * Copyright (C) 2020 Robert Bosch GmbH Copyright (C) 2020 Robert Bosch Manufacturing Solutions GmbH, Germany. All rights reserved.
 */

export interface Point {
  x: number;
  y: number;
}

export class GaugeCircle {
  constructor(private readonly center: Point, private readonly radius: number, private arcRotationOffset: number) {}

  private polarToCartesian(angleInDegrees: number): Point {
    const angleInRadians = ((angleInDegrees - this.arcRotationOffset) * Math.PI) / 180.0;

    return {
      x: this.center.x + this.radius * Math.cos(angleInRadians),
      y: this.center.y + this.radius * Math.sin(angleInRadians),
    };
  }

  toSvgArc(startAngle: number, endAngle: number): string {
    let start = this.polarToCartesian(endAngle);
    const end = this.polarToCartesian(startAngle);

    const largeArcFlag = endAngle - startAngle <= 180 ? '0' : '1';
    let closePath = false;

    if (endAngle === 360) {
      closePath = true;
      start = this.polarToCartesian(359.9);
    }

    return [
      'M',
      start.x,
      start.y,
      'A',
      this.radius,
      this.radius,
      0,
      largeArcFlag,
      0,
      end.x,
      end.y,
      closePath ? 'Z' : '',
    ].join(' ');
  }
}
