<?php
namespace CSB;

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

if (!class_exists('bchCodeAdmin')) {
    class bchCodeAdmin {
        public function __construct()
        {
            add_action('init', [$this, 'bch_codeBlock_post_type']);
            add_action('admin_menu', [$this, 'bch_sub_Menu']);
            add_filter('manage_bch_block_posts_columns', [$this, 'bch_setCustomColumn_edit']);
            add_action('manage_bch_block_posts_custom_column', [$this, 'bch_manageCustomColumn'], 10, 2);
            add_action('wp_ajax_csb_disabled_blocks', [$this, 'handleDisabledBlocks']);
        }
        public function handleDisabledBlocks()
        {
            $nonce = isset($_POST['_wpnonce']) ? sanitize_text_field(wp_unslash($_POST['_wpnonce'])) : null;
            if (!wp_verify_nonce($nonce, 'csb_disabled_blocks') || !current_user_can('manage_options')) {
                wp_send_json_error('Invalid Request');
            }

            $data = isset($_POST['data']) ? json_decode(sanitize_text_field(wp_unslash($_POST['data'])), true) : null;

            if (is_array($data)) {
                $data = array_values(array_map('sanitize_text_field', $data));
                update_option('csbDisabledBlocks', $data);
                wp_send_json_success($data);
            }

            wp_send_json_success(get_option('csbDisabledBlocks', []));
        }
        public function bch_codeBlock_post_type()
        {
            $template = [['bch/code-highlight']];
            if (CSBCH_HAS_PRO && csbIsPremium()) {
                $template = [['bch/code-parent']];
            }

            register_post_type(
                'bch_block',
                array(
                    'label' => 'Code Snippets',
                    'labels' => [
                        'add_new' => 'Add New', // Global page
                        'add_new_item' => 'Add New Code Snippets', // When click on new post
                        'edit_item' => 'Edit Code Snippets',
                        'not_found' => 'There is no Code Snippets please add one',
                        'item_published' => 'Code Snippets Published',
                        'item_updated' => 'Code Snippets Updated'
                    ],
                    'public' => false,
                    'show_ui' => true,
                    'show_in_rest' => true,
                    'menu_icon' => 'dashicons-media-code',
                    'template' => $template,
                    'template_lock' => 'all',
                )
            );
        }
        public function bch_sub_Menu()
        {
            add_submenu_page(
                'edit.php?post_type=bch_block',
                'Demo & Help',
                'Demo & Help',
                'manage_options',
                'bch_Dashboard',
                [$this, 'bch_Dashboard_page']
            );
        }
        public function bch_Dashboard_page()
        {
            ?>
            <div id='bchDashboard' data-info='<?php echo esc_attr(wp_json_encode([
                'version' => CSBCH_PLUGIN_VERSION,
                'isPremium' => csbIsPremium(),
                'hasPro' => CSBCH_HAS_PRO,
                'licenseActiveNonce' => wp_create_nonce('csbLicenseActive'),
                'adminUrl' => admin_url(),
                'disabledBlocksNonce' => wp_create_nonce('csb_disabled_blocks'),
                'csbDisabledBlocks' => get_option('csbDisabledBlocks', [])
            ])); ?>'></div>
            <?php
        }

        public function bch_setCustomColumn_edit($column)
        {
            unset($column['date']);
            $column['shortcode'] = 'ShortCode';
            $column['date'] = 'Date';
            return $column;
        }
        public function bch_manageCustomColumn($column_name, $post_id)
        {
            if ($column_name == 'shortcode') {
                echo '<div class="bPlAdminShortcode" id="bPlAdminShortcode-' . esc_attr($post_id) . '">
						<input value="[csb_code id=' . esc_attr($post_id) . ']" onclick="copyBPlAdminShortcode(\'' . esc_attr($post_id) . '\')" readonly>
						<span class="tooltip">Copy To Clipboard</span>
					  </div>';
            }
        }
    }

    new bchCodeAdmin();

}