<?php
/**
 * Global Colors.
 *
 * @package Ayyash
 * @author ThemeOO
 * @since 1.1.8
 */

if ( ! defined( 'ABSPATH' ) ) {
	header( 'Status: 403 Forbidden' );
	header( 'HTTP/1.1 403 Forbidden' );
	die();
}

if ( ! class_exists( 'Themeoo_Global_Color' ) ) {
	class Themeoo_Global_Color {

		/**
		 * Themeoo_Global_Color constructor.
		 *
		 * @return void
		 */
		public function __construct() {
			add_action( 'rest_request_after_callbacks', array( $this, 'elementor_add_theme_colors' ), 999, 3 );
			add_filter( 'rest_request_after_callbacks', array( $this, 'display_global_colors_front_end' ), 999, 3 );
		}

		/**
		 * Themeoo Palettes
		 *
		 * @return array
		 */
		public function global_palette() {
			return array(
				'palette' => array(
					get_theme_mod( 'colors_global_accent' ) ? get_theme_mod( 'colors_global_accent' ) : '#2626f3',
					get_theme_mod( 'colors_global_accent_shade' ) ? get_theme_mod( 'colors_global_accent_shade' ) : '#0000f2',
					get_theme_mod( 'colors_global_heading' ) ? get_theme_mod( 'colors_global_heading' ) : '#222222',
					get_theme_mod( 'colors_global_text' ) ? get_theme_mod( 'colors_global_text' ) : '#333333',
					get_theme_mod( 'colors_global_content_bg' ) ? get_theme_mod( 'colors_global_content_bg' ) : '#f9f9f9',
				),
			);
		}

		/**
		 * Global Color Palette Slugs
		 *
		 * @return array
		 */
		public function get_palette_slugs() {
			return array(
				'ayyash_ac',
				'ayyash_ahc',
				'ayyash_hc',
				'ayyash_tc',
				'ayyash_cbc',
			);
		}

		/**
		 * Global Color Palette labels
		 *
		 * @return array
		 */
		public function get_palette_labels() {
			return array(
				__( 'Ayyash Accent Color', 'ayyash' ),
				__( 'Ayyash Accent Hover Color', 'ayyash' ),
				__( 'Ayyash Header Color', 'ayyash' ),
				__( 'Ayyash Text Color', 'ayyash' ),
				__( 'Ayyash Content Background', 'ayyash' ),
			);
		}

		/**
		 * Theme Colors for elementor
		 *
		 * @param $response
		 * @param $handler
		 * @param $request
		 *
		 * @return mixed
		 * @phpcs:disable Generic.CodeAnalysis.UnusedFunctionParameter.FoundBeforeLastUsed
		 */
		public function elementor_add_theme_colors( $response, $handler, $request ) {
			$route = $request->get_route();
			if ( '/elementor/v1/globals' != $route ) {
				return $response;
			}

			$global_palette = self::global_palette();
			$data           = $response->get_data();
			$slugs          = self::get_palette_slugs();
			$labels         = self::get_palette_labels();

			foreach ( $global_palette['palette'] as $key => $color ) {
				$id    = $slugs[ $key ] ? $slugs[ $key ] : '';
				$label = $labels[ $key ] ? $labels[ $key ] : '';

				if ( ! $id || ! $label || ! $color ) {
					continue;
				}

				$data['colors'][ $id ] = [
					'id'    => esc_attr( $id ),
					'title' => $label,
					'value' => $color,
				];
			}

			$response->set_data( $data );

			return $response;
		}

		/**
		 * Display Colors in frontend
		 *
		 * @param $response
		 * @param $handler
		 * @param $request
		 *
		 * @return mixed|WP_Error|WP_HTTP_Response|WP_REST_Response
		 * @phpcs:disable Generic.CodeAnalysis.UnusedFunctionParameter.FoundBeforeLastUsed
		 */
		public function display_global_colors_front_end( $response, $handler, $request ) {
			$route = $request->get_route();

			if ( 0 !== strpos( $route, '/elementor/v1/globals' ) ) {
				return $response;
			}

			$slug_map      = array();
			$palette_slugs = self::get_palette_slugs();

			foreach ( $palette_slugs as $key => $slug ) {
				$slug_map[ $slug ] = $key;
			}

			$rest_id = substr( $route, strrpos( $route, '/' ) + 1 );

			if ( ! in_array( $rest_id, array_keys( $slug_map ), true ) ) {
				return $response;
			}

			$colors = self::global_palette();
			return rest_ensure_response(
				array(
					'id'    => esc_attr( $rest_id ),
					'title' => 'var(--' . esc_html( $slug_map[ $rest_id ] ),
					'value' => $colors['palette'][ $slug_map[ $rest_id ] ],
				)
			);
		}
	}

	new Themeoo_Global_Color();
}

