<?php
    /**
     * Admin: Branding bar
     *
     *
     */
    if (! defined('ABSPATH')) {
        exit;
    }
    // phpcs:ignore WordPress.Security.NonceVerification.Recommended
    $page = isset($_GET['page']) ? sanitize_text_field(wp_unslash($_GET['page'])) : '';
    // phpcs:ignore WordPress.Security.NonceVerification.Recommended
    $active_tab = isset($_GET['tab']) ? sanitize_text_field(wp_unslash($_GET['tab'])) : '';
    $add_new    = false;
    switch ($page) {
        case 'chama':
            $breadcrumb = "Dashboard &rsaquo; Welcome";
            break;
        case 'chama-settings':
            $breadcrumb = "Dashboard &rsaquo; Settings";
            break;
    }

    switch ($active_tab) {
        case 'welcome':
            $breadcrumb = "Dashboard &rsaquo; Welcome";
            break;
        case 'getting-started':
            $breadcrumb = "Dashboard &rsaquo; Getting Started";
            break;
        case 'upgrade':
            $breadcrumb = "Dashboard &rsaquo; Upgrade";
            break;
        case 'stripe':
            $breadcrumb = "Settings &rsaquo; Stripe";
            break;
        case 'users':
            $breadcrumb = "Users";
            break;
        case 'recaptcha':
            $breadcrumb = "Settings &rsaquo; Google reCAPTCHA";
            break;
        case 'pages':
            $breadcrumb = "Settings &rsaquo; Pages";
            break;
        case 'content':
            $breadcrumb = "Settings &rsaquo; Content";
            break;
        case 'protection':
            $breadcrumb = "Settings &rsaquo; Protection";
            break;
        case 'display':
            $breadcrumb = "Settings &rsaquo; Display";
            break;
        case 'branding':
            $breadcrumb = "Settings &rsaquo; Branding";
            break;
        case 'email-settings':
            $breadcrumb = "Settings &rsaquo; Email Settings";
            break;
        case 'email-templates':
            $breadcrumb = "Settings &rsaquo; Email Templates";
            break;
        case 'newsletter':
            $breadcrumb = "Settings &rsaquo; Newsletter";
            break;
        case 'limits':
            $breadcrumb = "Settings &rsaquo; Payment Limits";
            break;
        case 'miscellaneous':
            $breadcrumb = "Settings &rsaquo; Miscellaneous";
            break;
        case 'analytics':
            $breadcrumb = "Settings &rsaquo; Analytics";
            break;

    }
    // phpcs:ignore WordPress.Security.NonceVerification.Recommended
    $is_edit = isset($_GET['action']) && $_GET['action'] == 'edit';
    $screen  = get_current_screen();
    $is_add  = $screen->action == 'add';
    // phpcs:ignore WordPress.Security.NonceVerification.Recommended
    $is_all = ! $is_add && isset($_GET['post_type']);

    // phpcs:ignore WordPress.Security.NonceVerification.Recommended
    $post_type = isset($_GET['post_type']) ? sanitize_text_field(wp_unslash($_GET['post_type'])) : '';
    // phpcs:ignore WordPress.Security.NonceVerification.Recommended
    $post_type          = ! $post_type && isset($_GET['post']) ? get_post_type(sanitize_text_field(wp_unslash($_GET['post']))) : $post_type;
    $breadcrumb         = $post_type ? '' : $breadcrumb;
    $test_mode_donation = false;
    $test_mode_order    = false;
    // phpcs:ignore WordPress.Security.NonceVerification.Recommended
    $post_id = isset($_GET['post']) ? sanitize_text_field(wp_unslash($_GET['post'])) : '';
    if ($post_type) {
        $labels = get_post_type_labels(get_post_type_object($post_type));
        switch ($post_type) {
            case 'chama_tier':
            case 'chama_commission':
            case 'chama_campaign':

                $breadcrumb = $is_edit ? $labels->edit_item : $breadcrumb;
                $breadcrumb = $is_add ? $labels->add_new_item : $breadcrumb;
                $breadcrumb = $is_all ? $labels->name : $breadcrumb;
                $add_new    = $is_all;
                break;
            case 'chama_donation':
                $breadcrumb         = $is_all ? $labels->name : $breadcrumb;
                $breadcrumb         = $is_edit ? 'Donation - ' . $post_id : $breadcrumb;
                $test_mode_donation = $post_type == 'chama_donation' && $post_id ? get_post_meta($post_id, 'chama_is_test_donation', true) : false;
                break;
            case 'chama_subscription':
                $breadcrumb         = $is_all ? $labels->name : $breadcrumb;
                $breadcrumb         = $is_edit ? 'Subscription - ' . $post_id : $breadcrumb;
                $test_mode_donation = $post_type == 'chama_subscription' && $post_id ? get_post_meta($post_id, 'chama_is_test_subscription', true) : false;
                break;
            case 'chama_fund':
                $breadcrumb         = $is_all ? $labels->name : $breadcrumb;
                $breadcrumb         = $is_edit ? 'Fund - ' . $post_id : $breadcrumb;
                $test_mode_donation = $post_type == 'chama_fund' && $post_id ? get_post_meta($post_id, 'chama_is_test_fund', true) : false;
                break;
            case 'chama_com_order':
                $breadcrumb       = $is_all ? $labels->name : $breadcrumb;
                $breadcrumb       = $is_edit ? 'Commission Order - ' . $post_id : $breadcrumb;
                $$test_mode_order = $post_type == 'chama_com_order' && $post_id ? get_post_meta($post_id, 'chama_is_test_commission_order', true) : false;
                break;
        }
    }
?>
<div id="chama-admin-brading-bar">
    <div id="chama-branding-bar-left-col">
    <div id="chama-logo">
<?php if (! empty(CHAMA_UPGRADE_URL)): ?>
			<a href="<?php echo esc_url(CHAMA_UPGRADE_URL); ?>" target="_blank" rel="noopener noreferrer">
		<?php endif; ?>
		<img
			src="<?php echo esc_url(CHAMA_ASSETS_URL . 'img/chama_logo.svg'); // @phpstan-ignore-line                ?>"
			alt="Chama"
			class="chama-brand"
		/>
        <?php if (! empty(CHAMA_UPGRADE_URL)): ?>
			</a>
		<?php endif; ?>
        </div>
        <span class="chama-breadcrumb-divider">/</span>
        <h1 class="chama-breadcrumb">
        <?php echo esc_html($breadcrumb, 'chama'); ?>
	</h1>
    <?php if ($add_new): ?>
    <a href="<?php echo esc_url(admin_url('post-new.php?post_type=' . $post_type)); ?>" class="chama-page-title-action">Add New</a>
    <?php endif; ?>
<?php if ($test_mode_donation): ?>
        <span  class="chama-test-mode-label label-header" aria-label="This donation was made in test mode." rel="tooltip">Test Donation</span>
        <?php endif; ?>
<?php if ($test_mode_order): ?>
        <span  class="chama-test-mode-label label-header" aria-label="This order was made in test mode." rel="tooltip">Test Order</span>
        <?php endif; ?>
</div>
<div id="chama-branding-bar-right-col">
<span class="chama-version">v<?php echo esc_html(CHAMA_VERSION) ?></span>
</div>
</div>
