<?php
// Exit if accessed directly.
if (!defined('ABSPATH')) {
    exit;
}

if (!class_exists('SEPAdmin')) {
    class SEPAdmin
    {
        public function __construct()
        {

            add_action('init', [$this, 'sepRegisterPostType']);
            add_action('admin_enqueue_scripts', [$this, 'enqueueDashboardAssets']);
            add_action('admin_menu', [$this, 'sepAdminSubmenu']);
            add_filter('manage_section-separator_posts_columns', [$this, 'sep_setCustomColumn_edit']);
            add_action('manage_section-separator_posts_custom_column', [$this, 'sep_manageCustomColumn'], 10, 2);
        }

        public function  sepRegisterPostType()
        {
            $icon = "<svg stroke='#fff' color='#fff' fill='#fff' stroke-width='0' viewBox='0 0 24 24' height='1em' width='1em' xmlns='http://www.w3.org/2000/svg'><path d='M12 8.9757L16.2426 4.73306L14.8284 3.31885L12 6.14727L9.17157 3.31885L7.75736 4.73306L12 8.9757Z' fill='currentColor'></path><path d='M5 11.9998C5 11.4475 5.44772 10.9997 6 10.9997H18C18.5523 10.9997 19 11.4475 19 11.9998C19 12.552 18.5523 12.9998 18 12.9998H6C5.44772 12.9998 5 12.552 5 11.9998Z' ></path><path d='M12 15.0242L7.75736 19.2668L9.17157 20.681L12 17.8526L14.8284 20.681L16.2426 19.2668L12 15.0242Z' ></path></svg>";


            register_post_type(
                'section-separator',
                [
                    'label'               => 'Section Separator',
                    'labels'              => [
                        'add_new' => 'Add New Shortcode',
                        'add_new_item' => 'Add New Shortcode',
                        'edit_item' => 'Edit Shortcode',
                        'not_found' => 'No Shortcode found',
                    ],
                    'supports' => ['title', 'editor', 'revisions'],
                    'show_in_rest' => true,
                    'public' => true,
                    'publicly_queryable' => false,
                    'menu_icon' => 'data:image/svg+xml;base64,' . base64_encode($icon),
                    'item_published' => 'Section Separator Published',
                    'item_updated' => 'Section Separator Updated',
                    'template' => [['sep/separator']],
                    'template_lock' => 'all',

                ]
            );
        }


        public function sepAdminSubmenu()
        {
            $parent_slug = 'tools.php';

            add_submenu_page(
                'edit.php?post_type=section-separator',
                'Demo and Help',
                'Demo & Help',
                'manage_options',
                'separator_demo_page',
                [$this, 'sep_render_demo_page']
            );
        }

        // pass dashboard data through inline script

        public function enqueueDashboardAssets()
        {
            $data = [
                'version' => SEP_VERSION,
                'isPremium' => sepIsPremium(),
                'hasPro' => SEP_HAS_PRO,
            ];
            wp_add_inline_script(
                'sep-admin-dashboard',
                'const dashboardData = ' . wp_json_encode($data) . ';',
                'before'
            );
        }
        // render dashboard and  pass its data through data attributes

        public function sep_render_demo_page()
        {

?>
            <div id='sep-admin-dashboard'
                data-info='<?php echo esc_attr(wp_json_encode([
                                'version' => SEP_VERSION,
                                'isPremium' => sepIsPremium(),
                                'hasPro' => SEP_HAS_PRO,
                                'licenseActiveNonce' => wp_create_nonce('sepSeparatorLicenseActivation'),

                            ])); ?>'></div>
<?php
        }
        // set custom columns for edit page

        public function sep_setCustomColumn_edit($column)
        {
            unset($column['date']);
            $column['shortcode'] = 'ShortCode';
            $column['date'] = 'Date';
            $column['publisher'] = 'Publisher';
            return $column;
        }

        public function sep_manageCustomColumn($column_name, $post_id)
        {

            if ($column_name == 'shortcode') {
                echo '<div class="bPlAdminShortcode" id="bPlAdminShortcode-' . esc_attr($post_id) . '">
						<input value="[section-separator id=' . esc_attr($post_id) . ']" onclick="copyBPlAdminShortcode(\'' . esc_attr($post_id) . '\')" readonly>
						<span class="tooltip">Copy To Clipboard</span>
					  </div>';
            }
            if ($column_name == 'publisher') {
                echo 'Section Separator';
            }
        }
    }

    new SEPAdmin();
}
