import {
  DATA_URL,
  PROCEDURES,

} from 'config'

import { callApi } from 'helpers'

import { TournamentType, SportInfo } from './getMatches/types'

const proc = PROCEDURES.get_match_info

export type Team = {
  abbrev_eng: string,
  abbrev_rus: string,
  id: number,
  name_eng: string,
  name_rus: string,
  penalty_score: number | null,
  score: number,
  shirt_color: string | null,
}

export type MatchTournament = {
  id: number,
  name_eng: string,
  name_rus: string,
  sportType: number,
}

export type MatchSeason = {
  id: number,
  name: string,
}

export type VideoBound = {
  e: string,
  h: string,
  s: string,
}

export type VideoBounds = Array<VideoBound>

export enum MatchStatuses {
  Upcoming = 1,
  Active,
  Timeout,
  Finished,
  Parsed,
}

export type MatchInfo = {
  access?: boolean,
  c_match_calc_status: MatchStatuses | null,
  calc: boolean,
  country: TournamentType,
  country_id: number,
  date: string,
  has_hls?: boolean,
  has_video: boolean,
  live: boolean,
  playbackUrl?: string,
  season: MatchSeason,
  sport: SportInfo,
  storage: boolean,
  sub: boolean,
  team1: Team,
  team2: Team,
  tournament: MatchTournament,
  video_bounds?: VideoBounds,
  youtube_link?: string,
} | null

export const getMatchInfo = (sportId: number, matchId: number): Promise<MatchInfo> => {
  const config = {
    body: {
      params: {
        _p_match_id: matchId,
        _p_sport: sportId,
      },
      proc,
    },
  }

  return callApi({
    config,
    url: DATA_URL,
  })
}
