<?php

declare(strict_types=1);

namespace Signocore\Multilanguage\Application;

if (!defined('ABSPATH')) {
	exit;
}

use Signocore\Multilanguage\Infrastructure\Traits\Singleton;

final class SignocoreMultilanguage
{
	use Singleton;

	public string $color = '#b8642a';

	protected function init(): void
	{
		add_action('init', [$this, 'loadTranslations'], 0);
		add_filter('plugin_action_links_' . Constants::$pluginBase, [$this, 'actionLinks']);
		add_filter('plugin_row_meta', [$this, 'metaLinks'], 10, 2);

		ServiceRegistry::boot();
	}

	public function loadTranslations(): void
	{
		load_plugin_textdomain('signocore-multilanguage', false, Constants::SLUG . '/languages/');
	}

	/**
	 * @param array<int|string, string> $links
	 * @return array<int|string, string>
	 */
	public function actionLinks(array $links): array
	{
		$links['deactivate'] = str_replace('<a ', '<a style="color: #777;" ', $links['deactivate']);
		$links[] = '<a href="' . esc_url(admin_url('admin.php?page=signocore-ml-settings')) . '" style="color: ' . $this->color . ';" aria-label="' . __('Signocore Multilanguage Settings', 'signocore-multilanguage') . '">' . __('Settings', 'signocore-multilanguage') . '</a>';

		if (!LicenseManager::load()->isPro()) {
			$links[] = '<a href="' . esc_url(LicenseManager::getUpgradeUrl()) . '" target="_blank" style="color: ' . $this->color . '; font-weight: bold;" aria-label="' . __('Upgrade to Signocore Multilanguage Pro', 'signocore-multilanguage') . '">' . __('Go Pro', 'signocore-multilanguage') . '</a>';
		}

		return $links;
	}

	/**
	 * @param array<int|string, string> $pluginMeta
	 * @return array<int|string, string>
	 */
	public function metaLinks(array $pluginMeta, string $pluginFile): array
	{
		if ($pluginFile === Constants::$pluginBase) {
			return [
				sprintf(__('Version %s', 'signocore-multilanguage'), Constants::$pluginVersion),
				sprintf(__('By %s', 'signocore-multilanguage'), '<a href="https://signocore.com/" target="_blank" style="color: ' . $this->color . ';" aria-label="' . __('Signocore Website', 'signocore-multilanguage') . '">Signocore</a>'),
				'<a href="https://signocore.com/wordpress-plugins/" target="_blank" style="color: ' . $this->color . ';" aria-label="' . __('Signocore Plugins', 'signocore-multilanguage') . '">' . __('Premium Plugins', 'signocore-multilanguage') . '</a>',
				'<a href="https://signocore.com/kontakt/" target="_blank" style="color: ' . $this->color . ';" aria-label="' . __('Contact Signocore', 'signocore-multilanguage') . '">' . __('Contact Us', 'signocore-multilanguage') . '</a>',
			];
		}

		return $pluginMeta;
	}
}
