import {Component, ViewRef} from '@angular/core';

import {HsDialogContainerService} from 'hslayers-ng/components/layout/dialogs/dialog-container.service';
import {HsLayoutService} from 'hslayers-ng/components/layout/layout.service';
import {HsPanelComponent} from 'hslayers-ng/components/layout/panels/panel-component.interface';

//import {AdjusterEventService} from './adjuster-event.service';
import {AdjusterLoaderComponent} from './adjuster-loader.component';
import {AdjusterService} from './adjuster.service';

@Component({
  selector: 'pra-adjuster',
  template: require('./adjuster.directive.html'),
})
export class AdjusterComponent implements HsPanelComponent {
  data: any;
  //descriptionVisible: boolean;
  errorMsg: string;
  //method: string;
  viewRef: ViewRef;

  constructor(
    public adjusterService: AdjusterService,
    public hsDialogContainerService: HsDialogContainerService,
    public hsLayoutService: HsLayoutService
  ) {
    //this.descriptionVisible = false;
    //this.method = this.adjusterService.method;
    /*this.adjusterEventService.clustersLoaded.subscribe(({success, err}) => {
      if (!success) {
        this.errorMsg = err.message;
      }
    });*/
  }

  ngOnInit(): void {
    this.hsDialogContainerService.create(AdjusterLoaderComponent, {});
  }

  isVisible(): boolean {
    return this.hsLayoutService.panelVisible('adjuster');
  }

  noDataSelected(): boolean {
    if (this.adjusterService.factors.length === 0) {
      return true;
    }
    let datasetsEffectivelyTurnedOn = [];
    for (const factor of this.adjusterService.factors) {
      if (factor.weight === 0) {
        continue;
      }
      datasetsEffectivelyTurnedOn = [
        ...datasetsEffectivelyTurnedOn,
        ...factor.datasets.filter((ds) => ds.included),
      ];
    }
    return datasetsEffectivelyTurnedOn.length === 0;
  }

  noOperationSelected(): boolean {
    return (
      !this.adjusterService.allowIndex && !this.adjusterService.allowClusters
    );
  }
  /*selectMethod(): void {
    this.adjusterService.method = this.method;
    this.adjusterEventService.methodChanged.next(this.method);
  }*/
}
