let bcrypt = require('bcryptjs')
let randomColor = require('randomcolor')
var passportLocalSequelize = require('passport-local-sequelize');
let pagination = require('../lib/pagination.js')
const Errors = require('../lib/errors.js')
var crypto = require("crypto");
var cryptoRandomString = require("crypto-random-string");
module.exports = (sequelize, DataTypes) => {
	let User = sequelize.define('User', {
		username: {
			type: DataTypes.STRING(191),
			unique: {
				msg: 'Username is already taken, try another!',
				fields: ['username']
			},
			validate: {
				is: {
					args: [/^[a-zA-Z0-9_]*$/],
					msg: 'Username can only contain numbers and letters'
				},
				len: {
					args: [3, 16],
					msg: 'username must be between 3 and 16 characters'
				},
				isString (val) {
					if(typeof val !== 'string') {
						throw new sequelize.ValidationError('username must be a string')
					}
				},
				containsNoBlankCharacters (val) {
					if(/\s/g.test(val)) {
						throw new sequelize.ValidationError('username can\'t contain blank characters')
					}
				}
			},
			primaryKey: true
		},
		blurb: {
			type: DataTypes.TEXT,
			validate: {
				isString (val) {
					if(typeof val !== 'string') {
						throw new sequelize.ValidationError('description must be a string')
					}
				},
				len: {
					args: [0, 256],
					msg: 'description must be less than 256 characters'
				}
			}
		},
		forum_posts: {
			type: DataTypes.BIGINT
		},
		lastseen: {
			type: DataTypes.DATE
		},
		createdAt: {
			type: DataTypes.DATE
		},
		friends: {
			type: DataTypes.BIGINT
		},
		banned: {
			type: DataTypes.BOOLEAN
		}
	}, {hooks: {
			async afterValidate(user, options) {
				if(user.changed('hash') && user.hash.length <= 50) {
					user.hash = await bcrypt.hash(user.hash, 12)
				}

				options.hooks = false
				return options
			}
		}
	})
	User.associate = function (models) {
		User.hasMany(models.Post)
		User.hasMany(models.Thread)
	}
	User.prototype.getMeta = function (limit) {
		let Post = sequelize.models.Post
		let meta = {}

		let nextId = pagination.getNextIdDesc(Post, { userId: this.id }, this.Posts)

		if(nextId === null) {
			meta.nextURL = null
			meta.nextPostsCount = 0
		} else {
			meta.nextURL =
				process.env.VUE_APP_APIENDPOINT + process.env.VUE_APP_APIVERSION + `/` + `user/${this.username}?posts=true&limit=${limit}&from=${nextId - 1}`

			meta.nextPostsCount = pagination.getNextCount(
				Post, this.Posts, limit,
				{ UserId: this.id },
				true
			)
		}

		return meta
	}
	User.includeWallOptions = function (from, limit) {
		let models = sequelize.models

		return [{
			model: models.userWall,
			limit,
			where: { postNumber: { $gte: from } },
			order: [['id', 'ASC']]
		}]
	}
	User.includeOptions = function (from, limit) {
		let models = sequelize.models
		let options = models.Post.includeOptions()

		return [{
			model: models.Post,
			include: options,
			limit,
			where: { postNumber: { $gte: from } },
			order: [['id', 'ASC']]
		}]
	}
	User.prototype.comparePassword = function (password) {
		return bcrypt.compare(password, this.hash)
	}
	User.prototype.destroyVerifyPassword = function (password) {
		if(typeof password !== 'string') {
			throw Errors.invalidLoginCredentials
		}

		let correctPassword = bcrypt.compare(password, this.hash)

		if(correctPassword) {
			this.destroy()
		} else {
			throw Errors.invalidLoginCredentials
		}
	}
	User.prototype.removeKoins = function(amount) {
		if(this.koins >= amount) {
			this.update({koins: this.koins - amount})
		} else {
			throw Errors.insufficientKoins
		}
	}
	User.prototype.addKoins = function(amount) {
		this.update({koins: this.koins + amount})
	}
	User.prototype.rand = function() {
		this.update({ emailToken: cryptoRandomString({length: 250})})
	}
	User.prototype.randPasswordReset = function() {
		if(User) {
			this.update({ passwordResetToken: cryptoRandomString({length: 250}), deleteEnabled: true })
		} else {
			throw Errors.accountDoesNotExist
		}
	}
	User.prototype.randAccountDelete = function() {
		if(User) {
			this.update({ deleteCode: cryptoRandomString({length: 1024}), deleteEnabled: true})
		} else {
			throw Errors.accountDoesNotExist
		}
	}
	User.prototype.emailVerify = function () {
		this.update({ emailVerified: true })
	}
	User.prototype.updatePassword = function (currentPassword, newPassword) {
		if(currentPassword === newPassword) {
			throw Errors.passwordSame
		} else if(typeof currentPassword !== 'string' || typeof newPassword !== 'string') {
			throw new sequelize.ValidationError('Please enter your password')
		}

		let correctPassword = bcrypt.compare(currentPassword, this.hash)

		if(correctPassword) {
			this.update({ hash: newPassword })
		} else {
			throw Errors.invalidLoginCredentials
		}
	}
	User.prototype.reward  = function () {
		let ms = Date.now() - this.lastRewardDate
		let dayMs = 1000*60*60*24

		//Has less than 1 day passed
		//since generating token?
		return ms / dayMs < 1
	}
	User.prototype.doReward  = function () {
		if(User.lastRewardDate) {
			if(User.lastRewardDate.reward()) {
				throw Errors.invalidToken
			} else {
				throw Errors.invalidPassKey
			}
		} else {
			console.log("idk")
		}
	}
	User.prototype.invalidateJWT  = function () {
		this.update({ jwtOffset: this.jwtOffset + 1 })
	}
	User.prototype.recoveryUpdatePassword = function (password) {
		if(typeof password !== 'string') {
			throw new sequelize.ValidationError('Please enter your password')
		}
		this.update({ hash: password, passwordResetEnabled: false })
	}
	/* User.prototype.GenerateJWT = function () {
        const today = new Date();
        const expirationDate = new Date(today);
        expirationDate.setDate(today.getDate() + 60);

        let payload = {
            id: this.id,
            email: this.email,
            username: this.username,
        };

        return jwt.sign(payload, "AUSDHIASDHAHDAiyrgy3476rty734we6yrgwesyufeyhurfehyrurgty7346ertg645e37t6rgyu", {
            expiresIn: parseInt(expirationDate.getTime() / 1000, 10)
        });
    }, */
	User.prototype.updateEmail = function (emailCurrentPassword, newEmail) {
		if(typeof emailCurrentPassword !== 'string' || typeof newEmail !== 'string') {
			throw new sequelize.ValidationError('input must be a string')
		}

		let correctPassword = bcrypt.compare(emailCurrentPassword, this.hash)

		if(correctPassword) {
			this.update({ email: newEmail, emailVerified: false, emailToken: cryptoRandomString({length: 16})})
		} else {
			throw Errors.invalidLoginCredentials
		}
	}
	User.prototype.updateUsername = function (username, password) {
		if(typeof username !== 'string') {
			throw new sequelize.ValidationError('Please enter your new username')
		}
		if(typeof password !== 'string') {
			throw new sequelize.ValidationError('Please enter your password')
		}

		let correctPassword = bcrypt.compare(password, this.hash)
		if(correctPassword) {
			this.update({username: username, koins: this.koins - 200})
		} else {
			throw Errors.invalidLoginCredentials
		}
	}
	return User
}
