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

class SS_Meta_Boxes {

    public static function init() {
        add_action('add_meta_boxes', [__CLASS__, 'register']);
        add_action('save_post_ss_glossary', [__CLASS__, 'save']);
    }

    /**
     * REGISTER ALL META BOXES
     */
    public static function register() {

        add_meta_box('ss_definition', 'Definition', [__CLASS__, 'definition'], 'ss_glossary', 'normal', 'high');

        add_meta_box('ss_why_matters', 'Why it Matters', [__CLASS__, 'why_matters'], 'ss_glossary');

        add_meta_box('ss_simple_example', 'Simple Example', [__CLASS__, 'simple_example'], 'ss_glossary');

        add_meta_box('ss_related_terms', 'Related Terms', [__CLASS__, 'related_terms'], 'ss_glossary');

        add_meta_box('ss_video_url', 'YouTube Video', [__CLASS__, 'video_url'], 'ss_glossary');

        add_meta_box('ss_video_transcript', 'Video Transcript', [__CLASS__, 'video_transcript'], 'ss_glossary');

        add_meta_box('ss_where_appears', 'Where this term appears', [__CLASS__, 'where_appears'], 'ss_glossary');

        add_meta_box('ss_optional_content', 'Rarely needed. Use only if required', [__CLASS__, 'optional_editor'], 'ss_glossary', 'normal', 'low');
    }

    /**
     * FIELDS
     */

    public static function definition($post) {
        $value = get_post_meta($post->ID, 'ss_definition', true);
        echo '<textarea style="width:100%;height:120px;" name="ss_definition">'.esc_textarea($value).'</textarea>';
    }

    public static function why_matters($post) {

        if (!searchshifter_has_plan('pro')) {
            echo '<textarea disabled style="width:100%;height:120px;background:#eee;"></textarea>';
            ss_locked_field_message('pro');
            return;
        }

        $value = get_post_meta($post->ID, 'ss_why_matters', true);
        echo '<textarea style="width:100%;height:120px;" name="ss_why_matters">'.esc_textarea($value).'</textarea>';
    }

    public static function simple_example($post) {

        if (!searchshifter_has_plan('pro')) {
            echo '<textarea disabled style="width:100%;height:120px;background:#eee;"></textarea>';
            ss_locked_field_message('pro');
            return;
        }

        $value = get_post_meta($post->ID, 'ss_simple_example', true);
        echo '<textarea style="width:100%;height:120px;" name="ss_simple_example">'.esc_textarea($value).'</textarea>';
    }

    public static function related_terms($post) {

        if (!searchshifter_has_plan('pro')) {
            echo '<select disabled style="width:100%;height:120px;background:#eee;"><option>Locked</option></select>';
            ss_locked_field_message('pro');
            return;
        }

        $saved = (array) get_post_meta($post->ID, 'ss_related_terms', true);

        $terms = get_posts([
            'post_type' => 'ss_glossary',
            'numberposts' => -1
        ]);

        echo '<select name="ss_related_terms[]" multiple style="width:100%;height:140px;">';

        foreach ($terms as $t) {
            if ($t->ID == $post->ID) continue;

            echo '<option value="'.esc_attr($t->ID).'" '.selected(in_array($t->ID, $saved), true, false).'>'.esc_html($t->post_title).'</option>';
        }

        echo '</select>';
    }

    public static function video_url($post) {

        if (!searchshifter_has_plan('authority')) {
            echo '<input disabled style="width:100%;background:#eee;" />';
            ss_locked_field_message('authority');
            return;
        }

        $value = get_post_meta($post->ID, 'ss_video_url', true);

        echo '<input type="text" name="ss_video_url" style="width:100%;" value="'.esc_attr($value).'">';
    }

    public static function video_transcript($post) {

        if (!searchshifter_has_plan('authority')) {
            echo '<textarea disabled style="width:100%;height:150px;background:#eee;"></textarea>';
            ss_locked_field_message('authority');
            return;
        }

        $value = get_post_meta($post->ID, 'ss_video_transcript', true);

        echo '<textarea name="ss_video_transcript" style="width:100%;height:180px;">'.esc_textarea($value).'</textarea>';
    }

    public static function where_appears() {

        if (!searchshifter_has_plan('authority')) {
            echo '<p>Requires Authority plan</p>';
            return;
        }

        echo '<p>This will auto-generate pages list.</p>';
    }

    public static function optional_editor($post) {
        wp_editor(
            $post->post_content,
            'ss_optional_editor',
            [
                'textarea_name' => 'post_content',
                'media_buttons' => false,
                'textarea_rows' => 5,
            ]
        );
    }

    /**
     * SAVE DATA
     */
    public static function save($post_id) {

        if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return;
        if (!current_user_can('edit_post', $post_id)) return;

        $fields = [
            'ss_definition',
            'ss_why_matters',
            'ss_simple_example',
            'ss_video_url',
            'ss_video_transcript'
        ];

        foreach ($fields as $field) {
            if (isset($_POST[$field])) {
                update_post_meta($post_id, $field, wp_kses_post($_POST[$field]));
            }
        }

        if (isset($_POST['ss_related_terms'])) {
            update_post_meta($post_id, 'ss_related_terms', array_map('intval', $_POST['ss_related_terms']));
        }
    }
}