import { Component, OnInit } from '@angular/core';
import { Location } from '@angular/common';
import { Router, ActivatedRoute } from '@angular/router';
import { ServiceService } from './../../config/service.service';
import { MyordersService } from './../../config/myorder.service';

@Component({
  selector: 'app-orderplaced',
  templateUrl: './orderplaced.page.html',
  styleUrls: ['./orderplaced.page.scss']
})
export class OrderplacedPage implements OnInit {
  deliveryTime: any;
  cancelDelivery: any;
  timeinterval: any;
  constructor(
    private router: Router,
    private route: ActivatedRoute,
    private location: Location,
    private service: ServiceService,
    private myOrder: MyordersService
  ) {

  }



  data: any;

  relatedProd = {
    slidesPerView: 2
  };

  ionViewWillEnter() {
    this.deliveryTime = '00:00:00';
    this.service.get('order').then(val => {
        console.log(val);
        this.myOrder.getOrder(val).then(orderData => {
          console.log('My Orders value', this.myOrder.selItem);
          this.data = this.myOrder.selItem;
          const This = this;
          console.log(Math.round(Date.now() / 1000) + ' < ' + this.data.orderDate.seconds);
          this.timeinterval = setInterval(() => {
          This.getDeliveryTime(this.data.orderDate.seconds, this.data.time);
        }, 1000);
      });
    });
  }

  unEscape(text: string) {
    const regex = '%uFFFD';
    return unescape(text.replace(/%uFFFD/g, ''));
  }

  getDeliveryTime(timeStamp, minute) {
    let endDate = new Date(timeStamp * 1000);
    endDate = new Date(endDate.getTime() + minute * 60000);

    if (Math.round(Date.now() / 1000) < Math.round(endDate.getTime() / 1000)) {
      // console.log(endDate);
      const t = this.getTimeRemaining(endDate, new Date());
      const minutes = t.minutes < 10 ? '0' + t.minutes : t.minutes;
      const seconds = t.seconds < 10 ? '0' + t.seconds : t.seconds;
      const hours = t.hours < 10 ? '0' + t.hours : t.hours;
      this.deliveryTime = t.hours + ' : ' + minutes + ' : ' + seconds;
      if (t.t <= 0) {
        clearInterval(this.timeinterval);
      }
    }

  }

  getTimeRemaining(endtime, currDate) {
    const t = Date.parse(endtime) - Date.parse(currDate);
    this.cancelDelivery = t;
    const hours = Math.floor((((t / 1000) / 60) / 60) % 60);
    const seconds = Math.floor((t / 1000) % 60);
    const minutes = Math.floor((t / 1000 / 60) % 60);
    return {
      t,
      hours,
      minutes,
      seconds
    };
  }

  ngOnInit() { }

  goToPage(path, data = null) {
    this.router.navigateByUrl(path, { queryParams: data });
    document.body.scrollTop = document.documentElement.scrollTop = 0;
  }

  changeAddress(orderId) {
    this.service.set('orderId', orderId);
    this.router.navigateByUrl('changeaddress');
    document.body.scrollTop = document.documentElement.scrollTop = 0;
  }

  goBack() {
    this.location.back();
  }

  returnOrder(orderId, itemId) {
    
  }

  cancellOrder(orderId) {
    console.log(orderId);
    this.myOrder.cancelOrder(orderId).then((resp) => {
      if (resp) {
        this.service.showToast('Order cancelled!', 'top', 'my-toast', 1000);
        const This = this;
        setTimeout(() => {
          // This.goToPage('ordercancelled', { queryParams: this.myOrder.selItem });
        }, 1100);
      } else {
        this.service.showToast('Order cancellation failed!', 'top', 'my-error', 1000);
      }
    });
  }

  getStatus(status: number) {
    switch (status) {
      case 1:
        return 'Booked';
        break;
      case 2:
        return 'Processing';
        break;
      case 3:
        return 'Completed';
        break;
      case 4:
        return 'Cancelled';
        break;
      case 5:
        return 'Rejected';
        break;
      case 6:
        return 'Returned';
        break;
      default:
        return '';
    }
  }

  getShopperStatus(status: number) {
    switch (status) {
      case 0:
        return 'Not accepted yet';
        break;
      case 1:
        return 'Order preparing';
        break;
      case 2:
        return 'Order packed';
        break;
      case 3:
        return 'Delivered to rider';
        break;
      case 5:
        return 'Rejected';
        break;
      default:
        return '';
    }
  }

   //0: Not Accepted, 1: Accepted, 2: arrived store, 3: On the way to customer, 4: reached customer location, 6: order completed


  getRiderStatus(status: number) {
    switch (status) {
      case 1:
        return 'On the way to retailer store';
        break;
      case 2:
        return 'Arrived and waiting for the order';
        break;
      case 3:
        return 'On the way to your location';
        break;
      case 4:
        return 'Reached your location';
        break;
      default:
        return '';
    }
  }

  checkRider(riderStatus) {
    if (riderStatus === 0 || riderStatus > 4) {
      return false;
    } else {
      return true;
    }
  }

  checkRetrailer(retailerStatus, OrderStatus) {
    if (retailerStatus !== 4 && OrderStatus !== 4 && OrderStatus !== 3) {
      return true;
    } else {
      return false;
    }
  }

  getGst(price: number, gst: number) {
    const prodGst  = ((price * gst) / 100).toFixed(2);
    return prodGst;
  }
}
