<?php
/**
 * Misc
 *
 * @author Jegstudio
 * @since 1.0.0
 * @package gutenverse-themes-builder
 */

namespace Gutenverse_Themes_Builder\Export;

use Gutenverse_Themes_Builder\Database\Database;
use WP_Theme_JSON_Resolver;

/**
 * Class Misc
 *
 * @package gutenverse-themes-builder
 */
class Misc {
	/**
	 * Get Colors settings
	 */
	public static function get_color_settings() {
		$theme  = wp_get_theme();
		$data   = WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( $theme );
		$data   = json_decode( $data['post_content'] );
		$colors = array();

		if ( ! empty( $data->settings->color->palette->custom ) ) {
			$colors = $data->settings->color->palette->custom;
		}

		return $colors;
	}

	/**
	 * Get Style settings
	 */
	public static function get_style_settings() {
		$theme  = wp_get_theme();
		$data   = WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( $theme );
		$data   = json_decode( $data['post_content'] );
		$styles = array();

		if ( ! empty( $data->styles ) ) {
			$styles = $data->styles;
		}

		return $styles;
	}

	/**
	 * Fix colors names
	 *
	 * @param string $content template content .
	 * @param bool   $include_global_import .
	 */
	public static function fix_colors( $content, $include_global_import ) {
		$colors = self::get_color_settings();
		// change colors slug into lowercase to prevent errors.
		if ( ! $include_global_import ) {
			foreach ( $colors as $index => $color ) {
				$new_slug = 'theme-' . $index;

				$content = str_replace( '{&quot;type&quot;:&quot;variable&quot;,&quot;id&quot;:&quot;' . $color->slug . '&quot;}', '{&quot;type&quot;:&quot;variable&quot;,&quot;id&quot;:&quot;' . $new_slug . '&quot;}', $content );
				$content = str_replace( '{"type":"variable","id":"' . $color->slug . '"}', '{"type":"variable","id":"' . $new_slug . '"}', $content );
			}
		} else {
			foreach ( $colors as $index => $color ) {
				$slug    = gutenverse_themes_builder_to_kebab_case( $color->slug );
				$content = str_replace( '{&quot;type&quot;:&quot;variable&quot;,&quot;id&quot;:&quot;' . $color->slug . '&quot;}', '{&quot;type&quot;:&quot;variable&quot;,&quot;id&quot;:&quot;' . $slug . '&quot;}', $content );
				$content = str_replace( '{"type":"variable","id":"' . $color->slug . '"}', '{"type":"variable","id":"' . $slug . '"}', $content );
			}
		}
		return $content;
	}

	/**
	 * Fix colors names
	 *
	 * @param string $content template content .
	 * @param bool   $include_global_import .
	 */
	public static function fix_colors_for_core_style_export( $content, $include_global_import ) {
		$colors = self::get_color_settings();
		// change colors slug into lowercase to prevent errors.
		if ( ! $include_global_import ) {
			foreach ( $colors as $index => $color ) {
				$new_slug = 'theme-' . $index;
				$content  = str_replace( '|' . $color->slug, '|' . $new_slug, $content );
				$content  = str_replace( '-' . $color->slug, '-' . $new_slug, $content );
			}
		} else {
			foreach ( $colors as $index => $color ) {
				$slug    = gutenverse_themes_builder_to_kebab_case( $color->slug );
				$content = str_replace( '|' . $color->slug, '|' . $slug, $content );
				$content = str_replace( '-' . $color->slug, '-' . $slug, $content );
			}
		}
		return $content;
	}

	/**
	 * Fix font size slug
	 *
	 * @param string $content template content .
	 * @param array  $slug_arr .
	 */
	public static function fix_font_size_for_core_style_export( $content, $slug_arr ) {
		foreach ( $slug_arr as $key => $value ) {
			$content = str_replace( '--wp--preset--font-size--' . $key, '--wp--preset--font-size--' . $value, $content );
			$content = str_replace( 'var:preset|font-size|' . $key, 'var:preset|font-size|' . $value, $content );

		}
		return $content;
	}

	/**
	 * Download Font .
	 *
	 * @param string $url .
	 * @param string $target_dir .
	 */
	public static function download_font( $url, $target_dir ) {
		global $wp_filesystem;

		if ( empty( $wp_filesystem ) ) {
			require_once ABSPATH . 'wp-admin/includes/file.php';
			WP_Filesystem();
		}
		$response = wp_remote_get( $url, array( 'timeout' => 60 ) );

		$contents = wp_remote_retrieve_body( $response );

		$filename = basename( wp_parse_url( $url, PHP_URL_PATH ) );
		$new_file = $target_dir . $filename;

		$wp_filesystem->put_contents( $new_file, $contents, FS_CHMOD_FILE );

		return $filename;
	}

	/**
	 * Export Core Navigation blocks
	 *
	 * @param string $html_content .
	 */
	public static function fix_core_navigation( $html_content ) {
		$html_blocks = parse_blocks( $html_content );
		$blocks      = _flatten_blocks( $html_blocks );

		foreach ( $blocks as $block ) {
			if ( 'core/navigation' === $block['blockName'] && ! empty( $block['attrs']['ref'] ) ) {
				$block_before = serialize_block( $block );
				$navigation   = get_post( $block['attrs']['ref'] );
				$attributes   = $block['attrs'];

				unset( $attributes['ref'] );

				$attributes  = wp_json_encode( $attributes );
				$content     = $navigation->post_content;
				$block_after = "<!-- wp:navigation {$attributes} -->{$content}<!-- /wp:navigation -->";

				$html_content = str_replace( $block_before, $block_after, $html_content );
			}
		}

		return $html_content;
	}

	/**
	 * Get Constant Name
	 *
	 * @param string $data .
	 *
	 * @return string
	 */
	public static function get_constant_name( $data ) {
		if ( ! is_string( $data ) ) {
			return 'CONVERT_FAILED';
		}

		$namespace = preg_replace( '/[^a-zA-Z0-9\s]/', '_', $data );
		$parts     = explode( '_', $namespace );

		foreach ( $parts as &$part ) {
			$part = strtoupper( $part );
		}

		$namespace = implode( '_', $parts );

		return $namespace;
	}

	/**
	 * Get Namespace
	 *
	 * @param string $data .
	 *
	 * @return string
	 */
	public static function get_namespace( $data ) {
		if ( ! is_string( $data ) ) {
			return 'Convert_Failed';
		}

		$namespace = preg_replace( '/[^a-zA-Z0-9\s]/', '_', $data );
		$parts     = explode( '_', $namespace );

		foreach ( $parts as &$part ) {
			$part = ucfirst( $part );
		}

		$namespace = implode( '_', $parts );

		return $namespace;
	}
	/**
	 * Copy Dir
	 *
	 * @param string $source .
	 * @param string $destination .
	 * @param array  $exception array of exception .
	 */
	public static function copy_dir( $source, $destination, $exception = array() ) {
		if ( ! is_dir( $source ) ) {
			return;
		}

		if ( ! is_dir( $destination ) ) {
			wp_mkdir_p( $destination );
		}

		$items = scandir( $source );

		foreach ( $items as $item ) {
			if ( '.' === $item || '..' === $item || in_array( $item, $exception ) ) {
				continue;
			}

			$src_path  = $source . DIRECTORY_SEPARATOR . $item;
			$dest_path = $destination . DIRECTORY_SEPARATOR . $item;

			if ( is_dir( $src_path ) ) {
				self::copy_dir( $src_path, $dest_path );
			} else {
				copy( $src_path, $dest_path );
			}
		}
	}

	/**
	 * Check if block empty
	 *
	 * @param string $content .
	 */
	public static function is_serialized_block_empty( $content ) {
		if ( has_blocks( $content ) ) {
			$blocks = parse_blocks( $content );
			foreach ( $blocks as $block ) {
				if ( ! empty( trim( strip_tags( $block['innerHTML'] ) ) ) ) {
					return false;
				}
			}
		}
		return true;
	}

	/**
	 * Create Enqueue String
	 *
	 * @param string $handler script handler name.
	 * @param string $type script type.
	 * @param string $media media path.
	 * @param string $theme_data theme data.
	 */
	public static function create_enqueue_string( $handler, $type, $media = null, $theme_data ) {
		$theme_version = self::get_constant_name( $theme_data['slug'] ) . '_VERSION';
		if ( 'null' === $media ) {
			if ( 'css' === $type ) {
				return "wp_enqueue_style( '{$handler}' );";
			} else {
				return "wp_enqueue_script( '{$handler}' );";
			}
		} elseif ( 'css' === $type ) {
				return "wp_enqueue_style( '{$handler}', {$media}, array(), {$theme_version} );";
		} else {
			return "wp_enqueue_script( '{$handler}', {$media}, array(), {$theme_version}, true );";
		}
	}

	/**
	 * String to slug
	 *
	 * @param string $string .
	 * @param string $separator .
	 */
	public static function sluggify( $string, $separator = '-' ) {
		$string = mb_strtolower( $string, 'UTF-8' );
		$string = preg_replace( '/[^a-z0-9]+/u', $separator, $string );
		$string = trim( $string, $separator );
		return $string;
	}

	/**
	 * Media Upload
	 *
	 * @param string $filename .
	 *
	 * @return WP_Error/string the ID of the media.
	 */
	public static function image_separate_resolution( $filename ) {
		// Find resolution pattern like 150x150.
		if ( preg_match( '/(\d+x\d+)/', $filename, $matches ) ) {
			$resolution = $matches[1];

			// Remove the resolution part from the filename.
			$clean_name = str_replace( "-$resolution", '_', $filename );
			return $clean_name;
		}
		return $filename;
	}
}
