import { Component, OnInit, Input } from '@angular/core';
import { MatchService } from '../../services/match.service';
import { ToastService } from '../../services/toast.service';


interface HighestScoreDetails {
    score: {
        is_no: string;
        balls_faced: string;
        text: string;
    };
    match_id: string;
    match_date: string;
    vs: any;
}

interface BattingRecord {
    matches: string;
    innings: string;
    not_outs: string;
    runs: string;
    balls_faced: string;
    hundreds: string;
    fifties: string;
    fours: string;
    sixes: string;
    average: string;
    strike_rate: string;
    hs: HighestScoreDetails;
}

interface BestBowlingDetails {
    figure: {
        wickets: string;
        runs_given: string;
        text: string;
    };
    match_id: string;
    match_date: string;
    vs: HighestScoreDetails;
}

interface BowlingRecord {
    matches: string;
    innings: string;
    overs: string;
    balls_bowled: string;
    maidens: string;
    runs: string;
    wickets: string;
    average: string;
    strike_rate: string;
    economy_rate: string;
    four_wk_hauls: string;
    five_wk_hauls: string;
    best_bowling: BestBowlingDetails;
}

interface FieldingRecord {
    catches: string;
    stumpings: string;
    run_outs: string;
}

interface StatsForFormat {
    comp_type_id: string;
    comp_type: string;
    rankings: {
        bat: string;
        bowl: string;
    };
    overall: {
        batting_record: BattingRecord;
        bowling_record: BowlingRecord;
        fielding_record: FieldingRecord;
    }
}

interface RawPlayerTeamDetails {
    id: string;
    active: string;
    is_primary: string;
    is_international: string;
    text: string;
}

interface RawPlayerProfile {
    player_id: string;
    last_updated: string;
    fullname: string;
    name: string;
    dob: string;
    date_of_death: string;
    place_birth: string;
    nick_name: string;
    majorteams: {
        team: Array<RawPlayerTeamDetails>;
    };
    stats: {
        format: Array<StatsForFormat>;
    };
}

export interface RawPlayerData {
    profile: RawPlayerProfile;
}


@Component({
  selector: 'app-player-details',
  templateUrl: './player-details.component.html',
  styleUrls: ['./player-details.component.scss'],
})
export class PlayerDetailsComponent implements OnInit {
    @Input() playerId: string;
    playerDetails: RawPlayerData;

    constructor(
        private matchService: MatchService,
        private toastService: ToastService
    ) { }

    ngOnInit() {
        this.matchService.getPlayerDetails(this.playerId).then((data: RawPlayerData) => {
            console.log(data);
            this.playerDetails = data;
        }, (err) => {
            console.log(err);
            this.toastService.presentToastWithOptions("Failed to fetch player details", "danger");
        });
    }

}
