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

class Sifency_Admin_Page_Widgets_List {

    public function __construct() {
        add_action( 'admin_init', [ $this, 'register_settings' ] );
    }

    public function register_settings() {
        register_setting(
            'sifency_addons_options',
            'sifency_addons_settings',
            [ $this, 'sanitize_settings' ]
        );
    }

    public function sanitize_settings( $input ) {
        $sanitized_input = [];

        if ( isset( $input['widgets'] ) && is_array( $input['widgets'] ) ) {
            foreach ( $input['widgets'] as $widget_slug => $status ) {
                $sanitized_slug            = sanitize_key( $widget_slug );
                $sanitized_status         = ( 'on' === $status ) ? 'on' : 'off';
                $sanitized_input['widgets'][ $sanitized_slug ] = $sanitized_status;
            }
        }

        return $sanitized_input;
    }

    public function render_admin_page() {

        $settings = get_option( 'sifency_addons_settings', [] );

        // phpcs:ignore WordPress.Security.NonceVerification.Recommended
        $active_tab = isset( $_GET['tab'] ) ? sanitize_key( $_GET['tab'] ) : 'widgets';

        $all_widgets = \Sifency_Addons\Includes\Admin\Sifency_Addons_Admin_Widgets::get_all_sifency_widgets_for_settings();

        // Split widgets
        $free_widgets    = [];
        $premium_widgets = [];

        foreach ( $all_widgets as $category => $widgets ) {
            foreach ( $widgets as $slug => $widget ) {
                if ( ! empty( $widget['premium'] ) ) {
                    $premium_widgets[ $category ][ $slug ] = $widget;
                } else {
                    $free_widgets[ $category ][ $slug ] = $widget;
                }
            }
        }
        ?>

        <div class="sifency-admin-wrap sifency-dashboard-layout">

            <nav class="sifency-dashboard-nav">

                <a href="?page=sifency-addons-widgets"
                   class="sifency-nav-item sifency-admin-btn <?php echo ( $active_tab === 'widgets' ) ? 'active' : ''; ?>">
                    <span class="dashicons dashicons-tagcloud"></span>
                    <?php esc_html_e( 'All Widgets', 'sifency-addons' ); ?>
                </a>

                <a href="?page=sifency-addons-widgets&tab=free"
                   class="sifency-nav-item sifency-admin-btn <?php echo ( $active_tab === 'free' ) ? 'active' : ''; ?>">
                    <span class="dashicons dashicons-star-empty"></span>
                    <?php esc_html_e( 'Free', 'sifency-addons' ); ?>
                </a>

                <a href="?page=sifency-addons-widgets&tab=premium"
                   class="sifency-nav-item sifency-admin-btn <?php echo ( $active_tab === 'premium' ) ? 'active' : ''; ?>">
                    <span class="dashicons dashicons-awards"></span>
                    <?php esc_html_e( 'Premium', 'sifency-addons' ); ?>
                </a>

            </nav>

            <div class="sifency-admin-content">
                <form method="post" action="options.php">
                    <?php settings_fields( 'sifency_addons_options' ); ?>

                    <?php
                    if ( $active_tab === 'widgets' ) {
                        $this->render_widgets_tab( $settings, $all_widgets );
                    } elseif ( $active_tab === 'free' ) {
                        $this->render_widgets_tab( $settings, $free_widgets );
                    } elseif ( $active_tab === 'premium' ) {
                        $this->render_widgets_tab( $settings, $premium_widgets );
                    }
                    ?>

                    <?php 
                        submit_button( esc_html__( 'Save Changes', 'sifency-addons' ) );
                        echo '<div class="sifency-floating-add-btn text">';
                        submit_button( esc_html__( 'Save Changes', 'sifency-addons' ) );
                        echo '</div>';
                    ?>

                </form>
            </div>
        </div>
        <?php
    }

    /**
     * Render widgets list
     */
    private function render_widgets_tab( $settings, $widgets_data ) {
        $total_widgets_count = 0;
        foreach ( $widgets_data as $widgets ) {
            if ( is_array( $widgets ) ) {
                $total_widgets_count += count( $widgets );
            }
        }
        ?>

        <div class="sifency-addons-widgets-settings">
                
            <div class="sifency-tab-header">
                <div class="sifency-category-nav-tabs">
                    <button type="button" class="sifency-tab-trigger active" data-target="all">
                        <span class="sifency-tab-text"><?php esc_html_e( 'All', 'sifency-addons' ); ?></span>
                        <span class="sifency-tab-count"><?php echo esc_html( $total_widgets_count ); ?></span>
                    </button>

                    <?php 
                    foreach ( $widgets_data as $category_title => $widgets ) : 
                        $category_id = 'sifency-cat-' . sanitize_title( $category_title );
                        $widget_count = count( $widgets );
                    ?>
                        <button type="button" class="sifency-tab-trigger" data-target="<?php echo esc_attr( $category_id ); ?>">
                            <span class="sifency-tab-text"><?php echo esc_html( $category_title ); ?></span>
                            <span class="sifency-tab-count"><?php echo esc_html( $widget_count ); ?></span>
                        </button>
                    <?php 
                    endforeach; 
                ?>
                </div>
                <div class="sifency-master-controls">
                    <div class="sifency-control-group">
                        <button type="button" id="sifency-toggle-all" class="button sifency-toggle-all-btn sifency-admin-btn" data-action="enable"><?php esc_html_e( 'Enable All', 'sifency-addons' ); ?></button>
                    </div>
                    <div class="sifency-control-group">
                        <button type="button" id="sifency-toggle-none" class="button sifency-toggle-all-btn sifency-admin-btn" data-action="disable"><?php esc_html_e( 'Disable All', 'sifency-addons' ); ?></button>
                    </div>
                </div>
            </div>

            

           

            <div class="sifency-tab-content-wrapper">
                <?php 
                foreach ( $widgets_data as $category_title => $widgets ) : 
                    $category_id = 'sifency-cat-' . sanitize_title( $category_title );
                ?>

                    <div id="<?php echo esc_attr( $category_id ); ?>" class="sifency-widget-category-block">

                        <div class="sifency-section-header">
                            <h2 class="sifency-widget-category-title">
                                <?php echo esc_html( $category_title ); ?>
                            </h2>
                            <span class="sifency-section-count"><?php echo count($widgets); ?></span>
                        </div>

                        <div class="sifency-addons-widgets-grid">

                            <?php foreach ( $widgets as $slug => $widget ) :
                                $is_enabled = ! empty( $settings['widgets'][ $slug ] ) && $settings['widgets'][ $slug ] === 'on';
                                $is_premium = ! empty( $widget['premium'] );
                            ?>

                                <div class="sifency-widget-card <?php echo $is_premium ? 'pro-widget' : ''; ?>">

                                    <div class="sifency-widget-card-header">
                                        <i class="<?php echo esc_attr( $widget['icon'] ); ?>"></i>
                                        <h3><?php echo esc_html( $widget['title'] ); ?></h3>

                                        <?php if ( $is_premium && ! Sifency_Addons_Theme_Builder_Master::sifency_addons_is_legal() ) : ?>
                                            <span class="sifency-pro-badge">Pro</span>
                                        <?php endif; ?>
                                    </div>

                                    <div class="sifency-widget-card-footer">
                                        <label class="sifency-switch">
                                            <input type="hidden" name="sifency_addons_settings[widgets][<?php echo esc_attr( $slug ); ?>]" value="off">
                                            <input
                                                type="checkbox"
                                                class="sifency-widget-toggle-checkbox"
                                                name="sifency_addons_settings[widgets][<?php echo esc_attr( $slug ); ?>]"
                                                value="on"
                                                <?php checked( $is_enabled, true ); ?>
                                                <?php echo ( $is_premium && ! Sifency_Addons_Theme_Builder_Master::sifency_addons_is_legal() ) ? 'disabled' : ''; ?>
                                            >
                                            <span class="slider round"></span>
                                        </label>
                                    </div>

                                </div>

                            <?php endforeach; ?>

                        </div>
                    </div>

                <?php 
                endforeach; 
                ?>
            </div>

        </div>
        <?php
    }
}

new Sifency_Admin_Page_Widgets_List();
