<?php
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

add_action( 'admin_menu', 'elementsell_register_admin_menu', 5 );
add_action( 'admin_menu', 'elementsell_admin_menu_cleanup_submenus', 9999 );
add_action( 'in_admin_header', 'elementsell_strip_foreign_admin_notices', 0 );
add_action( 'in_admin_header', 'elementsell_strip_foreign_admin_notices', 999 );
add_action( 'admin_head', 'elementsell_hide_foreign_admin_notices_css', 1 );

/**
 * Autor pluginu (elementsell.cz) má ElementSell (Prodeje) + podkartu Publikovat.
 * Klienti mají jen ElementSell bez podkaret.
 *
 * @return bool
 */
function elementsell_is_plugin_author_admin() {
    return function_exists( 'elementsell_should_sync_publish_endpoint' )
        && elementsell_should_sync_publish_endpoint();
}

/**
 * @deprecated Použij elementsell_is_plugin_author_admin() / klient = !author.
 * @return string publish|main
 */
function elementsell_get_admin_menu_mode() {
    return elementsell_is_plugin_author_admin() ? 'publish' : 'main';
}

/**
 * URL hlavní admin stránky ElementSell (objednávky).
 *
 * @param array<string, scalar> $args
 * @return string
 */
function elementsell_get_main_admin_url( $args = [] ) {
    return elementsell_get_admin_page_url( 'elementsell', $args );
}

/**
 * Kanonická URL admin stránky pluginu — vždy admin.php?page=slug.
 *
 * @param string                $page_slug
 * @param array<string, scalar> $args
 * @return string
 */
function elementsell_get_admin_page_url( $page_slug, $args = [] ) {
    $query = array_merge(
        [ 'page' => (string) $page_slug ],
        is_array( $args ) ? $args : []
    );

    return add_query_arg( $query, admin_url( 'admin.php' ) );
}

/**
 * @return void
 */
function elementsell_register_admin_menu() {
    add_menu_page(
        'ElementSell',
        'ElementSell',
        'manage_options',
        'elementsell',
        'elementsell_render_orders_page',
        'dashicons-cart',
        6
    );

    add_submenu_page(
        null,
        'Detail faktury',
        'Detail faktury',
        'manage_options',
        'elementsell-invoice',
        'elementsell_render_invoice_detail_page'
    );
}

/**
 * U klienta: žádné podkarty (klik na ElementSell = prodeje/statistiky).
 * U autora: Prodeje (page=elementsell) + Publikovat — první položka musí zůstat
 * prodeje, jinak WP otevře první submenu = Publikovat a statistiky „zmizí“.
 *
 * @return void
 */
function elementsell_admin_menu_cleanup_submenus() {
    global $submenu;

    if ( ! is_array( $submenu ) || empty( $submenu['elementsell'] ) || ! is_array( $submenu['elementsell'] ) ) {
        return;
    }

    if ( ! elementsell_is_plugin_author_admin() ) {
        unset( $submenu['elementsell'] );

        return;
    }

    $keep_slugs = [ 'elementsell', 'elementsell-simple-publish' ];
    $kept       = [];

    foreach ( $submenu['elementsell'] as $item ) {
        $slug = isset( $item[2] ) ? (string) $item[2] : '';

        if ( ! in_array( $slug, $keep_slugs, true ) ) {
            continue;
        }

        if ( $slug === 'elementsell' ) {
            $item[0] = 'Prodeje';
        }

        $kept[ $slug ] = $item;
    }

    $ordered = [];

    foreach ( $keep_slugs as $slug ) {
        if ( isset( $kept[ $slug ] ) ) {
            $ordered[] = $kept[ $slug ];
        }
    }

    if ( empty( $ordered ) ) {
        unset( $submenu['elementsell'] );

        return;
    }

    $submenu['elementsell'] = $ordered;
}

/**
 * Je aktuální admin stránka ElementSell (hlavní nebo skryté podstránky)?
 *
 * @return bool
 */
function elementsell_is_elementsell_admin_page() {
    if ( ! is_admin() ) {
        return false;
    }

    $page = isset( $_GET['page'] ) ? sanitize_key( wp_unslash( (string) $_GET['page'] ) ) : '';

    if ( $page === '' ) {
        return false;
    }

    return $page === 'elementsell'
        || strpos( $page, 'elementsell-' ) === 0
        || strpos( $page, 'elementsell_' ) === 0;
}

/**
 * Na stránkách ElementSell vypne notices z WP core a cizích pluginů.
 * Vlastní billing notice nechá.
 *
 * @return void
 */
function elementsell_strip_foreign_admin_notices() {
    if ( ! elementsell_is_elementsell_admin_page() ) {
        return;
    }

    remove_all_actions( 'admin_notices' );
    remove_all_actions( 'all_admin_notices' );
    remove_all_actions( 'user_admin_notices' );
    remove_all_actions( 'network_admin_notices' );

    if ( function_exists( 'elementsell_render_billing_admin_notices' ) ) {
        add_action( 'admin_notices', 'elementsell_render_billing_admin_notices' );
    }

    // PHP hlášku znovu přidat JEN když PHP opravdu nesplňuje minimum.
    // Dřív se přidávala vždy → falešné „Plugin se nenačetl“ i na PHP 8.x.
    if (
        function_exists( 'elementsell_php_version_should_block_plugin' )
        && elementsell_php_version_should_block_plugin()
        && function_exists( 'elementsell_admin_notice_php_version' )
    ) {
        add_action( 'admin_notices', 'elementsell_admin_notice_php_version' );
    }
}

/**
 * CSS záloha — co by přesto uniklo (update-nag apod.).
 *
 * @return void
 */
function elementsell_hide_foreign_admin_notices_css() {
    if ( ! elementsell_is_elementsell_admin_page() ) {
        return;
    }

    echo '<style id="elementsell-hide-foreign-notices">
		body.toplevel_page_elementsell #wpbody-content > .notice:not(.elementsell-notice),
		body.toplevel_page_elementsell #wpbody-content > .update-nag,
		body.toplevel_page_elementsell #wpbody-content > .error:not(.elementsell-notice),
		body.toplevel_page_elementsell #wpbody-content > .updated:not(.elementsell-notice),
		body.toplevel_page_elementsell #wpbody-content > .update-message,
		body[class*="page_elementsell"] #wpbody-content > .notice:not(.elementsell-notice),
		body[class*="page_elementsell"] #wpbody-content > .update-nag,
		body[class*="page_elementsell"] #wpbody-content > .error:not(.elementsell-notice),
		body[class*="page_elementsell"] #wpbody-content > .updated:not(.elementsell-notice),
		body[class*="page_elementsell"] #wpbody-content > .update-message {
			display: none !important;
		}
	</style>';
}
