import { Component, Injectable } from '@angular/core';
import { Location } from '@angular/common';
import { Store } from '@ngrx/store';
import * as mapReducers from '../../reducers/map.reducer';
import { commonReducers, ItemTypeService, IItem } from '@farmmaps/common';
import { Router } from '@angular/router';
import { ForItemType } from '../for-item/for-itemtype.decorator';
import { AbstractSelectedItemComponent } from '../selected-item/selected-item.component';
import { ITemporalItemLayer} from '../../models/item.layer';
import * as mapActions from '../../actions/map.actions';
import { IItemLayer } from '../../models/item.layer';
import {TemporalService} from '../../services/temporal.service';


@ForItemType("vnd.farmmaps.itemtype.temporal")
@Injectable()
@Component({
  selector: 'fm-map-selected-item-temporal',
  templateUrl: './selected-item-temporal.component.html',
  styleUrls: ['./selected-item-temporal.component.scss']
})
export class SelectedItemTemporalComponent extends AbstractSelectedItemComponent {

  constructor(store: Store<mapReducers.State | commonReducers.State>, itemTypeService: ItemTypeService, location: Location, router: Router,public temporalService:TemporalService) {
    super(store, itemTypeService,location,router);
  }

  onLayerChanged(layerIndex: number) {
    this.store.dispatch(new mapActions.SetLayerIndex(layerIndex));
  }

  currentItemLayer():IItemLayer {
    return  (this.itemLayer as ITemporalItemLayer)?.selectedItemLayer
  }

  

  handleNextTemporal(event:MouseEvent) {
    this.store.dispatch(new mapActions.NextTemporal());
    event.preventDefault();    
  }

  handlePreviousTemporal(event:MouseEvent) {
    this.store.dispatch(new mapActions.PreviousTemporal());
    event.preventDefault();
  }

  selectedItem():IItem {
    const temporalItemLayer = this.itemLayer as ITemporalItemLayer;
    if(temporalItemLayer && temporalItemLayer.selectedItemLayer) {
       return temporalItemLayer.selectedItemLayer.item;
    }
    return null;
  }  

  layer(layers:any,layerIndex:number) {
    return layers.find(l => l.index == layerIndex);
  }

  handleGoToChart(item: IItem) {  
    this.router.navigate(['/viewer', 'temporal', 'item', item.parentCode, new Date(Date.parse(item.dataDate)).getUTCFullYear()]);
    return false;
  }
}
