<?php

namespace fluXtore\Core\Frontend;

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

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

        private function __construct() {}

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

            self::$instance = new self();

            add_shortcode( 'fluxtore-checkout-block', [ self::$instance, 'render_checkout_block' ] );
            add_shortcode( 'render-yes-block', [ self::$instance, 'render_yes_block' ] );
        }

        public function render_checkout_block( $attributes ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter
            ob_start();
            ?>
            <main class="wp-block-group has-global-padding is-layout-constrained wp-block-group-is-layout-constrained" id="wp--skip-link--target">
                <div class="entry-content alignwide wp-block-post-content is-layout-flow wp-block-post-content-is-layout-flow">
                    <?php echo do_shortcode( '[woocommerce_checkout]' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
                </div>
            </main>
            <style>
                .woocommerce-page table.shop_table { width:100% !important; }
            </style>
            <?php
            return ob_get_clean();
        }

        public function render_yes_block( $atts ) {
            $atts = shortcode_atts(
                [
                    'text'             => 'Click Me',
                    'background_color' => '#007bff',
                    'text_color'       => '#fff',
                    'url'              => '#',
                ],
                $atts
            );

            $style  = 'background-color: ' . esc_attr( $atts['background_color'] ) . '; color: ' . esc_attr( $atts['text_color'] ) . ';';
            $output = '<a href="' . esc_url( $atts['url'] ) . '" class="my-button" style="' . $style . '">' . esc_html( $atts['text'] ) . '</a>';

            return $output;
        }
    }
}

fluXtore_Shortcodes::init();
