<?php
/**
 * Magento 2 Google Analytics 4 (GA4) GTM
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Anowave license that is
 * available through the world-wide-web at this URL:
 * https://www.anowave.com/license-agreement/
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade this extension to newer
 * version in the future.
 *
 * @category 	Anowave
 * @package 	Anowave_Ec
 * @copyright 	Copyright (c) 2026 Anowave (https://www.anowave.com/)
 * @license  	https://www.anowave.com/license-agreement/
 */

namespace Anowave\Ec\Block;

use Anowave\Ec\Model\ResourceModel\ConsentCookie\CollectionFactory;
use Anowave\Ec\Model\Cache;

class Cookie extends \Magento\Framework\View\Element\Template
{
	const CACHE_COOKIES_KEY = 'gdpr_cookies';

	/**
	 * @var \Anowave\Ec\Helper\Data
	 */
	protected $helper;
	
	/**
	 * @var \Anowave\Ec\Helper\Cookie
	 */
	protected $helperCookie;

	/**
	 * @var CollectionFactory
	 */
	protected $cookieCollectionFactory;

	/**
	 * @var Cache
	 */
	protected Cache $cache;
	
	/**
	 * Constructor 
	 * 
	 * @param \Magento\Framework\View\Element\Template\Context $context
	 * @param \Anowave\Ec\Helper\Data $helper
	 * @param \Anowave\Ec\Helper\Cookie $helperCookie
	 * @param array $data
	 */
	public function __construct
	(
		\Magento\Framework\View\Element\Template\Context $context,
		\Anowave\Ec\Helper\Data $helper,
	    \Anowave\Ec\Helper\Cookie $helperCookie,
		CollectionFactory $cookieCollectionFactory,
		Cache $cache,
		array $data = []
	) 
	{
		/**
		 * Set Helper
		 * @var \Anowave\Ec\Helper\Data 
		 */
		$this->helper = $helper;
		
		/**
		 * Set cookie helper 
		 * 
		 * @var \Anowave\Ec\Helper\Cookie
		 */
		$this->helperCookie = $helperCookie;

		/**
		 * Set cookie collection factory
		 */
		$this->cookieCollectionFactory = $cookieCollectionFactory;

		/**
		 * Set cache
		 */
		$this->cache = $cache;
		
		/**
		 * Parent constructor
		 */
		parent::__construct($context, $data);
	}
	
	public function getHelper()
	{
	    return $this->helper;
	}
	
	public function getCheckAll() : bool
	{
	    return 1 === (int) $this->helper->getSegmentCheckall();
	}
	
	/**
	 * Get cookie segments 
	 * 
	 * @return array
	 */
	public function getSegments() : array
	{
	    return $this->helperCookie->getSegments();
	}
	
	/**
	 * Get segment mode 
	 * 
	 * @return bool
	 */
	public function getSegmentMode() : bool
	{
	    return $this->helper->getCookieDirectiveIsSegmentMode();
	}

	/**
	 * Get known classification 
	 * 
	 * @return array
	 */
	public function getClassification() : array
	{
		if (!$this->helper->getDisplayCookies())
		{
			return [];
		}

		$cache = $this->cache->load(static::CACHE_COOKIES_KEY);

		if ($cache)
		{
			return (array) $cache;
		}
		else 
		{
			$map = [];

			foreach ($this->cookieCollectionFactory->create() as $entity)
			{
				if ($entity->getCookieSegment())
				{
				$map[$entity->getCookieSegment()][] = $entity->getData();
				}
				else 
				{
					$map[\Anowave\Ec\Helper\Constants::UNCLASSIFIED][] = $entity->getData();
				}
			}

			$this->cache->save($map, static::CACHE_COOKIES_KEY);
		}

	    return $map;
	}

	/**
	 * Display cookie information 
	 * 
	 * @return bool
	 */
	public function getDisplayCookies() : bool
	{
		return $this->helper->getDisplayCookies();
	}
	
	/**
	 * Render template
	 * 
	 * {@inheritDoc}
	 * @see \Magento\Framework\View\Element\Template::_toHtml()
	 */
	protected function _toHtml()
	{
	    return !$this->helper->isActive() ? '' : $this->helper->filter(parent::_toHtml());
	}
}