<?php
namespace SabaiApps\Directories\Component\WordPressContent\Helper;

use SabaiApps\Directories\Application;
use SabaiApps\Directories\Component\Entity\Model\Bundle;

class RewriteRulesHelper {

	public function help( Application $application, $lang ) {
		$taxonomy_rules = $post_type_rules = $child_post_type_rules = $user_rules = $post_type_flat_path_rules = $other_rule_settings = array();
		foreach ( $application->Entity_Bundles() as $bundle ) {
			if ( empty( $bundle->info['public'] ) ) {
				continue;
			}

			$slug      = trim( $bundle->getPath( true, $lang ), '/' );
			$_slug     = null;
			$page_name = '';
			if ( ! $application->getPlatform()->isFullSiteEditorEnabled()
				&& ! empty( $bundle->info['wordpress_page'] )
			) {
				$main_url  = strtok( $application->getPlatform()->getMainUrl(), '?' );
				$page_name = str_replace( $main_url, '', strtok( get_permalink( $bundle->info['wordpress_page'] ), '?' ) );
				if ( strpos( $page_name, 'index.php' ) === 0 ) {
					$page_name = substr( $page_name, strlen( 'index.php' ) );
				}
				$page_name = trim( $page_name, '/' );
			}
			if ( ! empty( $bundle->info['parent'] )
				|| ! empty( $bundle->info['parent_field'] )
			) {
				$child_post_type_rules[ preg_quote( $slug ) . '/([0-9]+)/?$' ] = $child_post_type_rules[ preg_quote( $slug ) . '/([0-9]+)/[^/]+/?$' ] = 'index.php?post_type=' . $bundle->name
					. '&p=$matches[1]&drts_route=' . $slug . '/$matches[1]/'
					. '&drts_pagename=' . $page_name
					. '&drts_parent_pagename=' . dirname( trim( $slug, '/' ) )
					. '&drts_lang=' . $lang;
				if ( ! empty( $bundle->info['parent'] ) ) {
					$other_rule_settings[ $bundle->info['parent'] ][ '/' . $slug ] = array( 'type' => 'child_post_type' );
				}
			} elseif ( ! empty( $bundle->info['is_user'] ) ) {
				$user_rules[ preg_quote( $slug ) . '/([^/]+)/?$' ] = 'index.php?pagename=' . ( $page_name !== '' ? $page_name : dirname( trim( $slug, '/' ) ) ) // always need pagename for WP
					. '&drts_route=' . $slug . '/$matches[1]'
					. '&drts_pagename=' . $page_name
					. '&drts_parent_pagename=' . dirname( trim( $slug, '/' ) )
					. '&drts_is_user=1'
					. '&drts_lang=' . $lang;
			} elseif ( empty( $bundle->info['is_taxonomy'] ) ) {
				$permalink_type = 'hierarchical';
				if ( ! empty( $bundle->info['permalink_type'] ) ) {
					if ( $bundle->info['permalink_type'] === 'flat'
						&& ! empty( $bundle->info['is_primary'] )
					) {
						$permalink_type = 'flat';
					} elseif ( $bundle->info['permalink_type'] === '114compat' ) {
						$permalink_type = '114compat';
					}
				}
				$permalink_path = empty( $bundle->info['permalink_path'] ) ? false : $bundle->info['permalink_path'];
				if ( $permalink_path
					&& strpos( $permalink_path, '%id%' ) !== false
				) {
					switch ( $permalink_type ) {
						case 'flat':
							$post_type_flat_path_rules[ preg_quote( trim( $bundle->getPath( false, $lang ), '/' ) ) . '/([0-9]+)/?$' ] = 'index.php?post_type=' . $bundle->name
								. '&p=$matches[1]&drts_route=' . $slug . '/$matches[1]'
								. '&drts_pagename=' . $page_name
								. '&drts_parent_pagename=' . dirname( trim( $slug, '/' ) )
								. '&drts_lang=' . $lang;
							break;
						case '114compat':
							$_slug = $bundle->info['permalink_slug_114'] ?? ( $bundle->group . '-' . $bundle->info['slug'] );
						default:
							$post_type_rules[ preg_quote( $_slug ?? $slug ) . '/(.+)/([0-9]+)/?$' ] = 'index.php?post_type=' . $bundle->name
								. '&p=$matches[2]&drts_route=' . $slug . '/$matches[1]/$matches[2]'
								. '&drts_pagename=' . $page_name
								. '&drts_parent_pagename=' . dirname( trim( $slug, '/' ) )
								. '&drts_lang=' . $lang;
							$post_type_rules[ preg_quote( $_slug ?? $slug ) . '/([0-9]+)/?$' ]      = 'index.php?post_type=' . $bundle->name
								. '&p=$matches[1]&drts_route=' . $slug . '/$matches[1]'
								. '&drts_pagename=' . $page_name
								. '&drts_parent_pagename=' . dirname( trim( $slug, '/' ) )
								. '&drts_lang=' . $lang;
					}
				} else {
					switch ( $permalink_type ) {
						case 'flat':
							$post_type_flat_path_rules[ preg_quote( trim( $bundle->getPath( false, $lang ), '/' ) ) . '/([^/]+)/?$' ] = 'index.php?post_type=' . $bundle->name
								. '&name=$matches[1]&drts_route=' . $slug . '/$matches[1]'
								. '&drts_pagename=' . $page_name
								. '&drts_parent_pagename=' . dirname( trim( $slug, '/' ) )
								. '&drts_lang=' . $lang;
							break;
						case '114compat':
							$_slug = $bundle->info['permalink_slug_114'] ?? ( $bundle->group . '-' . $bundle->info['slug'] );
						default:
							$post_type_rules[ preg_quote( $_slug ?? $slug ) . '/(.+)/([^/]+)/?$' ] = 'index.php?post_type=' . $bundle->name
								. '&name=$matches[2]&drts_route=' . $slug . '/$matches[1]/$matches[2]'
								. '&drts_pagename=' . $page_name
								. '&drts_parent_pagename=' . dirname( trim( $slug, '/' ) )
								. '&drts_lang=' . $lang;
							$post_type_rules[ preg_quote( $_slug ?? $slug ) . '/([^/]+)/?$' ]      = 'index.php?post_type=' . $bundle->name
								. '&name=$matches[1]&drts_route=' . $slug . '/$matches[1]'
								. '&drts_pagename=' . $page_name
								. '&drts_parent_pagename=' . dirname( trim( $slug, '/' ) )
								. '&drts_lang=' . $lang;
					}
				}
				if ( ! empty( $bundle->info['taxonomies'] ) ) {
					foreach ( $bundle->info['taxonomies'] as $taxonomy_bundle_name ) {
						if ( ! $taxonomy_bundle = $application->Entity_Bundle( $taxonomy_bundle_name ) ) {
							continue;
						}

						$other_rule_settings[ $bundle->name ][ '/' . trim( $taxonomy_bundle->getPath( true, $lang ), '/' ) ] = array( 'type' => 'taxonomy' );
					}
				}
			} else {
				if ( $_bundle = $bundle->isAlias() ) {
					$tax_bundle_name      = $_bundle->name;
					$orig_tax_bundle_name = $bundle->name;
				} else {
					$tax_bundle_name      = $bundle->name;
					$orig_tax_bundle_name = '';
				}
				if ( ! empty( $bundle->info['is_hierarchical'] ) ) {
					$taxonomy_rules[ preg_quote( $slug ) . '/(.+)/([^/]+)/?$' ] = 'index.php?' . $tax_bundle_name . '=$matches[2]'
						. '&drts_route=' . $slug . '/$matches[1]/$matches[2]'
						. '&drts_pagename=' . $page_name
						. '&drts_parent_pagename=' . dirname( trim( $slug, '/' ) )
						. '&drts_lang=' . $lang
						. '&drts_orig_tax_bundle_name=' . $orig_tax_bundle_name;
				}
				$taxonomy_rules[ preg_quote( $slug ) . '/([^/]+)/?$' ] = 'index.php?' . $tax_bundle_name . '=$matches[1]'
					. '&drts_route=' . $slug . '/$matches[1]'
					. '&drts_pagename=' . $page_name
					. '&drts_parent_pagename=' . dirname( trim( $slug, '/' ) )
					. '&drts_lang=' . $lang
					. '&drts_orig_tax_bundle_name=' . $orig_tax_bundle_name;
			}
		}

		$other_rules = array();
		foreach ( $application->Filter( 'wordpresscontent_subpage_rewrite_rules', $other_rule_settings, array( $lang ) ) as $bundle_name => $_other_rule_settings ) {
			if ( ! $bundle = $application->Entity_Bundle( $bundle_name ) ) {
				continue;
			}

			foreach ( $_other_rule_settings as $path => $other_rule ) {
				$type = $other_rule['type'] ?? 'other';
				switch ( $type ) {
					case 'taxonomy':
					case 'child_post_type':
						$slug = trim( $path, '/' );
						if ( get_page_by_path( $slug ) ) {
							$other_rules[ preg_quote( $slug ) . '/?$' ] = 'index.php?pagename=' . $slug
								. '&drts_route=' . $slug
								. '&drts_parent_pagename=' . dirname( trim( $slug, '/' ) )
								. '&drts_lang=' . $lang
								. '&drts_is_subpage=1';
						}
						break;
					case 'system':
						$slug = $route = trim( $bundle->getPath( false, $lang ), '/' ) . '/' . $path;
						if ( isset( $other_rule['slug'] ) ) {
							$slug = trim( $bundle->getPath( false, $lang ), '/' ) . '/' . $other_rule['slug'];
						}
						if ( get_page_by_path( $slug ) ) {
							$other_rules[ preg_quote( $slug ) . '/?$' ] = 'index.php?pagename=' . $slug
								. '&drts_route=' . $route
								. '&drts_parent_pagename=' . dirname( trim( $slug, '/' ) )
								. '&drts_lang=' . $lang
								. '&drts_is_subpage=1';
						} else {
							$other_rules[ preg_quote( $slug ) . '/?$' ] = 'index.php?pagename=' . dirname( trim( $slug, '/' ) )
								. '&drts_route=' . $route
								. '&drts_lang=' . $lang;
						}
						break;
					default:
						$slug = trim( $bundle->getPath( false, $lang ), '/' ) . '/' . $path;
						if ( get_page_by_path( $slug ) ) {
							$other_rules[ preg_quote( $slug ) . '/?$' ] = 'index.php?pagename=' . $slug;
						}
				}
			}
		}

		return $taxonomy_rules + $post_type_rules + $child_post_type_rules + $user_rules + $other_rules + $post_type_flat_path_rules;
	}
}
