<?php

namespace fluXtore\Core\Admin;

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

if ( ! class_exists( 'fluXtore_Admin_Bar' ) ) {
    class fluXtore_Admin_Bar {
        /**
         * @var self|null
         */
        private static $instance = null;

        private function __construct() {}

        public static function init() {
            if ( null !== self::$instance ) {
                return;
            }

            self::$instance = new self();

            add_action( 'admin_bar_menu', [ self::$instance, 'add_back_button' ], 100 );
            add_action( 'elementor/editor/after_enqueue_scripts', [ self::$instance, 'render_elementor_back_button' ] );
            add_action( 'admin_bar_menu', [ self::$instance, 'add_new_funnel_button' ], 100 );
            add_action( 'admin_footer', [ self::$instance, 'render_editor_modal' ] );
        }

        public function add_back_button( $wp_admin_bar ) {
            global $post;

            if ( ! $post || 'fluxtore_step' !== get_post_type( $post ) ) {
                return;
            }

            $funnel_id = get_post_meta( $post->ID, '__fluxtore__flow', true );
            $back_url  = admin_url( 'admin.php?page=fluxtore#/' . $funnel_id . '/steps' );

            $wp_admin_bar->add_node(
                [
                    'id'     => 'back_to_step',
                    'title'  => 'Back to Funnel',
                    'href'   => $back_url,
                    'parent' => 'root-default',
                ]
            );
        }

        public function render_elementor_back_button() {
            global $post;

            if ( ! $post || 'fluxtore_step' !== get_post_type( $post ) ) {
                return;
            }

            $funnel_id = get_post_meta( $post->ID, '__fluxtore__flow', true );
            $back_url  = admin_url( 'admin.php?page=fluxtore#/' . $funnel_id . '/steps' );
            ?>
            <script>
                document.addEventListener('DOMContentLoaded', function () {
                    function addCustomBackButton() {
                        var container = document.querySelector('.MuiGrid-root.MuiGrid-container, .css-0');

                        if (container && !document.querySelector('.back-step-button')) {
                            var customButtonDiv = document.createElement('div');
                            customButtonDiv.className = 'MuiBox-root back-step-button';
                            customButtonDiv.innerHTML = '<a href="<?php echo esc_url( $back_url ); ?>" class="back-step-link"><i class="eicon-eye"></i> Back to Funnel</a>';
                            customButtonDiv.style.marginLeft = '10px';
                            container.appendChild(customButtonDiv);
                        }
                    }

                    var observer = new MutationObserver(function () {
                        if (document.querySelector('.MuiGrid-root.MuiGrid-container, .css-0')) {
                            addCustomBackButton();
                        }
                    });

                    observer.observe(document.body, { childList: true, subtree: true });
                });
            </script>
            <style>
                .back-step-button a {
                    color: currentColor;
                    display: flex;
                    align-items: center;
                    text-decoration: none;
                    margin-top: 16px;
                }

                .back-step-button a i {
                    margin-right: 5px;
                }
            </style>
            <?php
        }

        public function add_new_funnel_button( $wp_admin_bar ) {
            $args = [
                'parent' => 'new-content',
                'id'     => 'new-fluXtore-item1',
                'title'  => 'Add New Funnel',
                'href'   => admin_url( 'admin.php?page=fluxtore#/funnels' ),
            ];

            if ( is_admin() && isset( $_GET['page'] ) && 'fluxtore' === $_GET['page'] ) {
                $args['id']   = 'new-fluXtore-item';
                unset( $args['href'] );
            }

            $wp_admin_bar->add_node( $args );
        }

        public function render_editor_modal() {
            if ( ! is_admin() ) {
                return;
            }

            $ajax_url        = admin_url( 'admin-ajax.php' );
            $new_funnel_url  = admin_url( 'admin.php?page=fluxtore#/newfunnel' );
            $bump_nonce      = wp_create_nonce( 'fluxtore_bump_nonce' );
            $is_fluxtore_app = isset( $_GET['page'] ) && 'fluxtore' === $_GET['page'];

            include FLUXTORE_PLUGIN_DIR . 'core/admin/views/editor-modal.php';
        }
    }
}

fluXtore_Admin_Bar::init();
