import {Mongo} from "meteor/mongo";
import {Meteor} from "meteor/meteor";
import { check } from 'meteor/check';
import { Roles } from 'meteor/alanning:roles';
//import SimpleSchema from "simpl-schema";
import {AssetTypes} from "./asset-types";

// console.log("Setting Up Asset Assignments...")

export const AssetAssignments = new Mongo.Collection('assetAssignments');
/*
const TYPE_STUDENT = 1;
const TYPE_STAFF = 2;

const AssetAssignmentsSchema = new SimpleSchema({
	assetId: {
		type: String,
		label: "Asset ID",
		optional: false,
		index: 1,
		unique: false
	},
	assigneeId: {
		type: String,
		label: "Assignee ID",
		optional: false,
	},
	assigneeType: {  // 0: Student, 1: Staff
		type: SimpleSchema.Integer,
		label: "Assignee Type",
		optional: false,
		min: 0,
		max: 1,
		exclusiveMin: false,
		exclusiveMax: false,
	},
});

AssetAssignments.attachSchema(AssetAssignmentsSchema);
*/

if (Meteor.isServer) {
	// Drop any old indexes we no longer will use.  Create indexes we need.
	//try {AssetAssignments._dropIndex("name")} catch(e) {}
	//AssetAssignments.createIndex({name: "text"}, {name: "name", unique: false});

	//try {AssetTypes._dropIndex("AssetID")} catch(e) {}  //Typo put this as an index in AssetTypes instead of AssetAssignments.
	//AssetAssignments.createIndex({assetId: 1}, {name: "AssetID", unique: false});

	// This code only runs on the server
	Meteor.publish('assetAssignments', function(assetId) {
		// let query = {};
		//
		// if(assetId) {
		// 	query.assetId = assetId;
		// }
		//
		// return AssetAssignments.find(query);
		return [];
	});
}
Meteor.methods({
	/**
	 * Assigns the asset to the assignee.  The assignee should either be a Student or Staff member.
	 * @param assetId The Mongo ID of the asset (asset._id).
	 * @param assigneeType One of: 'Student', 'Staff'
	 * @param assigneeId The Mongo ID of the Student or Staff (person._id).
	 */
	'AssetAssignments.add'(assetId, assigneeType, assigneeId) {
		// check(assigneeId, String);
		// check(assigneeType, String);
		// check(assetId, String);
		//
		// if(assigneeType !== 'Student' && assigneeType !== 'Staff') {
		// 	// Should never happen.
		// 	console.error("Error: Received incorrect assignee type in adding an assignment.");
		// 	console.error(assigneeType);
		// }
		// else if(Roles.userIsInRole(Meteor.userId(), "admin", {anyScope:true})) {
		// 	AssetAssignments.insert({assetId, assigneeType, assigneeId});
		// }
	},
	'AssetAssignments.remove'(_id) {
		// check(_id, String);
		//
		// if(Roles.userIsInRole(Meteor.userId(), "admin", {anyScope:true})) {
		// 	//TODO: Need to first verify there are no checked out assets to the staff member.
		// }
	},
});

// console.log("Asset assignments setup.")