import { ITimeoutService } from 'angular';

export class ProductController {

  public product: any;
  public prices: boolean;

  constructor(
    public API_URL: string,
    private $element: JQLite,
    private $timeout: ITimeoutService
  ) {}

  private objectFitCoverSupported(): boolean {
    if (!('CSS' in window && 'supports' in CSS)) {
      // If the browser doesn't support CSS.supports,
      // then it conveniently also won't support object-fit!
      return false;
    }

    return CSS.supports('object-fit', 'cover');
  }

  public $postLink(): void {
    if (!this.objectFitCoverSupported()) {
      this.$timeout(() => {
        let imgs = this.$element[0].querySelectorAll('img.product-image-photo');
        for (let i = 0, img; (img = imgs[i]); i++) {
          let src = img.getAttribute('data-src');
          // Set the src to a transparent 1x1 gif image
          img.setAttribute('data-src', 'data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7');
          img.setAttribute('src', 'data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7');
          img.style.backgroundSize = 'cover';
          img.style.backgroundPosition = 'center center';
          img.style.backgroundImage = `url('${src}')`;
        }
      });
    }
  }

  public gotoProduct(): void {
    location.pathname = `/catalog/product/view/id/${this.product.id}/`;
  }

  public get hasBadgeText(): boolean {
    return this.hasCustomAttribute('badge_text');
  }

  public get hasSpecialPrice(): boolean {
    return this.hasCustomAttribute('special_price') && this.specialIsPriceActive;
  }

  public getBgColorClass(): string {
    const prefixedColors = ['red', 'green', 'yellow'];
    const badgeColor = this.getAttribute('badge_color');
    let prefix = '';
    if (prefixedColors.indexOf(badgeColor) !== -1) {
      prefix = '-secondary';
    }
    return `bg${prefix}-${badgeColor}`;
  }

  public get specialIsPriceActive(): boolean {
    const fromDate = this.getAttribute('special_from_date');
    const from = new Date(fromDate);
    const toDate = this.getAttribute('special_to_date');
    const to = new Date(toDate);
    const now = new Date();
    const checks = [from.getDate() <= now.getDate()];
    if (toDate) {
      checks.push(to.getDate() >= now.getDate());
    }
    return checks.every(result => result);
  }

  public getCurrentPrice(): number {
    let currentPrice;
    if (this.hasSpecialPrice) {
      currentPrice = this.getAttribute('special_price') * 1.19;
    } else {
      currentPrice = this.product.price * 1.19;
    }
    return parseFloat(currentPrice.toFixed(2));
  }

  public getAttribute(attribute_code: string): any {
    let result = this.product.custom_attributes.find((item) => {
      return item.attribute_code === attribute_code;
    });
    if (result) {
      return result.value;
    } else {
      return '';
    }
  }

  public hasCustomAttribute(attributeName): boolean {
    return this.getAttribute(attributeName)
      && (this.getAttribute(attributeName) !== '');
  }

}
