import mongoose from 'mongoose'

const MatchSchema = new mongoose.Schema({
  created: Date,
  idString: String,
  fileLine: Number,
  category: String,
  place: String,
  date: Date,
  player1: { type: mongoose.Schema.Types.ObjectId, ref: 'Player' },
  player2: { type: mongoose.Schema.Types.ObjectId, ref: 'Player' },
  result: String,
  doubles: Boolean
})

MatchSchema.methods.equal = function (matchData, cb) {
  for (let property in matchData) {
    if (property == 'created') return true
    if (this[property] instanceof Date) {
      if (this[property].getTime() != matchData[property].getTime()) {
        console.log('dates dont match', property, this[property], matchData[property])
        return false
      }
    } else if (this[property] instanceof mongoose.Types.ObjectId) {
        if (!this[property].equals(matchData[property])) {
            console.log('objectids dont match', this[property], matchData[property])
            return false
        }
    } else {
      if (this[property] != matchData[property]) {
        console.log('property doesnt match', property, this[property], typeof this[property], matchData[property], typeof matchData[property])
        return false
      }
    }
  }
  return true
}

const Match = mongoose.model('Match', MatchSchema)

export default Match