<?php

class Indexes {
 
 	private static $indexesDirectory;
	private static $lastArray;
	private static $disableSave;
	
	function Indexes($iDir){
		self::$indexesDirectory = $iDir;
		self::$lastArray = array();	
	}
	public static function setIndexesDirectory($iDir){
	 	self::$indexesDirectory = $iDir;
		self::$lastArray = array();	
	}

	public static function getValues($fileName){
		$arr = self::getArray($fileName);
		foreach($arr as $key2=>$value2){
			foreach($arr[$key2] as $key=>$value){
				$retArr[$key2][] = $key;
			}
		}
		return (array) $retArr;
	}
	
	public static function getValue($fileName, $key){
		$arr = self::getArray($fileName);
		$retArr = array();
		if(is_array($arr[$key])){
			foreach($arr[$key] as $key=>$value){
				$retArr[] = $key;
			}
		}
		return $retArr;
	}
	
	public static function setValue($fileName, $key, $value){
		$arr = self::getArray($fileName);
		$arr[$key][$value] = true;
		self::setArray($fileName, $arr);
	}
	
	public static function removeValue($fileName, $value){
		$arr = self::getArray($fileName);
	//	print("sssssssss");
		//print_r($arr );
		foreach($arr as $key=>$value2){
			if($arr[$key][$value]==true){
				if(isset($arr[$key][$value]))  unset($arr[$key][$value]);
			}
		}
		if(count($arr[$key])==0) unset($arr[$key]);
		self::setArray($fileName, $arr);
	}
	public static function removeValueByKeyValue($fileName, $key, $value){
		$arr = self::getArray($fileName);
		if(isset($arr[$key]) && isset($arr[$key][$value]))  unset($arr[$key][$value]);
		if(empty($arr[$key])) unset($arr[$key]);
		self::setArray($fileName, $arr);
	}
	public static function removeValues($fileName, $key){
		$arr = self::getArray($fileName);
		if(isset($arr[$key])) unset($arr[$key]);
		self::setArray($fileName, $arr);
	}
	
	public static function updateValue($fileName, $key, $value){
	 	self::$disableSave = true;
		self::removeValue($fileName, $value);
		self::$disableSave = false;
		self::setValue($fileName, $key, $value);
	}
	public static function updateValueByKeyOnePerOne($fileName, $key, $value){
	 	self::$disableSave = true;
		self::removeValues($fileName, $key);
		self::$disableSave = false;
		self::setValue($fileName, $key, $value);
	}
	
	
	// PRIVATE
	
	private static function getFileContent($fileName){
		$filePath = self::$indexesDirectory."/".$fileName.".txt";
		return Util::readFile($filePath);
	}
	
	private static function setFileContent($fileName, $content){
	 	if(!self::$disableSave){
			$filePath = self::$indexesDirectory."/".$fileName.".txt";
			Util::writeFile($filePath, $content);
		}
	}
	
	public static function getArray($fileName, $forceRead=false){
	 	if (isset(self::$lastArray[$fileName]) &&  is_array(self::$lastArray[$fileName])  && $forceRead==false){
	 		reset(self::$lastArray[$fileName]);
	 		return  self::$lastArray[$fileName];
	 	}
		$content = self::getFileContent($fileName);
		if($content===false) $content = serialize(array());
		return unserialize($content);
	}
	
	private static function setArray($fileName, $arr){
		self::$lastArray[$fileName] = $arr;
		$content = serialize($arr);
		self::setFileContent($fileName, $content);
	}
	
}