'use strict'
/**
 * Dependencies
 */
const cwd = process.cwd()
const path = require('path')
const firebase = require('firebase')
const BaseHandler = require(path.join(cwd, 'handlers', 'BaseHandler'))
const User = require(path.join(cwd, 'models', 'User'))
const Game = require(path.join(cwd, 'models', 'Game'))
const Player = require(path.join(cwd, 'models', 'Player'))

/**
 * Scope
 */
const scope = 'RegisterHandler'

/**
 * RegisterHandler
 */
class RegisterHandler extends BaseHandler {

  static handle (event) {
    let handler = new RegisterHandler(event)

    Promise
      .resolve({handler})
      .then(handler.validate)
      .then(handler.registerPlayer)
      .then(handler.mapGamePlayer)
      .then(handler.done)
      .catch(handler.internalServerError({
        scope
      }))
  }
    
  validate (opts) {
    let {handler} = opts

    if (!handler.event || !handler.event.subject || !handler.event.game) {
      return handler.error({
        scope,
        message: 'registration event must have subject and game fields'
      })
    }

    return new Promise ((resolve, reject) => {
      return Promise.all([
        Game.get(handler.event.game),
        User.get(handler.event.subject)
      ]).then((game, user) => {
        if (!game) {
          return handler.error({
            scope,
            message: `${handler.event.game} is an invalid game`
          })
        }

        if (!user) {
          return handler.error({
            scope,
            message: `${handler.event.subject} is an invalid user`
          })
        }

        if (game.status !== 'registration') {
          return handler.error({
            scope,
            message: `${game.name} is not open for registration`
          })
        }

        resolve({
          handler,
          game,
          user
        })
      })
    })
  }

  registerPlayer (opts) {
    let {handler, game, user} = opts

    return new Promise((resolve, reject) => {
      return Player.get(handler.game.name, handler.user.uid).loaded
        .then((player) => {
          return player.create({
            game: game_name,
            uid: subject,
            display_name: user.username,
            picture: user.picture,
            game_state: 'human',
            secret: player.newSecret()
          })
        }).then(() => {
          resolve({
            handler,
            game,
            user,
            player
          })
        }).catch((err) => {
          return handler.error({
            scope,
            message: `Could not create player ${user.uid} in game ${game.name}`
          })
        })
    })
  }

  mapGamePlayer (opts) {
    let {handler, game, player} = opts

    return new Promise((resolve, reject) => {
      return game.mapPlayer(player)
        .then(() => {
          resolve(opts)
        }).catch((err) => {
          return handler.error({
            scope,
            message: `Could not map player to game`
          })
        })
    })
  }

}

/**
 * Exports
 */
module.exports = RegisterHandler

@@ -2045,32 +2045,357 @@
 n((player) =%3E %7B%0A
+          return new Promise((resolve, reject) =%3E %7B%0A            Secret.create(player.uid)%0A              .then((secret) =%3E %7B%0A                resolve(%7B%0A                  secret,%0A                  player%0A                %7D)%0A              %7D)%0A          %7D)%0A        %7D)%0A        .then((opts) =%3E %7B%0A          let %7Bsecret, player%7D = opts%0A
           return

refactor to use new Secret model