var gDeviceInfo = null;

function getDeviceInfo(inInitInfo) {
	if(!gDeviceInfo) gDeviceInfo = new DeviceInfo(inInitInfo);
	return gDeviceInfo;
}

var DeviceInfo = Class.create({
	initialize: function() {
		this.time = null;
		this.hostname = null;
		this.domain = null;
		this.description = null;
		this.workgroup = null;
		this.system = null;
		this.product = null;
	},
	init: function(inCallback) {
		this.callback = inCallback;
		
		new EdconfRequest({
			methodName: 'getMachineInfo',
			parameters: {clock: 'yes', os: 'yes'},
			callback: this.getMachineInfoCallback.bind(this),
			onError: this.setIsUnavailable.bind(this)
		});
	},
	reload: function() {
		this.init(this.callback);
	},
	setIsUnavailable: function() {
		debug('edconfd is unavailable');
		if('Login' == document.title) {
			// The login function will alert the user
			edconfd_down = true;
			if(this.callback) this.callback();
		} else {
			// Redirect to the login page
			window.location = '/login.html';
		}
	},
	trySendCallback: function() {
		if(this.callback && this.hostname && this.time && this.domain) this.callback();
	},
	// General capabilities methods
	getIs5big: function() {
		return ('5big Network' == this.description);
	},
	getIsD2Network: function() {
		return ('d2 Network' == this.description);
	},
	getIs2big: function() {
		return ('2big Network' == this.description);
	},
	getIsBigDisk: function() {
		return ('BigDisk Network' == this.description);
	},
	getLogoUrl: function() {
		if(this.getIs5big()) {
			return "/images/ED5big.jpg";
		} else if(this.getIs2big()) {
			return "/images/ED2big.jpg";
		} else if(this.getIsD2Network()) {
			return "/images/d2Network.png";
		} else if(this.getIsBigDisk()) {
			return "/images/BigDisk.png";
		} else {
			return "/images/d2Network.png";
		}
	},
	getSupportDomains: function() {
		return this.domain.supported;
	},
	getDomainEnabled: function() {
		return this.domain.enabled;
	},
	getDomainName: function() {
		return this.domain.name;
	},
	// Device info methods
	getHostname: function() {
		return this.hostname;
	},
	getDescription: function() {
		return this.description;
	},
	getWorkgroup: function() {
		return this.workgroup;
	},
	getTime: function() {
		return this.time;
	},
	getDateString: function() {
		if('en' == getLanguage().substring(0,2)) {
			return this.time.month +'/'+ this.time.day +'/'+ this.time.year;
		} else {
			return this.time.day +'/'+ this.time.month +'/'+ this.time.year;
		}
	},
	getTimeString: function() {
		return this.time.hour +':'+ this.time.minute;
	},
	getSystem: function() {
		return this.system;
	},
	getProductInfo: function() {
		return this.product;
	},
	getRootDevice: function() {
		return this.rootDevice;
	},
	getSupportRaid: function() {
		// Hide the raid interface on consumer products
		if('BigDisk Network' == this.product) return false;
		return (0 != this.raidDevices);
	},
	getHasFan: function() {
		return !this.getIsD2Network();
	},
	getIsFrontLedEnabled: function() {
		return this.isFrontLedEnabled;
	},
	getMachineInfoCallback: function(inXmlData) {
		// General info
		this.hostname = inXmlData.getTagContent('hostname');
		this.description = inXmlData.getTagContent('description');
		this.workgroup = inXmlData.getTagContent('workgroup');
		
		// Time info
		this.setTimeInfo(inXmlData);
		
		// System info
		this.system = inXmlData.getTagAttributes('os', ['name', 'version']);
		Object.extend(this.system, inXmlData.getTagAttributes('hardware', ['arch', 'mem']));
		
		// Domain info
		this.domain = {
			name: inXmlData.getTagContent('workgroup') || '',
			enabled: (null != inXmlData.getTagContent('oldworkgroup')),
			supported: true
		};
		
		// Storage info
		this.rootDevice = inXmlData.getTagContent('rootdevice');
		this.raidDevices = inXmlData.getTagContent('raiddevices') || 0;
		
		// LED enabled
		this.isFrontLedEnabled = (inXmlData.getTagContent('frontled') != 'disabled');
		
		this.trySendCallback();
	},
	setTimeInfo: function(inXmlData) {
		this.time = inXmlData.getTagAttributes('time', ['hour', 'minute', 'second']);
		Object.extend(
			this.time,
			inXmlData.getTagAttributes('date', ['year', 'month', 'day', 'wday'])
		);
		this.time.timezone = inXmlData.getTagContent('timezone');
		Object.extend(this.time, inXmlData.getTagAttributes('timezone', ['GMT']));
		
		this.time.hour = intToString(this.time.hour, 2);
		this.time.minute = intToString(this.time.minute, 2);
		this.time.second = intToString(this.time.second, 2);
		this.time.year = intToString(this.time.year, 4);
		this.time.month = intToString(this.time.month, 2);
		this.time.day = intToString(this.time.day, 2);
	}
});
