import Joi from 'joi'
import _ from 'lodash'

export default
class Translator {

    constructor(deps = {}) {
        this.Interactor = deps.Interactor || require('./Interactor').default
        this.moment = deps.moment || require('moment')
    }

    get(req, res) {
        let { params } = req

        let interactor = new this.Interactor()
        return interactor.fetchAll(params, { user: req.user })
            .then(result => {
                res.status(200).json(result)
            })
            .catch(error => {
                let status = error.status || 500
                res.status(status).json(error)
            })
    }

    getOne(req, res) {
        let { params } = req

        let interactor = new this.Interactor()
        return interactor.fetchOne(params, { user: req.user })
            .then(result => {
                res.status(200).json(result)
            })
            .catch(error => {
                let status = error.status || 500
                res.status(status).json(error)
            })
    }

    validateCreateBody(body) {
        const images = ['/images/dilma.jpg', '/images/gaben.jpg', '/images/lula.jpg', '/images/nissin.jpg', '/images/moharu.jpg']
        // Find the lower access level
        const schema = Joi.object({
            login: Joi.string().required(),
            password: Joi.string().required(),
            name: Joi.string().required(),
            image: Joi.string().default(_.sample(images)).forbidden(),
            access: Joi.string().default('user').forbidden(),
            createdAt: Joi.string().default(this.moment().toISOString()).forbidden(),
        }).required()

        return new Promise((resolve, reject) => {
            const {error, value} = Joi.validate(body, schema)
            if(error) {
                let messages = error.details.map(e => e.message)
                reject({
                    status: 400,
                    messages
                })
            } else if(value) {
                resolve(value)
            }
        })
    }

    post(req, res) {
        let { body } = req

        let interactor = new this.Interactor()
        return this.validateCreateBody(body)
            .then(result => interactor.create(result))
            .then(result => {
                res.status(201).json(result)
            })
            .catch(error => {
                console.warn(error)
                let status = error.status || 500
                res.status(status).json(error)
            })
    }

    validateLoginBody(body) {
        const schema = Joi.object({
            login: Joi.string().required(),
            password: Joi.string().required(),
        }).required()

        return new Promise((resolve, reject) => {
            const {error, value} = Joi.validate(body, schema)
            if(error) {
                let messages = error.details.map(e => e.message)
                reject({
                    status: 400,
                    messages
                })
            } else if(value) {
                resolve(value)
            }
        })
    }

    postLogin(req, res) {
        let { body } = req

        let interactor = new this.Interactor()
        return this.validateLoginBody(body)
            .then(result => interactor.login(result))
            .then(result => {
                res.status(200).json(result)
            })
            .catch(error => {
                let status = error.status || 500
                res.status(status).json(error)
            })
    }

}
