var cookieStorage = Class.create({
	set: function(key, value) {
		return EasyCookie.set(key, value, {
			expires: 1,
			path: '/'
		});
	},
	get: function(key, defaultValue) {
		if (! EasyCookie.has(key)) {
			return defaultValue
		}
		return EasyCookie.get(key);
	},
	has: function(key) {
		return EasyCookie.has(key);
	},
	remove: function(key) {
		EasyCookie.remove(key, {path: '/'})
	}
});

var UPPersist = Class.create(UPStorageInterface, {
	initialize: function(observer) {
		this.observer = observer;
		
		this.globalStorage = new Persist.Store('JChatStoreGlobal_j');
		this.cookieStorage = new cookieStorage();
		
		// локальный storage
		this.localStorage = new $H();
		this.localStorageKey = 'jchatData_j';
		
		this.localCookieName = 'jchat_data_j';
		this.cookiePrefix = 'jchat_j';
		
		this.init();
	},

	init: function() {
	},


	getFromGlobalStorage: function(key, defaultValue) {
		var r = defaultValue;
		this.globalStorage.get(key, function(t, v) {
			if (v) {
				v = v.toString();
			}
			if (t) {
				try {
					v = v.evalJSON();
					v = v.evalJSON();
				}
				catch(e) {}
				if (v) {
					r = v;
				}
			}
		});
//		window.console.log('globalStorageGet: ' + key)
		return r;
	},
	
	setToGlobalStorage: function(key, value) {
//		window.console.log('globalStorageSet: ' + key)
		this.globalStorage.set(key, Object.toJSON(value));
	},
	
	/**
	 * работает с localStorage
	 */
	get: function(key, defaultValue) {
		switch (this.getKeyType(key)) {
			case 1: // храним в общей куке
				if (this.cookieStorage.has(this.localCookieName)) {
					var data = this.cookieStorage.get(this.localCookieName).evalJSON();
					if (key in data) {
						return data[key];
					}
				}
				break;
			
			case 2: // храним в отдельной куке каждое значение
				if (this.cookieStorage.has(this.cookiePrefix + key)) {
					return this.cookieStorage.get(this.cookiePrefix + key).evalJSON(); 
				}
				break;
				
			default:
				return this.getFromGlobalStorage(key, defaultValue);
				break;
		}
		
		return defaultValue;
		//return this.localStorage.get(key) || defaultValue;
	},
	
	/**
	 * работает с localStorage
	 */
	set: function(key, value) {
		switch (this.getKeyType(key)) {
			case 1: // храним в общей куке
				if (this.cookieStorage.has(this.localCookieName)) {
					var data = this.cookieStorage.get(this.localCookieName).evalJSON();
				}
				else {
					data = {};
				}
				data[key] = value;
				this.cookieStorage.set(this.localCookieName, Object.toJSON(data));
				break;
			
			case 2: // храним в отдельной куке каждое значение
				this.cookieStorage.set(this.cookiePrefix + key, Object.toJSON(value));
				break;
				
			default: // храним в storage
				this.setToGlobalStorage(key, value);
				break;
		}
		//this.localStorage.set(key, value);
	},
	
	/**
	 * работает с localStorage
	 */
	remove: function(key) {
		switch (this.getKeyType(key)) {
			case 1: // храним в общей куке
				if (this.cookieStorage.has(this.localCookieName)) {
					var data = this.cookieStorage.get(this.localCookieName).evalJSON();
					delete data[key];
					this.cookieStorage.set(this.localCookieName, Object.toJSON(data));
				}
				break;
			
			case 2: // храним в отдельной куке каждое значение
				this.cookieStorage.remove(this.cookiePrefix + key);
				break;
				
			default: // храним в storage
				this.setToGlobalStorage(key, null);
				break;
		}
	},
	
	/**
	 * работает с localStorage
	 */
	clear: function() {
		this.localStorage = new $H();
	},
	
	getKeyType: function(key) {
		cookie = [ // храним в общей cookie
			'users_to_invite',
			'active_chat',
			'chats_new_messages',
			'chat_history_updated_at',
			'permanent_rooms',
			'chat_list_updated_at',
			'idle_stopped',
			'strope_idle_timestamp',
			'chat_list_updated_at',
			'my_jabber_id',
		];

		cookies = [ // каждую в отдельной cookie
			'chat_list',
			'chat_sounds_settings',
			'display_names',
			'messages_to_send',
			'chats_with_new_messages',
			'rooms_to_enter',
			'entered_rooms'
		];

		if (cookie.indexOf(key) != -1) {
			return 1; // храним в общей куке
		}

		if (cookies.indexOf(key) != -1) {
			return 2; // храним этот ключ в отдельной куке
		}

		substring = [
			['last_message_time', 1], // общая (endsWith)
			['room_users_updated_at', 1], // общая
			['chat_name', 1], // общая
			['room_users', 1] // общая кука для юзеров всех комнат
		]

		for (var i = 0; i < substring.size(); i++) {
			var s = substring[i][0];
			if (key.startsWith(s) || key.endsWith(s)) {
				return substring[i][1];
			}
		}

		return 3; // храним в storage
	}
});
