import { Pipe, PipeTransform } from '@angular/core';
import { DatePipe } from '@angular/common';
import * as moment from 'moment';
   
   @Pipe({
     name: 'customDate'
   })
   export class CustomDatePipe extends 
                DatePipe implements PipeTransform {

    minutes:any;
    hour:any;
    firstvalue:any;
    secondvalue:any;
    resultvalue:any;
    dayscaluclation:any;
    minscaluclation:any;
  


     transform(value: any, args?: any): any {
      //  this.today;
      let currentdatevalue = Date.now();
    
      let d1 = new Date(currentdatevalue); let d2 = new Date(value);
    
      let giventimestamp = moment(value).unix(); // 1318874398
      let currenttimestamp=moment(currentdatevalue).unix();
    //   alert(currenttimestamp);
    //  console.log("giventimestamp-->"+giventimestamp)
    //  console.log("currenttimestamp-->"+currenttimestamp)
     
    //    if(Number(giventimestamp) >= currentdatevalue){
       // console.log("d1--currenttime"+d1)
     //   console.log("d2--giventime"+d2)
        if (d2 > d1) {
            
      //  console.log(this.resultvalue)
        this.firstvalue=giventimestamp;
        this.secondvalue=currenttimestamp;
        this.resultvalue='Overdue';
      //  console.log(this.resultvalue)
      }else{
        
        this.firstvalue=currenttimestamp;
        this.secondvalue=giventimestamp;
        this.resultvalue='to go';
     //   console.log(this.resultvalue)
      }
    
      
      let differencetime=Number(this.firstvalue) - Number(this.secondvalue);

    //   console.log("giventimestamp_timestamp-->"+giventimestamp_timestamp);
    //   console.log("currenttimestamp-->"+currenttimestamp);
    //   console.log("differencetime-->"+differencetime);
      
         this.minutes = "0" + Math.floor(differencetime / 60);
        //  get minutes
        let seconds = "0" + Math.floor(differencetime - this.minutes * 60);
        // get seconds
         this.hour = "0" + Math.floor(differencetime / 3600);
        //get hour
        if ( this.hour > 1 ) { 
            if(this.hour.substr(-2) >24){
                var temp=Math.round(this.hour/24);
               
                this.dayscaluclation=temp;

                // if(this.minutes.substr(-2) >30){
                //     this.minscaluclation=.5;
                // }else{
                //     this.minscaluclation='';
                // }

            }else{

            }
            // return this.hour.substr(-2) + ":" + this.minutes.substr(-2) + ":" + seconds.substr(-2);
            return this.dayscaluclation + "days"+ this.resultvalue ;
        } else {
            // return this.minutes.substr(-2) + ":" + seconds.substr(-2);
            // if(this.minutes.substr(-2) >30){
            //     this.minscaluclation=.5;
            // }else{
            //     this.minscaluclation='';
            // }
            // return this.minutes.substr(-2) + ":" + seconds.substr(-2);
        }
       //return differencetime;
     }
   }