<?php

class Thought extends PhpObject
{
	const THOUGHT_TABLE = 'thoughts';
	
	private $iId;
	private $iUserId;
	private $asOps;
	private $iCreateTimestamp;
	private $sLed;
	
	/**
	 * Database Handle
	 * @var Db
	 */
	private $oDb;
	
	public function __construct(&$oDb, $iUserId, $iId=0)
	{
		parent::__construct(__CLASS__, Settings::DEBUG);
		$this->oDb = $oDb;
		$this->setUserId($iUserId);
		$this->setId($iId);
	}
	
	public function getId()
	{
		return $this->iId;
	}
	
	public function setId($iId, $bOpen=true)
	{
		$this->iId = $iId;
		if($this->iId > 0 && $bOpen) $this->open($this->iId);
	}
	
	private function setUserId($iUserId)
	{
		$this->iUserId = $iUserId;
	}
	
	public function setOps($asOps, $bSave=false)
	{
		$this->asOps = $asOps;
		if($bSave) return $this->save();
	}
	
	public function openLast($iLimit=0)
	{
		$iId = $this->oDb->selectValue(
			self::THOUGHT_TABLE,
			"MAX(".Db::getId(self::THOUGHT_TABLE).")",
			array(Db::getId(MyThoughts::USER_TABLE) => $this->iUserId));
		
		$bSuccess = ($iId > 0);
		if($bSuccess) $this->open($iId);
		return $bSuccess;
	}
	
	public function open($iId)
	{
		if($iId > 0)
		{
			if($this->iUserId > 0) {
				$asWhere = array(Db::getId(self::THOUGHT_TABLE)=>$iId, Db::getId(MyThoughts::USER_TABLE) => $this->iUserId);
				$asInfo = $this->oDb->selectRow(self::THOUGHT_TABLE, $asWhere);
				
				$this->iId = $asInfo[Db::getId(self::THOUGHT_TABLE)];
				$this->iUserId = $asInfo[Db::getId(MyThoughts::USER_TABLE)];
				$this->asOps = self::decodeThought($asInfo[Db::getText(self::THOUGHT_TABLE)]);
				$this->iCreateTimestamp = strtotime($asInfo['created']);
				$this->sLed = $asInfo['led'];
			}
			else $this->addError('getting thought info with no user id');
		}
		else $this->addError('getting thought info with no thought id');
	}
	
	public function save()
	{
		$asThought = array(
			Db::getId(MyThoughts::USER_TABLE)	=> $this->iUserId,
			Db::getText(self::THOUGHT_TABLE)	=> self::encodeThought($this->asOps)
		);
		
		if($this->iId > 0) $this->oDb->updateRow(self::THOUGHT_TABLE, $this->iId, $asThought);
		else $this->iId = $this->oDb->insertRow(self::THOUGHT_TABLE, $asThought);
		
		return $this->iId;
	}
	
	public function get()
	{
		return array(
			'id'		=> $this->iId,
			'id_user'	=> $this->iUserId,
			'ops'		=> $this->asOps,
			'created'	=> $this->iCreateTimestamp,
			'created_f'	=> date('l, j F', $this->iCreateTimestamp),
			'led'		=> $this->sLed
		);
	}
	
	public static function getThoughtDates(Db $oDb, int $iUser)
	{
		$asInfo = array(
			'select'	=> array(Db::getId(self::THOUGHT_TABLE), 'created'),
			'from'		=> self::THOUGHT_TABLE,
			'constraint'=> array(Db::getId(MyThoughts::USER_TABLE) => $iUser),
			'orderBy'	=> array('created'=>'DESC')
		);
			
		return $oDb->selectRows($asInfo);
	}
	
	private static function encodeThought($sthought)
	{
		return base64_encode(serialize(explode("\n", self::shuffleText(json_encode($sthought)))));
	}
	
	private static function decodeThought($sEncodedThought)
	{
		return json_decode(self::shuffleText(implode("\n", unserialize(base64_decode($sEncodedThought)))), true);
	}
	
	private static function shuffleText($sText)
	{
		$sRandomText = Settings::RAND_TEXT;
		for($iIndex=0; $iIndex < strlen($sText); $iIndex++)
		{
			$sText[$iIndex] = $sRandomText[$iIndex%strlen($sRandomText)] ^ $sText[$iIndex];
		}
		return $sText;
	}
}