<?php
/*
 * Software Name: Eorisis Pixels
 * Software URI: https://eorisis.com/shop/software/joomla/pixels
 * Author: Eorisis
 * Author URI: https://eorisis.com
 * Copyright: Copyright (C) 2011-2025 EORISIS. All Rights Reserved.
 * License: GNU General Public License version 2; See https://eorisis.com/gpl
 * Terms of Service: https://eorisis.com/tos
 */
namespace com_eorisis_pixels\framework\lib\core\view;
\defined('_JEXEC') or die;

use array_replace, str_replace;
use com_eorisis_pixels\framework\lib\core\app\config as app_config;
use com_eorisis_pixels\framework\lib\core\cms\language as cms_language;
use com_eorisis_pixels\framework\lib\core\cms\uri as cms_uri;
use com_eorisis_pixels\framework\lib\core\model\convert;
use com_eorisis_pixels\framework\lib\core\model\notices;
use com_eorisis_pixels\framework\lib\core\view\link;

class dashboard
{
	private $is_pro = false;

	// --------------------------------------------------

	public function __construct() {}

	// --------------------------------------------------

	public function main($canDo) : string
	{
		$this->is_pro = \defined('EORISIS_PIXELS_PRO');

		$html  = '<div class="eo-panel main">';
		$html .=	'<h1>'.cms_language::text('EO_DASHBOARD').'</h1>';
		$html .=	'<br>';
		$html .=	'<br>';

		if (!notices::system_plugin_issue())
		{
			$color = $this->is_pro ? 'darkgrey' : 'blue';

			// Meta Pixels
			$html .= '<div class="dashboard-links">';
			$html .=	$this->items_link('metapixels', ['title_key' => 'EO_METAPIXELS']);
			$html .=	$this->cats_link('metapixels', ['title_key' => 'EO_CATS']);
			$html .=	$this->options_link('metapixelopt', [
												'title_key' => 'EO_METAPIXEL_OPTIONS',
												'desc_key' => 'EO_METAPIXEL_OPT_DSC',
												'color' => $color]);
			$html .= '</div>';

			// Traffic Filters
			$html .= '<div class="dashboard-links">';
			$html .=	$this->items_link('trafficfilters', ['title_key' => 'EO_TRAFFICFILTERS', 'desc_key' => 'EO_CREATE_AND_MANAGE_FILTERS_DSC']);
			$html .=	$this->cats_link('trafficfilters', ['title_key' => 'EO_CATS']);
//			$html .=	$this->options_link('trafficfilteropt', [
//												'title_key' => 'EO_TRAFFICFILTER_OPTIONS',
//												'desc_key' => 'EO_TRAFFICFILTER_OPT_DSC',
//												'color' => $color]);
			$html .= '</div>';

			$html .= '<br>';

			if ($this->is_pro)
			{
				$html .= '<br><br>';
			}
		}

		$html .=	'<div class="dashboard-links">';
		$html .=		$this->options_link('pref', [
							'title_key' => 'EO_PREF',
							'desc_key' => 'EO_PREF_DSC'
						]);
		$html .=	'</div>';
		$html .=	'<div class="dashboard-links">';
		$html .=		$this->options_link('ui', [
							'title_key' => 'EO_INTERFACE',
							'desc_key' => 'EO_INTERFACE_OPT_DSC'
						]);
		$html .=	'</div>';

		$html .= '</div>';
		return $html;
	}

	// --------------------------------------------------

	public function bottom() : string
	{
		app_config::init();
		$language_uri = $this->language_uri();

		$html  = '<div class="eo-panel bottom">';
		$html .=	'<div class="eo-info">';
		$html .=		'<span class="group">';
		$html .=			'<h5>'.$this->link('https://eorisis.com'.$language_uri.app_config::get('app_uri'), cms_language::text('EO_APP_PAGE')).'</h5>';
		$html .=			'Eorisis '.cms_language::text('EO_APPS_FOR_JOOMLA_WORDPRESS').': ';
		$html .=			$this->link('https://eorisis.com'.$language_uri.'/shop/software/');
		$html .=			'<br>';
		$html .=			cms_language::text('EO_SUPPORT_JED').': '.$this->link('https://eorisis.com/'.app_config::get('short_uri').'/jed');
		$html .=			'<br>';
//		$html .=			cms_language::text('EO_SUPPORT_DEV').': '.$this->link('https://eorisis.com'.$language_uri.'/donate', cms_language::text('EO_MAKE_DONATION'));
//		$html .=			'<br>';
		$html .=			str_replace('(C)', '&copy;', app_config::get('copyright'));
		$html .=			'<div class="social">'.$this->network_links($language_uri).'</div>';
		$html .=		'</span>';
		$html .=	'</div>';
		$html .= '</div>';
		return $html;
	}

	// --------------------------------------------------

	private function link(string $uri, string $content = '') : string
	{
		return link::a($uri, [
			'content' => $content,
			'target' => '_blank',
			'title' => cms_language::text('EO_OPEN_IN_NEW_WINDOW')
		]);
	}

	// --------------------------------------------------

	private function language_uri() : string
	{
		return cms_language::current_tag() == 'el-GR' ? '/el' : '';
	}

	// --------------------------------------------------

	private function items_link(string $view, array $args) : string
	{
		if (!$this->is_pro)
		{
			return '';
		}

		$args = array_replace([
			'layout' => '',
			'title_key' => '',
			'color' => 'blue',
			'desc_key' => 'EO_CREATE_AND_MANAGE_DSC'
		], $args);

		return link::a(cms_uri::component_route($view, $args['layout']), [
			'content' => convert::to_uppercase(cms_language::text($args['title_key'])),
			'title' => cms_language::text($args['desc_key']),
			'class' => 'button-modern round fade-button-colors '.$args['color']
		]);
	}

	// --------------------------------------------------

	private function cats_link(string $view, array $args) : string
	{
		if (!$this->is_pro)
		{
			return '';
		}

		$args = array_replace([
			'title_key' => '',
			'desc_key' => 'EO_ITEMS_OPT_DSC',
			'color' => 'grey'
		], $args);

		return link::a(\com_eorisis_pixels\framework\lib\pro\cms\component::get_category_uri($view), [
			'content' => convert::to_uppercase(cms_language::text($args['title_key'])),
			'title' => cms_language::text($args['desc_key']),
			'class' => 'button-modern round fade-button-colors '.$args['color']
		]);
	}

	// --------------------------------------------------

	private function options_link(string $view, array $args = []) : string
	{
		$args = array_replace([
			'title_key' => 'EO_OPTIONS',
			'desc_key' => 'EO_ITEMS_OPT_DSC',
			'color' => 'darkgrey',
			'class' => 'button-modern round fade-button-colors',
			'is_disabled' => false
		], $args);

		if ($args['is_disabled'] and !$this->is_pro)
		{
			return '';
		}

		$arguments['content'] = convert::to_uppercase(cms_language::text($args['title_key']));

		if ($args['is_disabled'])
		{
			$uri = '#';
			$arguments['class'] = 'button-modern round disabled';

			if ($args['desc_key'])
			{
				$arguments['title'] = cms_language::text('EO_UPGRADE_TO_PRO').'. '.cms_language::text($args['desc_key']);
			}
		}
		else
		{
			$uri = cms_uri::component_task_edit($view);
			$arguments['class'] = $args['class'].' '.$args['color'];

			if ($args['desc_key'])
			{
				$arguments['title'] = cms_language::text($args['desc_key']);
			}
		}

		return link::a($uri, $arguments);
	}

	// --------------------------------------------------

	private function network_links(string $language_uri) : string
	{
		$images_dir = app_config::media_uri_relative(false).'/assets/images';
		$html = '';

		// Contact Support
		$html .= link::a('https://eorisis.com'.$language_uri.'/contact/support', [
			'content' => link::img($images_dir.'/help-circle-solid.svg', ['alt' => 'support', 'class' => 'help']),
			'target' => '_blank',
			'title' => cms_language::text('EO_CONTACT_SUPPORT')
		]);

		// Receive Newsletters
//		$html .= link::a('https://eorisis.com'.$language_uri.'/mailing-list', [
//			'content' => link::img($images_dir.'/mail-in.svg', ['alt' => 'newsletter']),
//			'target' => '_blank',
//			'title' => cms_language::text('EO_RECEIVE_NEWSLETTERS')
//		]);

		// Visit the website
		$html .= link::a('https://eorisis.com/'.$language_uri, [
			'content' => link::img($images_dir.'/internet.svg', ['alt' => 'website']),
			'target' => '_blank',
			'title' => cms_language::text('EO_VISIT_THE_WEBSITE')
		]);

		// Linkedin
		$html .= link::a('https://www.linkedin.com/company/eorisis', [
			'content' => link::img($images_dir.'/linkedin.svg', ['alt' => 'linkedin']),
			'target' => '_blank',
			'title' => cms_language::text('EO_FOLLOW_ON_LINKEDIN')
		]);

		// Facebook
		$html .= link::a('https://www.facebook.com/eorisis', [
			'content' => link::img($images_dir.'/facebook.svg', ['alt' => 'facebook']),
			'target' => '_blank',
			'title' => cms_language::text('EO_FOLLOW_ON_FACEBOOK')
		]);

		// Youtube
		$html .= link::a('https://www.youtube.com/@eorisis', [
			'content' => link::img($images_dir.'/youtube.svg', ['alt' => 'youtube']),
			'target' => '_blank',
			'title' => cms_language::text('EO_SUBSCRIBE_ON_YOUTUBE')
		]);

		return $html;
	}
}
