import { Component, OnInit } from '@angular/core';
import { Location } from '@angular/common';
import { MatchService } from '../services/match.service';
import { ToastService } from '../services/toast.service';
import { SocialSharing } from '@ionic-native/social-sharing/ngx';

export interface MobileAppStanding {
    id: string;
    name: string;
    short_name: string;
    position: number;

    total: number;
    
    wins: number;
    lost: number;
    tied: number;
    no_result: number;

    net_run_rate: number;
}

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

    standings: Array<MobileAppStanding> = [];

    constructor(
        private location: Location,
        private matchService: MatchService,
        private toastService: ToastService,
        private socialSharing: SocialSharing,
    ) { }


    share() {
        let message = 'IPL League Table Standings 2021- ';

        for (let i = 0; i < this.standings.length; i += 1) {
            message += this.standings[i].short_name + (i + 1 === this.standings.length ? '.' : ', ');
        }

        message += ' To know more please visit the link! #SaddaPunjab';

        this.socialSharing.share(message, '', '', 'https://www.punjabkingsipl.in/points-table');
        console.log(message, '', '', 'https://www.punjabkingsipl.in/points-table');
    }

    ngOnInit() {
        this.matchService.getLeagueTable().then((data: Array<MobileAppStanding>) => {
            this.standings = data;
        }, (err) => {
            console.log(err);
            this.toastService.presentToastWithOptions("Failed to get standings data", "danger");
        });
    }

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

}
