<?php

namespace WPML\Setup;

use WPML\Ajax\Endpoint\Upload;
use WPML\Core\LanguageNegotiation;
use WPML\Core\WP\App\Resources;
use WPML\Element\API\Languages;
use WPML\FP\Fns;
use WPML\FP\Lst;
use WPML\FP\Maybe;
use WPML\FP\Obj;
use WPML\FP\Wrapper;
use WPML\LIB\WP\Option as WPOption;
use WPML\LIB\WP\User;
use WPML\Setup\Endpoint\CheckTMAllowed;
use WPML\Setup\Endpoint\TranslationServices;
use WPML\TM\Menu\TranslationServices\Endpoints\Activate;
use WPML\TM\Menu\TranslationServices\Endpoints\Deactivate;
use WPML\TranslationMode\Endpoint\SetTranslateEverything;
use WPML\TranslationRoles\UI\Initializer as TranslationRolesInitializer;
use WPML\UIPage;
use function WPML\Container\make;

class Initializer {
	public static function loadJS() {
		Wrapper::of( self::getData() )->map( Resources::enqueueApp( 'setup' ) );
	}

	public static function getData() {
		$currentStep = Option::getCurrentStep();

		$siteUrl = self::getSiteUrl();

		Option::setOnlyMyselfAsDefault();

		$defaultLang  = self::getDefaultLang();
		$originalLang = Option::getOriginalLang();
		if ( ! $originalLang ) {
			$originalLang = $defaultLang;
			Option::setOriginalLang( $originalLang );
		}

		$userLang = Languages::getUserLanguageCode()->getOrElse( $defaultLang );

		if ( defined( 'OTGS_INSTALLER_SITE_KEY_WPML' ) ) {
			self::savePredefinedSiteKey( OTGS_INSTALLER_SITE_KEY_WPML );
		}

		$translationMethod = Option::shouldTranslateEverything( Option::getTranslateEverythingDefaultInSetup() )
			? 'automatic'
			: 'manual';

		return [
			'name' => 'wpml_wizard',
			'data' => [
				'currentStep'          => $currentStep,
				'endpoints'            => Lst::concat( [
					'setOriginalLanguage'    => Endpoint\SetOriginalLanguage::class,
					'setSupport'             => Endpoint\SetSupport::class,
					'setSecondaryLanguages'  => Endpoint\SetSecondaryLanguages::class,
					'currentStep'            => Endpoint\CurrentStep::class,
					'addressStep'            => Endpoint\AddressStep::class,
					'licenseStep'            => Endpoint\LicenseStep::class,
					'translationStep'        => Endpoint\TranslationStep::class,
					'setTranslateEverything' => SetTranslateEverything::class,
					'recommendedPlugins'     => Endpoint\RecommendedPlugins::class,
					'finishStep'             => Endpoint\FinishStep::class,
					'addLanguages'           => Endpoint\AddLanguages::class,
					'upload'                 => Upload::class,
					'getTranslationServices' => TranslationServices::class,
					'activateService'        => Activate::class,
					'deactivateService'      => Deactivate::class,
					'checkTMAllowed'         => CheckTMAllowed::class,
				], TranslationRolesInitializer::getEndPoints() ),
				'languages'            => [
					'list'               => Obj::values( Languages::withFlags( Languages::getAll( $userLang ) ) ),
					'secondaries'        => Fns::map( Languages::getLanguageDetails(), Option::getTranslationLangs() ),
					'original'           => Languages::getLanguageDetails( $originalLang ),
					'customFlagsDir'     => self::getCustomFlagsDir(),
					'predefinedFlagsDir' => \WPML_Flags::get_wpml_flags_url(),
				],
				'siteAddUrl'           => 'https://wpml.org/account/sites/?add=' . urlencode( $siteUrl ) . '&wpml_version=' . self::getWPMLVersion(),
				'siteKey'              => self::getSiteKey(),
				'usePredefinedSiteKey' => self::isPredefinedSiteKeySaved(),
				'supportValue'         => \OTGS_Installer_WP_Share_Local_Components_Setting::get_setting( 'wpml' ),
				'address'              => [
					'siteUrl'       => $siteUrl,
					'mode'          => self::getLanguageNegotiationMode(),
					'domains'       => LanguageNegotiation::getDomains() ?: [],
					'gotUrlRewrite' => got_url_rewrite(),
				],
				'isTMAllowed'              => Option::isTMAllowed() === true,
				'isTMDisabled'             => Option::isTMAllowed() === false,
				'ateBaseUrl'               => self::getATEBaseUrl(),
				'whenFinishedUrlLanguages' => admin_url( UIPage::getLanguages() ),
				'whenFinishedUrlTM'        => admin_url( UIPage::getTM() ),
				'ateSignUpUrl'             => admin_url( UIPage::getTMATE() ),
				'languagesMenuUrl'         => admin_url( UIPage::getLanguages() ),
				'adminUserName'            => User::getCurrent()->display_name,
				'translation'              => Lst::concat(
					[
						'whoModes'   => Option::getTranslationMode(),
						'method'     => $translationMethod,
						'reviewMode' => Option::getReviewMode(),
					],
					TranslationRolesInitializer::getTranslationData()
				),
			],
		];
	}

	/**
	 * @return bool
	 */
	private static function isPredefinedSiteKeySaved() {
		return function_exists( 'OTGS_Installer' )
		       && defined( 'OTGS_INSTALLER_SITE_KEY_WPML' )
		       && OTGS_INSTALLER_SITE_KEY_WPML
		       && OTGS_Installer()->get_site_key( 'wpml' ) === OTGS_INSTALLER_SITE_KEY_WPML;
	}

	/**
	 * @param string $siteKey
	 */
	private static function savePredefinedSiteKey( $siteKey ) {
		if ( function_exists( 'OTGS_Installer' ) ) {
			$args   = [
				'repository_id' => 'wpml',
				'nonce'         => wp_create_nonce( 'save_site_key_wpml' ),
				'site_key'      => $siteKey,
				'return'        => 1,
			];
			$result = OTGS_Installer()->save_site_key( $args );
			if ( empty( $result['error'] ) ) {
				icl_set_setting( 'site_key', $siteKey, true );
			}
		}
	}

	/**
	 * @return string
	 */
	private static function getLanguageNegotiationMode() {
		if (
			Option::getCurrentStep() === 'address'
			&& ! got_url_rewrite()
			&& LanguageNegotiation::getMode() === WPML_LANGUAGE_NEGOTIATION_TYPE_DIRECTORY

		) {
			return LanguageNegotiation::getModeAsString( WPML_LANGUAGE_NEGOTIATION_TYPE_PARAMETER );
		}

		return LanguageNegotiation::getModeAsString();
	}

	/**
	 * @return string
	 */
	private static function getDefaultLang() {
		$getLangFromConstant = function () {
			global $sitepress;

			return Maybe::fromNullable( $sitepress->get_wp_api()->constant( 'WP_LANG' ) )
			            ->map( Languages::localeToCode() )
			            ->getOrElse( 'en' );
		};

		return Maybe::fromNullable( WPOption::getOr( 'WPLANG', null ) )
		            ->map( Languages::localeToCode() )
		            ->getOrElse( $getLangFromConstant );

	}

	private static function getCustomFlagsDir() {
		return sprintf( '%s/flags/', Obj::propOr( '', 'baseurl', wp_upload_dir() ) );
	}

	private static function getATEBaseUrl() {
		return make( \WPML_TM_ATE_AMS_Endpoints::class )->get_base_url( \WPML_TM_ATE_AMS_Endpoints::SERVICE_ATE );
	}

	/**
	 * @return string
	 */
	private static function getWPMLVersion() {
		return Obj::prop( 'Version', get_plugin_data( WPML_PLUGIN_PATH . '/' . WPML_PLUGIN_FILE ) );
	}

	/**
	 * @return string
	 */
	private static function getSiteKey() {
		$siteKey = wpml_get_setting( 'site_key', (string) OTGS_Installer()->get_site_key( 'wpml' ) );
		return is_string( $siteKey ) && strlen( $siteKey ) === 10 ? $siteKey : '';
	}

	private static function getSiteUrl() {
		return OTGS_Installer()->get_installer_site_url('wpml');
	}
}

