import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { Location } from '@angular/common';
import * as moment from 'moment';
import { AlertController } from '@ionic/angular';

@Component({
    selector: 'app-booking-details',
    templateUrl: './booking-details.page.html',
    styleUrls: ['./booking-details.page.scss'],
})
export class BookingDetailsPage implements OnInit {
    matchDay: any;
    selectedStand: number;
    selectedSeats: Array<{
        id: string,
        price: string,
    }> = [];

    constructor(
        private route: ActivatedRoute,
        private location: Location,
        public alertController: AlertController
    ) { }

    async confirmPayment() {
        const alert = await this.alertController.create({
            header: 'Purchase Complete',
            message: 'Your ticket details will be sent over E-Mail and SMS, Enjoy the game!',
            mode: 'ios',
            buttons: [{
                text: 'Okay!',
                role: 'confirm',
                handler: () => {
                    setTimeout(() => {
                        this.back();
                    }, 100);
                }
            }]
          });
      
          await alert.present();
    }

    ngOnInit() {
        this.matchDay = JSON.parse(this.route.snapshot.paramMap.get('matchData'));
        console.log(this.matchDay);
    }

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

    getFirstChar(text: string) {
        return text.charAt(0)
    }

    getEventDateTime(dateTime: Date) {
        return moment(dateTime).format('ddd, MMM DD YYYY hh:mm a');
    }

    addSeat() {
        let seats: Array<any> = this.matchDay.seatsAvailable[this.selectedStand].seats;
        
        this.selectedSeats.push(seats.pop());
    }


    removeSeat() {
        let seats: Array<any> = this.matchDay.seatsAvailable[this.selectedStand].seats;

        seats.push(this.selectedSeats.pop());
    }

    getTotalAmount() {
        let total = 0;

        this.selectedSeats.forEach((seat) => {
            total += Number(seat.price)
        });

        return total;
    }

}
