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";

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

//
// An asset type is a specific type of equipment.  Example: Lenovo 100e Chromebook.
//
export const AssetTypes = new Mongo.Collection('assetTypes');
/*

const AssetTypesSchema = new SimpleSchema({
	name: {
		type: String,
		label: "Model Name",
		optional: false,
		trim: true,
	},
	description: {
		type: String,
		label: "Description",
		optional: true,
		trim: true,
		defaultValue: "",
	},
});

AssetTypes.attachSchema(AssetTypesSchema);
 */

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

	//Debug: Show all indexes.
	// AssetTypes.rawCollection().indexes((err, indexes) => {
	// 	console.log(indexes);
	// });

	// This code only runs on the server
	Meteor.publish('assetTypes', function() {
		return AssetTypes.find({});
	});
}
Meteor.methods({
	'assetTypes.add'(name, description, year) {
		check(name, String);
		check(description, String);
		check(year, String);

		if(Roles.userIsInRole(Meteor.userId(), "admin", {anyScope:true})) {
			AssetTypes.insert({name, description, year});
		}
	},
	'assetTypes.update'(_id, name, description, year) {
		check(_id, String);
		check(name, String);
		check(description, String);
		check(year, String);

		if(Roles.userIsInRole(Meteor.userId(), "admin", {anyScope:true})) {
			AssetTypes.update({_id}, {$set: {name, description, year}});
		}
	},
	'assetTypes.remove'(_id) {
		check(_id, String);

		if(Roles.userIsInRole(Meteor.userId(), "admin", {anyScope:true})) {
			//TODO: Need to either remove all assets of this type, or change their type.
		}
	},
});

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