import axios from 'axios'
import * as auth from '../auth.json';
import { Config } from '../App';

const tmdb = axios.create({
  baseURL: `https://api.themoviedb.org/3/movie`,
  headers: {
    Authorization: 'Bearer ' + auth.token,
    'Content-Type': 'application/json',
  }
})
export default {
  /**
* Calls tmdb API/popular and then fires the callback with res.data.results as argument
* @param {Function} callback
* @returns {boolean}
*/
  getPopular: async function({ language, region, page }: Config, callback: Function) {
    let res = await tmdb.get('/popular',
      {
        params: {
          language,
          region,
          page
        }
      })
    console.log(res)
    if (res.status === 200) {
      res = res.data.results
      callback(res)
      return true
    } else {
      throw Error("API call failed! Response: " + JSON.stringify(res))
    }
  },
  /**
   * Calls TMDB/similar 
   */
  getSimilar: async function({ language, page }: Config, id: number, callback: Function) {
    let res = await tmdb.get(id + '/similar', {
      params: {
        language,
        page
      }
    })
    console.log(res)
    if (res.status === 200) {
      res = res.data.results
      callback(res)
      return true
    } else {
      throw Error("API call failed! Response: " + JSON.stringify(res))
    }

  }

}
