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

import {HsDialogContainerService} from 'hslayers-ng';
import {HsLayoutService} from 'hslayers-ng';
import {HsPanelComponent} from 'hslayers-ng';

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

@Component({
  selector: 'pra-adjuster',
  templateUrl: './adjuster.component.html',
  styleUrls: ['./adjuster.component.scss'],
})
export class AdjusterComponent implements HsPanelComponent, OnInit {
  name = 'adjuster';
  data: any;
  //descriptionVisible: boolean;
  errorMsg: string;
  //method: string;
  viewRef: ViewRef;

  constructor(
    public adjusterService: AdjusterService,
    public adjusterPresetsService: AdjusterPresetsService,
    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, {});
  }

  hasDatasets(factor): boolean {
    return factor.datasets.length > 0;
  }

  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);
  }*/
}
