import { IController } from 'angular';
export const RESET_DELAY: number = 2000;
export const OPTIMISTIC_COLOR: string = 'green';
export const OPTIMISTIC_ICON: string = 'success';

/**
 * Controller holding the application-logic for <button-component>
 */
export class ButtonController implements IController {

  public optimistic: string;
  public icon: string;
  public color: string;
  public type: string;
  public disabled: boolean = false;
  public link: string;
  public resetDelay: number = RESET_DELAY;

  private iconBefore;
  private colorBefore;
  private disabledBefore;

  constructor(
    private $window,
    private $timeout
  ) {}

  private setStateSuccess(): void {
    this.iconBefore = this.icon;
    this.colorBefore = this.color;
    this.disabledBefore = this.disabled;
    this.icon = OPTIMISTIC_ICON;
    this.color = OPTIMISTIC_COLOR;
    this.disabled = true;
  }

  private resetState(): void {
    this.icon = this.iconBefore;
    this.color = this.colorBefore;
    this.disabled = this.disabledBefore;
  }

  clickHandler(): void {
    if (!this.link) {
      if (this.optimistic === 'true') {
        this.setStateSuccess();
        this.$timeout(() => {
          this.resetState();
        }, this.resetDelay);
      }
    } else {
      this.$window.location.href = this.link;
    }

  }
}
