<?php
/**
 * WP admin menu registration.
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

class ZINV_Menu {

    public static function register(): void {
        add_action( 'admin_menu', [ __CLASS__, 'add_menu_page' ] );
        add_action( 'admin_head', [ __CLASS__, 'render_invoice_icon_styles' ] );
        if ( defined( 'ZINV_PLUGIN_FILE' ) ) {
            add_filter( 'plugin_action_links_' . plugin_basename( ZINV_PLUGIN_FILE ), [ __CLASS__, 'add_plugin_action_links' ] );
        }
    }

    public static function add_plugin_action_links( array $links ): array {
        $settings_link = sprintf(
            '<a href="%s">%s</a>',
            esc_url( admin_url( 'admin.php?page=zoventic-invoices' ) ),
            esc_html__( 'Settings', 'zoventic-invoices' )
        );
        array_unshift( $links, $settings_link );
        return $links;
    }

    public static function get_menu_icon(): string {
        if ( defined( 'ZINV_PLUGIN_FILE' ) ) {
            return plugins_url( 'assets/images/menu-icon.svg', ZINV_PLUGIN_FILE );
        }
        return 'dashicons-media-spreadsheet';
    }

    public static function add_menu_page(): void {
        $hook_suffix = add_menu_page(
            __( 'Zoventic Invoices', 'zoventic-invoices' ),
            __( 'Invoices', 'zoventic-invoices' ),
            'zinv_view_dashboard',
            'zoventic-invoices',
            [ __CLASS__, 'render' ],
            self::get_menu_icon(),
            26
        );

        if ( ! empty( $hook_suffix ) ) {
            add_action( 'load-' . $hook_suffix, [ __CLASS__, 'suppress_admin_notices' ] );
        }
    }

    public static function suppress_admin_notices(): void {
        remove_all_actions( 'admin_notices' );
        remove_all_actions( 'all_admin_notices' );
        remove_all_actions( 'user_admin_notices' );
        remove_all_actions( 'network_admin_notices' );
    }

    public static function render_invoice_icon_styles(): void {
        ?>
        <style id="zinv-invoice-icon-styles">
            #adminmenu #toplevel_page_zoventic-invoices .wp-menu-image,
            #toplevel_page_zoventic-invoices .wp-menu-image {
                display: flex !important;
                align-items: center !important;
                justify-content: center !important;
                width: 36px !important;
                height: 34px !important;
                float: left !important;
                padding: 0 !important;
                margin: 0 !important;
                opacity: 1 !important;
            }
            #adminmenu #toplevel_page_zoventic-invoices .wp-menu-image img,
            #toplevel_page_zoventic-invoices .wp-menu-image img {
                width: 20px !important;
                height: 20px !important;
                max-width: 20px !important;
                max-height: 20px !important;
                padding: 0 !important;
                margin: 0 auto !important;
                display: inline-block !important;
                vertical-align: middle !important;
                border-radius: 4px !important;
                opacity: 1 !important;
                filter: none !important;
                -webkit-filter: none !important;
                transition: transform 0.15s ease !important;
            }
            #adminmenu #toplevel_page_zoventic-invoices:hover .wp-menu-image img,
            #adminmenu #toplevel_page_zoventic-invoices.wp-has-current-submenu .wp-menu-image img,
            #adminmenu #toplevel_page_zoventic-invoices.current .wp-menu-image img,
            #adminmenu #toplevel_page_zoventic-invoices.wp-menu-open .wp-menu-image img {
                opacity: 1 !important;
                filter: none !important;
                -webkit-filter: none !important;
                transform: scale(1.08) !important;
            }
            body.toplevel_page_zoventic-invoices .notice,
            body.toplevel_page_zoventic-invoices .updated,
            body.toplevel_page_zoventic-invoices .error,
            body.toplevel_page_zoventic-invoices .update-nag,
            body.toplevel_page_zoventic-invoices div.updated,
            body.toplevel_page_zoventic-invoices div.error,
            body.toplevel_page_zoventic-invoices .is-dismissible,
            body.toplevel_page_zoventic-invoices #wpbody-content > .notice,
            body.toplevel_page_zoventic-invoices #wpbody-content > .update-nag,
            body.toplevel_page_zoventic-invoices #wpbody-content > .updated,
            body.toplevel_page_zoventic-invoices #wpbody-content > .error,
            body.zinv-fullscreen-app .notice,
            body.zinv-fullscreen-app .update-nag,
            body.zinv-fullscreen-app .updated,
            body.zinv-fullscreen-app .error,
            body.zinv-fullscreen-app #wpbody-content > .notice,
            body.zinv-fullscreen-app #wpbody-content > .update-nag {
                display: none !important;
            }
        </style>
        <?php
    }

    public static function render(): void {
        if ( ! current_user_can( 'zinv_view_dashboard' ) ) {
            wp_die( esc_html__( 'You do not have permission to access this page.', 'zoventic-invoices' ) );
        }
        do_action( 'zoventic_invoices_render_admin_page' );
    }
}
