<?php
/**
 * Plugin bootstrap.
 *
 * @package Maca_Tjatt
 */

defined( 'ABSPATH' ) || exit;

/**
 * Class Maca_Tjatt
 */
final class Maca_Tjatt {

	/**
	 * @var Maca_Tjatt|null
	 */
	private static $instance = null;

	/**
	 * @var array<string, string>|null
	 */
	private $swedish_translations = null;

	/**
	 * @return Maca_Tjatt
	 */
	public static function instance() {
		if ( null === self::$instance ) {
			self::$instance = new self();
		}

		return self::$instance;
	}

	/**
	 * Constructor.
	 */
	private function __construct() {
		$this->includes();
		$this->init_hooks();
	}

	/**
	 * Load dependencies.
	 */
	private function includes() {
		require_once MACA_TJATT_PLUGIN_DIR . 'includes/class-maca-tjatt-db.php';
		require_once MACA_TJATT_PLUGIN_DIR . 'includes/class-maca-tjatt-ai-client.php';
		require_once MACA_TJATT_PLUGIN_DIR . 'includes/class-maca-tjatt-service.php';
		require_once MACA_TJATT_PLUGIN_DIR . 'includes/class-maca-tjatt-indexer.php';
		require_once MACA_TJATT_PLUGIN_DIR . 'includes/class-maca-tjatt-frontend.php';
		require_once MACA_TJATT_PLUGIN_DIR . 'includes/class-maca-tjatt-admin.php';
		require_once MACA_TJATT_PLUGIN_DIR . 'includes/hub-api.php';
		require_once MACA_TJATT_PLUGIN_DIR . 'includes/hub-rest.php';
		new Maca_Tjatt_Hub_Rest();
	}

	/**
	 * Register hooks.
	 */
	private function init_hooks() {
		add_action( 'init', array( $this, 'setup_translations' ), 0 );
		add_action( 'init', array( 'Maca_Tjatt_Service', 'maybe_schedule_cron' ) );
		add_filter( 'cron_schedules', array( 'Maca_Tjatt_Service', 'add_cron_schedules' ) );
		add_action( 'maca_tjatt_daily_reindex', array( 'Maca_Tjatt_Indexer', 'build_index' ) );
		add_action( 'maca_tjatt_reindex_event', array( 'Maca_Tjatt_Indexer', 'build_index' ) );
		add_action( 'save_post', array( 'Maca_Tjatt_Indexer', 'on_content_changed' ), 20, 2 );

		new Maca_Tjatt_Frontend();

		if ( is_admin() ) {
			new Maca_Tjatt_Admin();
		}
	}

	/**
	 * Register Swedish gettext filters when the site or user locale is Swedish.
	 */
	public function setup_translations() {
		if ( ! $this->should_use_swedish() ) {
			return;
		}

		add_filter( 'gettext', array( $this, 'filter_gettext' ), 10, 3 );
		add_filter( 'ngettext', array( $this, 'filter_ngettext' ), 10, 5 );
	}

	/**
	 * @return bool
	 */
	private function should_use_swedish() {
		$locale = function_exists( 'determine_locale' ) ? determine_locale() : get_locale();

		return 0 === strpos( (string) $locale, 'sv' );
	}

	/**
	 * @return array<string, string>
	 */
	private function get_swedish_translations() {
		if ( null !== $this->swedish_translations ) {
			return $this->swedish_translations;
		}

		$file = MACA_TJATT_PLUGIN_DIR . 'languages/translations-sv.php';

		if ( ! is_readable( $file ) ) {
			$this->swedish_translations = array();
			return $this->swedish_translations;
		}

		$translations = include $file;

		$this->swedish_translations = is_array( $translations ) ? $translations : array();

		return $this->swedish_translations;
	}

	/**
	 * @param string $translation Current translation.
	 * @param string $text        Original string.
	 * @param string $domain      Text domain.
	 * @return string
	 */
	public function filter_gettext( $translation, $text, $domain ) {
		if ( 'maca-tjatt' !== $domain ) {
			return $translation;
		}

		$map = $this->get_swedish_translations();

		return isset( $map[ $text ] ) ? $map[ $text ] : $translation;
	}

	/**
	 * @param string $translation Current translation.
	 * @param string $single      Singular string.
	 * @param string $plural      Plural string.
	 * @param int    $number      Item count.
	 * @param string $domain      Text domain.
	 * @return string
	 */
	public function filter_ngettext( $translation, $single, $plural, $number, $domain ) {
		if ( 'maca-tjatt' !== $domain ) {
			return $translation;
		}

		$map = $this->get_swedish_translations();

		if ( isset( $map[ $single ] ) ) {
			return $map[ $single ];
		}

		return $translation;
	}
}
