import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams, ModalController, AlertController, MenuController } from 'ionic-angular';
import { Myservice } from "../../providers/myservice";
import { Storage } from "@ionic/storage";
import { driver } from "../../models/mymodel";
import { dataService } from "../../providers/common.service";
import { Toast } from "@ionic-native/toast";
import { TranslateService } from "@ngx-translate/core";

@IonicPage()
@Component({
  selector: 'page-quickride',
  templateUrl: 'quickride.html',
})
export class QuickridePage {
  bookingId = this.navParams.get('id')
  rideDetail: any;
  local: driver
  baseUrl = this.myservice.base_url
  lang: string = 'en';

  constructor(public navCtrl: NavController, public navParams: NavParams, private myservice: Myservice, private storage: Storage, private data: dataService, private toast: Toast, private modalCtrl: ModalController, public alertCtrl: AlertController, private translate: TranslateService, private menu: MenuController) {
  }

  ionViewDidEnter() {
    this.menu.swipeEnable(false)
    this.langTrans()

    this.storage.get('driver_data').then(data => {
      if (data) {
        this.local = data;
      }
    })

    this.myservice.load_post({ id: this.bookingId }, 'getBookingDetails').subscribe(resp => {
      if (resp.status == 'success') {
        this.rideDetail = resp.data

        var tempf = this.rideDetail.pickup_area.split(',')

        this.rideDetail.from = tempf[0]
        this.rideDetail.fromAd = this.rideDetail.pickup_area
        this.rideDetail.timestamp = new Date(this.rideDetail.pickup_date + ' ' + this.rideDetail.pickup_time).getTime()

        var temp = this.rideDetail.drop_area.split(',')
        this.rideDetail.to = temp[0]
        this.rideDetail.toAd = this.rideDetail.drop_area

        this.data.setrideData(this.rideDetail)
      }
      else {
        this.translate.get(['quickride.' + resp.message]).subscribe(value => {
          this.myservice.show_alert('', value['quickride.' + resp.message])
        })
      }
    })
  }

  langTrans() {
    this.storage.get('lang').then(lang => {
      if (lang != null) {
        this.translate.use(lang)
        this.lang = lang;
      }
      else {
        this.translate.use('en')
      }
    })
  }

  accept() {
    this.myservice.load_post({ bookingId: this.bookingId, driverId: this.local.id }, 'rideAccept').subscribe(resp => {
      if (resp.status == 'success') {
        this.translate.get(['quickride.' + resp.message]).subscribe(value => {
          this.toast.show(value['quickride.' + resp.message], '2000', 'center').subscribe(toast => {
            console.log(toast);
          });
        })
        this.navCtrl.pop()
      }
      else {
        this.translate.get(['quickride.' + resp.message]).subscribe(value => {
          this.myservice.show_alert('', value['quickride.' + resp.message])
        })
      }
    })
  }

  reject() {
    let modal = this.modalCtrl.create('RejectPage', { bookId: this.bookingId })
    modal.onDidDismiss(data => {
      if (data) {
        const alert = this.alertCtrl.create({
          title: 'Success',
          message: data,
          buttons: [
            {
              text: 'Ok',
              handler: data => {
                this.navCtrl.setRoot('HomePage')
              }
            }
          ]
        });
        alert.present();
      }
    })
    modal.present();
  }

  pick() {
    this.myservice.show_loader()
    this.navCtrl.push('UserlocationPage')
  }

  back() {
    this.navCtrl.pop()
  }

  ionViewWillLeave() {
    this.menu.swipeEnable(true)
  }
}
