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

export interface MediaQueryBuilderLogic {
  query: string;
  and: MediaQueryBuilderLiterals;
  or: MediaQueryBuilderLiterals;
}

export interface MediaQueryBuilderLiterals {
  landscape: MediaQueryBuilderLogic;
  portrait: MediaQueryBuilderLogic;

  gt(pixels: number): MediaQueryBuilderLogic;

  ge(pixels: number): MediaQueryBuilderLogic;

  lt(pixels: number): MediaQueryBuilderLogic;

  le(pixels: number): MediaQueryBuilderLogic;

  expression(expression: string): MediaQueryBuilderLogic;
}

export class MediaQueryBuilder implements MediaQueryBuilderLogic, MediaQueryBuilderLiterals {
  static readonly MARGIN = 0.1;

  private _query = '';

  static is(): MediaQueryBuilderLiterals {
    return new MediaQueryBuilder();
  }

  // region Operators & Logic
  get query(): string {
    return this._query;
  }

  get and(): MediaQueryBuilderLiterals {
    this._query += ' and ';
    return this;
  }

  get or(): MediaQueryBuilderLiterals {
    this._query += ' or ';
    return this;
  }

  // endregion

  // region Literals
  get landscape(): MediaQueryBuilderLogic {
    return this.expression('orientation: landscape');
  }

  get portrait(): MediaQueryBuilderLogic {
    return this.expression('orientation: portrait');
  }

  gt(pixels: number): MediaQueryBuilderLogic {
    return this.expression('min-width: ' + (pixels + MediaQueryBuilder.MARGIN) + 'px');
  }

  ge(pixels: number): MediaQueryBuilderLogic {
    return this.expression('min-width: ' + pixels + 'px');
  }

  lt(pixels: number): MediaQueryBuilderLogic {
    return this.expression('max-width: ' + (pixels - MediaQueryBuilder.MARGIN) + 'px');
  }

  le(pixels: number): MediaQueryBuilderLogic {
    return this.expression('max-width: ' + pixels + 'px');
  }

  expression(expression: string): MediaQueryBuilderLogic {
    this._query += '(' + expression + ')';
    return this;
  }

  // endregion
}
