import { AfterViewInit, AfterContentInit, Component, ElementRef, Input, OnInit, OnDestroy, NgZone, Renderer2, ViewChild, ViewContainerRef, TemplateRef, EmbeddedViewRef, ViewRef, ChangeDetectorRef, DoCheck, OnChanges, SimpleChange, ViewEncapsulation } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { FormGroup, FormControl, FormArray, FormBuilder, Validators } from '@angular/forms';
import { Subscription } from 'rxjs/Subscription';
import { Subscriber } from 'rxjs/Subscriber';
import { Observable } from 'rxjs/Observable';
import { of } from 'rxjs/observable/of';
import { delay, share } from 'rxjs/operators';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/toPromise';
import { CurrencyService, ExchangeService, FilterService, UserService, LoaderService, TokenService, IKnokOngoingArgs } from '../../../core/shared';
import { ILog, Log, applyMixins, ViewRefWatcherService } from '../../shared/helpers';
import * as MDC from 'material-components-web';
import { head as _head, findIndex as _findIndex, fill as _fill } from 'lodash/array';
import { has as _has } from 'lodash/object';
import { SimpleChanges } from '@angular/core/src/metadata/lifecycle_hooks';

@Component({
  selector: 'knk-search-stats',
  templateUrl: './knk-search-stats.component.pug',
  styleUrls: ['./knk-search-stats.component.scss']
})
export class KnkSearchStatsComponent implements AfterViewInit, OnChanges, OnInit, OnDestroy, ILog {
  filterType: string;
  // knoksersIFollow: boolean = false;
  // containsTA: boolean = false;
  // containsFA: boolean = false;
  // price: number[] = [1, 40];

  knoks: number = 0;
  currentProfit: number = 0;
  totalPotentialProfit: number = 0;

  // userIsKnokser: boolean = false;

  /* Mixins */
  public log: (...args: any[]) => void;

  protected applyTimeout: any = null;
  protected lang: string;

  private loadUserSubscription: Subscription;
  private totalSubscription: Subscription;
  private filterTypeSubscription: Subscription;

  constructor(private element: ElementRef,
              private route: ActivatedRoute,
              private userService: UserService,
              private filterService: FilterService) {
  }

  ngOnInit(): void {
    const langParamName: string = 'lang';
    this.lang = this.route.snapshot.data[langParamName];

    if (this.userService) {
      const isTestPropertyName: string = 'test';
      this.userService.testParam = this.route.snapshot.data[isTestPropertyName];
    }

    if (this.filterService) {
      
      this.filterTypeSubscription = this.filterService.filterTypeObservable.subscribe((filterType: string) => {
        this.filterType = filterType;
      });

      this.totalSubscription = this.filterService.ongoingTotalObservable.subscribe((total: {currentProfit: number, potentialProfit: number, knoksCount: number}) => {
        this.currentProfit = total.currentProfit;
        this.totalPotentialProfit = total.potentialProfit;
        this.knoks = total.knoksCount;
      });
    }
  }

  ngOnChanges(changes: SimpleChanges): void {
    // if (_has(changes, 'filterType')) {
    //   let filterType: SimpleChange = changes.filterType;
    //   if (!filterType.isFirstChange()) {
    //     this.viewWatcher.unset('stats');
    //   }

    //   let viewRef: EmbeddedViewRef<any>;
    //   this.changeDetector.detectChanges();

    //   viewRef = this.statsTplRef.createEmbeddedView(null);
    //   this.viewWatcher.set('stats', viewRef);
    // }
  }

  ngAfterViewInit(): void {
    // todo: create auto init directive and clean it
    MDC.autoInit(this.element.nativeElement);
  }

  ngOnDestroy(): void {
    if (this.loadUserSubscription) {
      this.loadUserSubscription.unsubscribe();
    }

    if (this.filterTypeSubscription) {
      this.filterTypeSubscription.unsubscribe();
    }

    if (this.totalSubscription) {
      this.totalSubscription.unsubscribe();
    }
  }

  trackByFn(index: any, item: any): any {
    return index;
  }

  booleanConverter(some: any): boolean {
    try {
      return !!some;
    } catch (e) {
      return false;
    }
  }
}

applyMixins(KnkSearchStatsComponent, [Log]);
