import { Injectable} from '@angular/core';
import {DatePipe} from '@angular/common';
import {TimespanService} from '@farmmaps/common';
import { IItemLayer, ITemporalItemLayer,TemporalItemLayer} from '../models/item.layer';


@Injectable()
export class TemporalService {   
    constructor(private timespanService$:TimespanService,private datePipe$: DatePipe) {}

    hasNext(itemLayer:IItemLayer):boolean {
        const temporalItemLayer = itemLayer as  ITemporalItemLayer;
        return temporalItemLayer && temporalItemLayer.nextItemLayer != null;
      }
    
      selectedDate(itemLayer:IItemLayer):string {
        const temporalItemLayer = itemLayer as  ITemporalItemLayer;        
        if(temporalItemLayer && temporalItemLayer.selectedItemLayer) {
          if(temporalItemLayer && temporalItemLayer.selectedItemLayer.item.dataDate && temporalItemLayer.selectedItemLayer.item.dataEndDate) {
            const sd = new Date(Date.parse(temporalItemLayer.selectedItemLayer.item.dataDate));
            const ed = new Date(Date.parse(temporalItemLayer.selectedItemLayer.item.dataEndDate));
            return this.timespanService$.getCaption(sd,ed);
          } else {
            const d = new Date(Date.parse(temporalItemLayer.selectedItemLayer.item.dataDate));
            return this.datePipe$.transform(d, "shortDate");  
          }
        }
        return "";
      }
    
      nextDate(itemLayer:IItemLayer):string {
        const temporalItemLayer = itemLayer as  ITemporalItemLayer;
        if(temporalItemLayer && temporalItemLayer.nextItemLayer && temporalItemLayer.nextItemLayer.item) {
          if(temporalItemLayer.nextItemLayer.item.dataDate && temporalItemLayer.nextItemLayer.item.dataEndDate) {
            const sd = new Date(Date.parse(temporalItemLayer.nextItemLayer.item.dataDate));
            const ed = new Date(Date.parse(temporalItemLayer.nextItemLayer.item.dataEndDate));
            return this.timespanService$.getCaption(sd,ed);
          } else {
            const d = new Date(Date.parse(temporalItemLayer.nextItemLayer.item.dataDate));
            return this.datePipe$.transform(d, "shortDate");  
          }
        }
        return "";
      }
    
      hasPrevious(itemLayer:IItemLayer):boolean {
        const temporalItemLayer = itemLayer as  ITemporalItemLayer;
        return temporalItemLayer && temporalItemLayer.previousItemLayer != null;
      }
    
      previousDate(itemLayer:IItemLayer):string {
        const temporalItemLayer = itemLayer as  ITemporalItemLayer;
        if(temporalItemLayer && temporalItemLayer.previousItemLayer && temporalItemLayer.previousItemLayer.item)  {
          if(temporalItemLayer.previousItemLayer.item.dataDate && temporalItemLayer.previousItemLayer.item.dataEndDate) {
            const sd = new Date(Date.parse(temporalItemLayer.previousItemLayer.item.dataDate));
            const ed = new Date(Date.parse(temporalItemLayer.previousItemLayer.item.dataEndDate));
            return this.timespanService$.getCaption(sd,ed);
          } else {
            const d = new Date(Date.parse(temporalItemLayer.previousItemLayer.item.dataDate));
            return this.datePipe$.transform(d, "shortDate");  
          }
        }
        return "";
      }
}