<?php

class Stylekit extends Metabox {
    /**
     * Unique of metabox.
     *
     * @var string
     */
    public $unique_id = 'stylekit_box_id';

    /**
     * Unique of metabox.
     *
     * @var string
     */
    public $title = 'Style Kit';

    /**
     * Context of metabox.
     *
     * @var string
     */
    public $context = 'normal';

    /**
     * Method called via autoloader for action.
     *
     * @return void
     */
    public function action() {
        $flex_obj = flo_get_option_buffer('flex_active_stylekits_names');
        if (!empty($flex_obj)) {
            //add and save metabox
            add_action('wp_ajax_save_stylekit_with_ajax', [$this, 'save_stylekit_with_ajax']);
            add_action('add_meta_boxes', [$this, 'add'], 5);
            add_action('save_post', [$this, 'save']);
        }
    }

    /**
     * Priority of metabox.
     *
     * @var string
     */
    public $priority = 'high';

    public function save_stylekit_with_ajax() {

        Flo_Flex_Builder_Admin::check_capabilities('publish_pages', $check_nonce = true);

        $post_id = $_POST['post_id'];
        $stylekit_name = $_POST['stylekit'];

        // ajax will send null if checkbox is disabled, in these cases we request the global stylekit styles
        if (!$stylekit_name) {
            $stylekit_name = get_option('flex_active_stylekit_name');
            Flo_Flex_Builder_Stylekits::update_stylekit_pages([
                $post_id => [
                    'enabled' => '0',
                    'stylekit_value' => null
                ]
            ]);
        } else {
            Flo_Flex_Builder_Stylekits::update_stylekit_pages([
                $post_id => [
                    'enabled' => '1',
                    'stylekit_value' => $stylekit_name
                ]
            ]);
        }

        $all_stylekits = get_option('flex_all_stylekits_obj');
        $stylekit = $all_stylekits[$stylekit_name];
        $generalInfo = $stylekit['generalInfo'];

        $gFonts = null;
        if (isset($generalInfo['gFontsUrl']) && strlen($generalInfo['gFontsUrl'])) {
            $gFonts = $generalInfo['gFontsUrl'];
        }

        $stylekitData = Flex_Builder_Data::getFlexStylekitStyles($post_id);
        $flex_data = [];
        if ($stylekitData) {
            $colorsData = $stylekitData['colors'];
            $flex_data['flothemes_font_map'] = $stylekitData['fontData'];
            $flex_data['flex_stylekit_color_list'] = $colorsData['values'];
            $flex_data['flex_stylekit_color_labels'] = $colorsData['labels'];
        }

        echo json_encode([
            "gFonts" => $gFonts,
            "name" => $stylekit_name,
            "flex_data" => $flex_data,
        ]);
        wp_die();
    }

    public function save($post_id) {
        $stylekit_checker = isset($_POST['stylekit-metabox__checker']);

        if ($stylekit_checker && !$post_id === wp_is_post_revision($post_id)) {
            $checkbox_on = isset($_POST['stylekit-metabox__custom--enable']) ? $_POST['stylekit-metabox__custom--enable'] : false;
            $stylekit_name = isset($_POST['stylekit-metabox__custom']) ? $_POST['stylekit-metabox__custom'] : false;

            if ($checkbox_on && $stylekit_name) {
                Flo_Flex_Builder_Stylekits::update_stylekit_pages([
                    $post_id => [
                        'enabled' => $checkbox_on,
                        'stylekit_value' => $stylekit_name
                    ]
                ]);
            }

            if (isset($_POST['stylekit-metabox__preload'])) {
                update_post_meta($post_id, 'stylekit-metabox__preload', $_POST['stylekit-metabox__preload']);
            }
        }
    }

    public function html($post) {
        //vars
        $post_id = get_the_ID();
        $activation_key = get_option('flotheme_theme_key');
        $active_id = get_option('flex_active_stylekit_name');
        $stylekits_pages = get_option('flex_stylekits_pages');
        $active_stylekits_names = flo_get_option_buffer('flex_active_stylekits_names');

        $uploads_dir = wp_get_upload_dir();
        $uploads_dir_url = isset($uploads_dir['baseurl']) ? $uploads_dir['baseurl'] . '/flex/stylekits' : false;

        $stylekit_enabled =  0;
        $stylekit_value =  '';

        $all_stylekit_names = flo_get_option_buffer('flex_active_stylekits_names');
        $active_name = '';
        if (isset($all_stylekit_names[$active_id])) {
            $active_st = $all_stylekit_names[$active_id];
            $active_name = $active_st['name'];
        }

        if (!empty($stylekits_pages[$post_id]['enabled']) && !empty($stylekits_pages[$post_id]['stylekit_value'])) {
            $stylekit_enabled =  $stylekits_pages[$post_id]['enabled'];
            $stylekit_value =  $stylekits_pages[$post_id]['stylekit_value'];
        }

        $preload = get_post_meta($post_id, 'stylekit-metabox__preload', true);
?>
        <?php if ($activation_key != null) : ?>
            <div class="stylekit-metabox">
                <a class="stylekit-metabox__preview" href=<?php echo admin_url('admin.php?page=flex-block-settings'); ?> target="_blank">
                    <?php if (strpos($active_id, 'custom') !== false) : ?>
                        <div class="stylekit-metabox__preview--gray-thumb"></div>
                    <?php else : ?>
                        <img class="stylekit-metabox__preview--image" src=<?php echo S3_FLEX_BUCKET_ENDPOINT . "/stylekits/$active_id/thumbnail.jpg" ?> alt="stylekit image">
                    <?php endif; ?>
                    <div class="stylekit-metabox__preview--kit">
                        <h5>GLOBAL STYLE KIT:</h5>
                        <h1><?php echo $active_name; ?></h1>
                    </div>
                </a>
                <div class="stylekit-metabox__custom">
                    <div class="stylekit-metabox__custom--wrapper">
                        <label for="stylekit-metabox__custom--enable">Enable Other Style Kit</label>
                        <div class="acf-true-false">
                            <input type="hidden" id="stylekit-metabox__uploads-url" name="stylekit-metabox__uploads-url" value="<?php echo $uploads_dir_url; ?>">
                            <input type="hidden" id="stylekit-metabox__checker" name="stylekit-metabox__checker">
                            <input type="checkbox" id="stylekit-metabox__custom--enable" name="stylekit-metabox__custom--enable" value="<?php echo $stylekit_enabled; ?>" <?php echo $stylekit_enabled ? 'checked' : '' ?>>
                        </div>
                    </div>
                    <div class="stylekit-metabox__custom--wrapper acf-field">
                        <select name="stylekit-metabox__custom" id="stylekit-metabox__custom" <?php echo $stylekit_enabled ? '' : 'disabled' ?>>
                            <?php foreach ($active_stylekits_names as $active_stylekits_name) :
                                //vars
                                $stylekit_id = $active_stylekits_name['id'];
                                $stylekit_name = $active_stylekits_name['name'];
                            ?>
                                <?php if ($stylekit_id != $active_id && $stylekit_id !== NULL && $stylekit_name !== NULL) : ?>
                                    <option value=<?php echo $stylekit_id ?> <?php selected($stylekit_value, $stylekit_id) ?>>Style Kit: <?php echo $stylekit_name ?></option>
                                <?php endif; ?>
                            <?php endforeach; ?>
                        </select>
                    </div>
                </div>
            </div>
        <?php else : ?>
            <div class="stylekit-metabox--no-key">
                <div class="stylekit-metabox--no-key__item">
                    <h1>We can't find your purchase code</h1>
                    <p>Please add purchase code, or buy product</p>
                </div>
            </div>
        <?php endif; ?>
<?php
    }
}
