import * as program from 'commander'

import {
  getClient,
  serverLogin,
  Server,
  Client,
  User
} from '../../shared/utils'

program
  .option('-u, --url <url>', 'Server url')
  .option('-n, --username <username>', 'Username')
  .option('-p, --password <token>', 'Password')
  .parse(process.argv)

if (
  !program['url'] ||
  !program['username'] ||
  !program['password']
) {
  if (!program['url']) console.error('--url field is required.')
  if (!program['username']) console.error('--username field is required.')
  if (!program['password']) console.error('--password field is required.')
  process.exit(-1)
}

getClient(program.url)
  .then(res => {
    const server = {
      url: program['url'],
      user: {
        username: program['username'],
        password: program['password']
      } as User,
      client: {
        id: res.body.client_id as string,
        secret: res.body.client_secret as string
      } as Client
    } as Server

    return serverLogin(server)
  })
  .then(accessToken => {
    console.log(accessToken)
    process.exit(0)
  })
  .catch(err => {
    console.error(err)
    process.exit(-1)
  })
