<?php

namespace WooSwatches;

defined('ABSPATH') || exit;

class Hooks {

    public function __construct() {
        add_action( 'admin_notices',  [ $this, 'gs_wc_requirement_notice' ] );
        add_action( 'init', array( $this, 'flush_rewrite_rules' ) );
        add_action( 'current_screen', array( $this, 'remove_all_notices' ) );
        add_action( 'admin_head', [$this, 'print_plugin_icon_css'] );
        add_action( 'plugins_loaded', 'WooSwatches\gs_update_plugin_version' );
    }

    public function gs_wc_requirement_notice() {
        if ( ! class_exists( 'WooCommerce' ) ) {
            $message = __( "<strong>Variation Swatches</strong> is an add-on of <strong>WooCommerce</strong>. Please Install & activate the required plugin.", 'gs-woo-variation' );
            echo wp_kses_post( sprintf( '<div class="%1$s"><p>%2$s</p></div>', 'notice notice-error', $message ) );
        }
    }

    public function flush_rewrite_rules() {
		if ( ! get_option( 'gs_swatches_permalinks_flushed' ) ) {
			flush_rewrite_rules();
			update_option( 'gs_swatches_permalinks_flushed', 1 );
		}
	}

    public function remove_all_notices( $curr_screen ) {

		if ( $curr_screen->base === 'toplevel_page_powerup' ) {
			remove_all_actions( 'admin_notices' );
		}
	}

	public function print_plugin_icon_css() {
		echo '<style>#adminmenu .toplevel_page_woo_products_variation .wp-menu-image img,#adminmenu .menu-icon-woo_products_variation .wp-menu-image img{padding-top:8px;height:auto;opacity:.8;width:18px}</style>';
	}

}