import { Component, OnInit, ViewChild } from '@angular/core';
import { IonSlides } from '@ionic/angular';
import { Location } from '@angular/common';
import { Router, ActivatedRoute } from '@angular/router';
import { CartsService } from './../../config/cart.service';
import { OrdersService } from './../../config/order.service';


@Component({
  selector: 'app-cart',
  templateUrl: './cart.page.html',
  styleUrls: ['./cart.page.scss'],
})
export class CartPage implements OnInit {

  @ViewChild(IonSlides, { static: false }) slides: IonSlides;
  currentIndex: any;
  count: any;
  cartwizard = {
    initialSlide: 0,
    speed: 1000,
    allowTouchMove: false
  };

  constructor(
    private router: Router,
    private route: ActivatedRoute,
    private location: Location,
    public cartService: CartsService,
    public orderService: OrdersService
  ) {

    this.currentIndex = 0;
    this.count = 1;
    this.cartService.cartList();
  }

  ngOnInit() {
  }



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

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



  next(index: number = null) {
    console.log(index);
    if (index === 2) {
      const state = this.orderService.checkOut(this.cartService.carts).then((data) => {
        console.log('ready for slide');
        this.slides.slideNext();
      });
    } else {
      this.slides.slideNext();
    }
  }

  prev() {
    this.slides.slidePrev();
  }

  addCount() {
    // this.count = this.count+1
  }

  removeCount() {
    // this.count = this.count - 1;
    if (this.count <0){
      this.count = 0;
    }
  }


  slideChanged() {
    this.slides.getActiveIndex().then(index => {
      this.currentIndex = index;
      console.log(this.currentIndex);
    });
  } 

  sliderChange() {
    this.orderService.checkout.subscribe(state => {
      if (state) {
        this.next();
        this.orderService.checkout.next(false);
      }
    });
  }

}
