<?php

namespace Planaday\Platform\WordpressConfig;

use Planaday\Config\General\Debug\DebugSettings;
use Planaday\Platform\PlatformAbstraction\UrlHandler;
use Planaday\Utility\Debug\Log\Logging;
use Planaday\Utility\PluginData;

class WPUrlHandler implements UrlHandler {
	use Logging;

	public function __construct(
		protected PluginData $pluginData,
		protected DebugSettings $debugSettings
	) {}

	public function getUrlQueryField( string $fieldName): ?string {
		global $wp_query;
		$queryVars = $wp_query->query_vars;

		if (array_key_exists($fieldName, $queryVars)) {
			return urldecode($queryVars[$fieldName]);
		}

		return null;
	}

	public function getPageWithShortCode(string $shortcode):?string {
		$pages = get_pages();
		foreach ($pages as $page) {
			if (has_shortcode($page->post_content, $shortcode)) {
				return '/' . $page->post_name;
			}
		}

		return null;
	}

	public function getPagesWithShortCode(string $shortcode):array {
		$pages = get_pages();
		$outputPages = [];

		foreach ($pages as $page) {
			if (has_shortcode($page->post_content, $shortcode)) {
				$outputPages[$page->ID] = [
					'title' => $page->post_title,
					'id' => $page->ID
				];
			}
		}

		return $outputPages;
	}

	public function getPagesWithBlock(string $blockName):array {
		$pages = get_pages();
		$outputPages = [];

		foreach ($pages as $page) {
			if (has_block($blockName, $page)) {
				$outputPages[$page->ID] = [
					'title' => $page->post_title,
					'id' => $page->ID
				];
			}
		}

		return $outputPages;
	}

	/**
	 * @return array<array{title:string, id:int}>
	 */
	public function getAllPages(): array {
		$pages = get_pages();

		if ($pages === false) {
			return [];
		}

		$outputPages = [];
		foreach ($pages as $page) {
			$outputPages[$page->ID] = [
				'title' => $page->post_title,
				'id' => $page->ID
			];
		}

		return $outputPages;
	}

	public function getBaseUrl(): string {
		$baseUrlOverride = $this->debugSettings->getBaseUrlOverride();
		if (!empty($baseUrlOverride)) return $baseUrlOverride;
		return get_site_url();
	}

	public function getWebsiteName(): string {
		return get_bloginfo();
	}

	/**
	 * @param int $pageId
	 *
	 * @return false|string
	 */
	public function getPageUrl( int $pageId ): false|string{
		$this->logInfo('getting url for page with id: ' . $pageId);
		$permaLink = get_permalink($pageId);

		$this->logInfo('permalink: ' . $permaLink);
		return $permaLink;
	}

	public function currentPageHasShortCode( string $shortcode ): bool {
		global $post;
		return has_shortcode( $post->post_content, $shortcode );
	}

	public function redirect( string $url ): void {
		if ( wp_redirect( $url ) ) {
			exit;
		}
	}

	public function flushRewriteRules(): void {
		$this->logInfo("Flushing rewrite rules");
		flush_rewrite_rules();
	}

	public function getFileUrl(string $filePath): string {
		return plugins_url(
			substr(
				$filePath,
				strlen(dirname($filePath)) + 1
			),
			$filePath
		);
	}

	public function getCurrentPageId(): int {
		global $post;
		return $post->ID;
	}

	public function getRestApiPrefix():string{
		return '/wp-json/' . $this->pluginData->getFullPrefix() . '/v1/';
	}
}