import mongoose from 'mongoose'

const PlayerSchema = new mongoose.Schema({
  created: Date,

  category: String,
  licenseNr: String,
  gender: String,
  name: String,
  firstName: String,
  birthDate: Date,
  phonePrivate: String,
  phoneWork: String,
  phoneMobile: String,
  email: String,
  ranking: String,
  licenseNrDP: String,
  nameDP: String,
  firstNameDP: String,
  birthDateDP: String,
  rankingDP: String,
  confirmed: Boolean,
  paid: Boolean,

  doubles: Boolean,
  junior: Boolean,
  fullName: String,
  phone: String,
  idString: String,
})

PlayerSchema.methods.delta = function (playerData, cb) {
  const delta = {}
  for (let property in playerData) {
    if (this[property] instanceof Date) {
      if (this[property].getTime() != playerData[property].getTime()) {
        delta[property] = {a: this[property], b: playerData[property]}
      }
    } else {
      if (this[property] != playerData[property]) {
        delta[property] = {a: this[property], b: playerData[property]}
      }
    }
  }
  return delta
}

PlayerSchema.methods.equal = function (playerData, cb) {
  const { created, _id, ...rest } = this.delta(playerData)
  return (Object.keys(rest).length === 0)
}

const Player = mongoose.model('Player', PlayerSchema)

export default Player
