import { Component } from '@angular/core';
import { Router, ActivatedRoute, NavigationEnd } from '@angular/router';
import { Sensor } from '@overflow/commons-typescript/model/sensor';
import { BreadcrumbService } from '@app/commons/service/breadcrumb.service';
import { Target } from '@overflow/commons-typescript/model/target';

enum CONTAINER_TYPES {
  List = 1,
  Detail,
  Setting,
}

@Component({
  selector: 'of-pages-sensor',
  templateUrl: './sensor-page.component.html',
})
export class SensorPageComponent {

  containerType: CONTAINER_TYPES;
  sensorID: string;

  constructor(
    private router: Router,
    private route: ActivatedRoute,
    private breadcrumbService: BreadcrumbService
  ) {
    this.route.params.subscribe(params => {
      if (params['id']) {
        this.onDetailContainer(params['id']);
      } else {
        this.onListSettingContainer();
      }
    });
  }

  onListSettingContainer() {
    this.breadcrumbService.setItems([
      { label: 'Sensor', routerLink: ['/sensor/list'], }
    ]);
    if (this.router.url === '/sensor/list') {
      this.containerType = CONTAINER_TYPES.List;
    } else {
      this.containerType = CONTAINER_TYPES.Setting;
    }
  }

  onDetailContainer(sensorID: string) {
    this.sensorID = sensorID;
    this.breadcrumbService.setItems([
      { label: 'Sensor', routerLink: ['/sensor/list'] },
      { label: this.sensorID }
    ]);
    this.containerType = CONTAINER_TYPES.Detail;
  }

  onSensorSelect(sensor: Sensor) {
    this.router.navigate(['sensor', sensor.id, 'info']);
  }

  onAddSensor(target?: Target) {
    const queryParams = target ? {
      target : target.id,
      mode : 'NEW',
    } : {
      mode : 'NEW',
    };
    this.router.navigate(['sensor/setting'], { queryParams: queryParams });
  }

}
