{"size": 2147, "ext": "js", "max_stars_repo_path": "src/utils/MinecraftUtils.js", "max_stars_repo_name": "crazy2g/api", "max_stars_repo_stars_event_min_datetime": "2018-10-03T09:48:08.000Z", "max_stars_repo_stars_event_max_datetime": "2018-10-03T09:48:08.000Z", "max_issues_repo_path": "src/utils/MinecraftUtils.js", "max_issues_repo_name": "Crazy2G/api", "max_issues_repo_issues_event_min_datetime": "2018-10-14T17:34:28.000Z", "max_issues_repo_issues_event_max_datetime": "2018-10-15T17:56:58.000Z", "max_forks_repo_path": "src/utils/MinecraftUtils.js", "max_forks_repo_name": "Crazy2G/api", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": 1, "max_forks_count": null, "avg_line_length": 26.8375, "max_line_length": 146, "alphanum_fraction": 0.5957149511}
const rp = require('request-promise-native')
const servers = require('../resources/MinecraftServers.json')

const convertUUID = (user) => {
  return new Promise((resolve) => {
    rp.get(`https://api.mojang.com/users/profiles/minecraft/${user}`).then(res => {
      const response = res ? JSON.parse(res) : null
      if (!response) resolve('8667ba71b85a4004af54457a9734eed7')
      else resolve(response.id)
    })
  })
}

const getAvatar = (user) => {
  return new Promise((resolve) => {
    convertUUID(user).then(uuid => {
      rp.get({
        uri: `https://crafatar.com/avatars/${uuid}?overlay=true`,
        encoding: null
      }).then(image => {
        resolve(image)
      })
    })
  })
}

const getHead = (user) => {
  return new Promise((resolve) => {
    convertUUID(user).then(uuid => {
      rp.get({
        uri: `https://crafatar.com/renders/head/${uuid}?overlay=true`,
        encoding: null
      }).then(image => {
        resolve(image)
      })
    })
  })
}

const getBody = (user) => {
  return new Promise((resolve) => {
    convertUUID(user).then(uuid => {
      rp.get({
        uri: `https://crafatar.com/renders/body/${uuid}?overlay=true`,
        encoding: null
      }).then(image => {
        resolve(image)
      })
    })
  })
}

const getSkin = (user) => {
  return new Promise((resolve) => {
    convertUUID(user).then(uuid => {
      rp.get({
        uri: `https://crafatar.com/skins/${uuid}`,
        encoding: null
      }).then(image => {
        resolve(image)
      })
    })
  })
}

const getStatus = () => {
  return Promise.all(servers.map(async ({ url, name }) => {
    var response = await rp.head({ url, simple: false, resolveWithFullResponse: true, timeout: 7500, time: true }).catch(e => e)
    var online = (response.statusCode === 200 || response.statusCode === 404 || response.statusCode === 403) ? `${response.elapsedTime}ms` : false
    return { url, name, online }
  }))
}

module.exports.convertUUID = convertUUID
module.exports.getAvatar = getAvatar
module.exports.getHead = getHead
module.exports.getBody = getBody
module.exports.getSkin = getSkin
module.exports.getStatus = getStatus
