<?php
if (!defined("ROOT_PATH"))
{
	header("HTTP/1.1 403 Forbidden");
	exit;
}
class pjFrontFavs extends pjFront
{
	/**
	 * Visible favourites count: number of saved favourites minus any whose
	 * product is no longer active (hidden). Mirrors the header badge logic so
	 * the live count stays consistent with a full page reload.
	 */
	private function pjActionFavsCount($data)
	{
		if (!is_array($data) || empty($data))
		{
			return 0;
		}
		$product_ids = array();
		foreach ($data as $fav => $whatever)
		{
			$item = @unserialize($fav);
			if (is_array($item) && isset($item['product_id']))
			{
				$product_ids[] = (int) $item['product_id'];
			}
		}
		if (empty($product_ids))
		{
			return 0;
		}
		// Only count favourites whose product still EXISTS and is ACTIVE - this
		// matches what the favourites list actually shows. (Previously only hidden
		// products were subtracted, so favourites of deleted products inflated the
		// badge while the list rendered empty.)
		$active = pjProductModel::factory()
			->where('t1.status', 1)
			->whereIn('t1.id', array_unique($product_ids))
			->findAll()
			->getDataPair('id', 'id');
		$count = 0;
		foreach ($product_ids as $pid)
		{
			if (isset($active[$pid])) { $count++; }
		}
		return (int) $count;
	}

	public function pjActionAdd()
	{
		$this->setAjax(true);
		
		if ($this->isXHR())
		{
			if (isset($_COOKIE[$this->defaultCookie]))
			{
			    $cookie_value = base64_decode(stripslashes($_COOKIE[$this->defaultCookie]));
			    $data = unserialize($cookie_value);
			}
			
			if (!isset($data) || $data === FALSE)
			{
				$data = array();
			}
			
			$arr = pjUtil::normalizeCart($this->_post->raw());
			if (!empty($arr))
			{
				$data[serialize($arr)] = 1;
			}
			
			setcookie($this->defaultCookie, base64_encode(serialize($data)), time() + 60*60*24*30);
			pjAppController::jsonResponse(array('status' => 'OK', 'code' => 202, 'text' => __('system_202', true), 'favs_count' => $this->pjActionFavsCount($data)));
		}
		exit;
	}
	
	public function pjActionCheck()
	{
		$this->setAjax(true);
		
		if ($this->isXHR())
		{
			if (!isset($_COOKIE[$this->defaultCookie]) || empty($_COOKIE[$this->defaultCookie]))
			{
				pjAppController::jsonResponse(array('status' => 'ERR', 'code' => 100, 'text' => 'Fav list not set or empty.'));
			}
			
			$cookie_value = base64_decode(stripslashes($_COOKIE[$this->defaultCookie]));
			$data = unserialize($cookie_value);
			
			if (!isset($data) || $data === FALSE)
			{
				pjAppController::jsonResponse(array('status' => 'ERR', 'code' => 100, 'text' => 'Fav list is empty.'));
			}
			
			$arr = pjUtil::normalizeCart($this->_post->raw());
			$key = serialize($arr);
			if (!array_key_exists($key, $data))
			{
				pjAppController::jsonResponse(array('status' => 'ERR', 'code' => 100, 'text' => 'Stock was not found in the favs list.'));
			}
			
			pjAppController::jsonResponse(array('status' => 'OK', 'code' => 200, 'text' => 'Stock found in the favs list.'));
		}
		exit;
	}
	
	public function pjActionRemove()
	{
		$this->setAjax(true);
		
		if ($this->isXHR())
		{
			if ($this->_post->check('hash') && $this->_post->toString('hash') && isset($_COOKIE[$this->defaultCookie]) && !empty($_COOKIE[$this->defaultCookie]))
			{
			    $cookie_value = base64_decode(stripslashes($_COOKIE[$this->defaultCookie]));
			    $favs = unserialize($cookie_value);
				foreach ($favs as $key => $whatever)
				{
					if ($this->_post->toString('hash') == md5($key))
					{
						$favs[$key] = NULL;
						unset($favs[$key]);
						$favs_count = $this->pjActionFavsCount($favs);
						if (empty($favs))
						{
							$favs = "";
							$time = time() - 3600;
						} else {
						    $favs = base64_encode(serialize($favs));
							$time = time() + 60*60*24*30;
						}
						setcookie($this->defaultCookie, $favs, $time);
						$response = array('status' => 'OK', 'code' => 203, 'text' => __('system_203', true), 'favs_count' => $favs_count);
						break;
					}
				}
			}
			if (!isset($response))
			{
				$response = array('status' => 'ERR', 'code' => 102, 'text' => __('system_102', true));
			}
			pjAppController::jsonResponse($response);
		}
		exit;
	}
	
	public function pjActionEmpty()
	{
		$this->setAjax(true);
		
		if ($this->isXHR())
		{
			if (isset($_COOKIE[$this->defaultCookie]) && !empty($_COOKIE[$this->defaultCookie]))
			{
				setcookie($this->defaultCookie, "", time() - 3600);
				$response = array('status' => 'OK', 'code' => 204, 'text' => __('system_204', true), 'favs_count' => 0);
			} else {
				$response = array('status' => 'ERR', 'code' => 103, 'text' => __('system_103', true));
			}
			pjAppController::jsonResponse($response);
		}
		exit;
	}
}
?>