<?php

namespace WooSwatches;

use function WooSwatchesPro\is_license_valid;

class Admin {

	public function __construct() {
		add_action( 'admin_menu', array( $this, 'admin_menu' ) );
		add_action( 'admin_enqueue_scripts', array( $this, 'scripts' ) );
	}

	public function admin_menu() {

		add_menu_page(
			__( 'Variation Swatches', 'gs-woo-variation' ),
			__( 'Woo Swatches', 'gs-woo-variation' ),
			'manage_options',
			'woo_products_variation',
			array( $this, 'admin_view' ),
			GS_WOO_SWATCHES_PLUGIN_URI . '/assets/img/icon.svg',
			6
		);

		add_submenu_page(
			'woo_products_variation',
			__( 'Variation Swatches Settings', 'gs-woo-variation' ),
			__( 'Settings', 'gs-woo-variation' ),
			'manage_options',
			'woo_products_variation',
			array( $this, 'admin_view' )
		);

	}

	public function admin_view() {
		include GS_WOO_SWATCHES_PLUGIN_DIR . 'includes/template.php';
	}

	public function scripts( $hook ) {

		if ( ! in_array( $hook, [ 'toplevel_page_woo_products_variation', 'woo-variation-swatches_page_gs-variation', 'edit-tags.php', 'term.php', 'post.php' ] ) ) {
			return;
		}

		// Styles
		wp_register_style( 'gs-swatches-grid', GS_WOO_SWATCHES_PLUGIN_URI . '/assets/libs/gs-swatches-grid/gs-swatches-grid.min.css', array(), GS_WOO_SWATCHES_VERSION );
		wp_register_style( 'gs-swatches-font-awesome', GS_WOO_SWATCHES_PLUGIN_URI . '/assets/libs/font-awesome/css/all.min.css', array(), GS_WOO_SWATCHES_VERSION );
		wp_register_style( 'gs-swatches-admin', GS_WOO_SWATCHES_PLUGIN_URI . '/assets/admin/css/admin.min.css', array('gs-swatches-font-awesome'), GS_WOO_SWATCHES_VERSION );
		wp_register_style( 'gs-swatches-single-prod', GS_WOO_SWATCHES_PLUGIN_URI . '/assets/admin/css/single-prod-item-admin.min.css', array(), GS_WOO_SWATCHES_VERSION );
		
		wp_enqueue_style( 'wp-color-picker' );
		wp_enqueue_style( 'powerup-admin-fonts', 'https://fonts.googleapis.com/css2?family=Roboto:wght@400;500;700&display=swap', array(), GS_WOO_SWATCHES_VERSION );
		wp_enqueue_style( 'gs-swatches-admin' );
		wp_enqueue_style( 'gs-swatches-grid' );
		wp_enqueue_style( 'gs-swatches-single-prod' );

		// Scripts
		wp_enqueue_media();
		
		if ( ! is_pro_active() || ! is_license_valid() ) {
			wp_register_script( 'gs-swatches-admin', GS_WOO_SWATCHES_PLUGIN_URI . '/assets/admin/js/admin.min.js', array( 'jquery' ), GS_WOO_SWATCHES_VERSION, true );
			wp_register_script( 'gs-admin-ajax', GS_WOO_SWATCHES_PLUGIN_URI . '/assets/admin/js/admin-ajax.js', array( 'jquery', 'wp-color-picker' ), GS_WOO_SWATCHES_VERSION, true );
		}

		do_action( GS_WOO_SWATCHES_HOOK_PREFIX . 'scripts' );

		wp_enqueue_script( 'gs-admin-ajax' );
		wp_enqueue_script( 'gs-swatches-admin' );

		$data = array(
			'nonce'            => wp_create_nonce( 'gs_variation_swatches' ),
			'ajax_url'         => admin_url( 'admin-ajax.php' ),
			'admin_url'        => admin_url(),
			'site_url'         => home_url(),
			'ajax_action'      => 'gs_swatches_ajax_handler',
			'translations'     => $this->get_translation_srtings(),
			'settings_options' => $this->get_setting_options(),
			'modules'          => $this->get_module_list()
		);

		$single_prod = array(
			'nonce'            => wp_create_nonce( 'gs_variation_swatches' ),
			'ajax_url'         => admin_url( 'admin-ajax.php' ),
			'product_id'       => get_the_ID()
		);

		wp_localize_script( 'gs-swatches-admin', '_swatches_data', $data );
		wp_localize_script( 'gs-admin-ajax', '_single_prod_settings', $single_prod );
	}

	function get_module_list() {
		return plugin()->modules->get_all_modules();
	}

	function get_translation_srtings() {
		return require_once GS_WOO_SWATCHES_PLUGIN_DIR . 'includes/translations.php';
	}

	function get_settings() {
		return plugin()->modules->get_settings();
	}

	function get_setting_options() {

		$pref_options = array(
			'shape_style'          => Helpers::get_shape_styles(),
			'color_field_style'    => Helpers::get_color_field_style(),
			'image_field_style'    => Helpers::get_image_field_style(),
			'button_field_style'   => Helpers::get_button_field_style(),
			'disable_attr_style'   => Helpers::get_attr_styles(),
			'attr_img_size'        => Helpers::get_img_sizes(),
		);
		
		if ( is_pro_active() && is_license_valid() ) {
			$pref_options = apply_filters( GS_WOO_SWATCHES_HOOK_PREFIX . 'setting_options', $pref_options );
		}

		return $pref_options;
	}
}
