<?php
class Retheme_post_widget extends WP_Widget {

    public function __construct() {
        parent::__construct('retheme_post', __("Theme - Custom Posts", 'jelajah-text'), [
            'description' => __('A custom widget to display posts'),
            'customize_selective_refresh' => true,
        ]);
    }

    public function widget($args, $instance) {
        echo $args['before_widget'];

        $title = !empty($instance['title']) ? $instance['title'] : __('Latest Post', 'jelajah-text');

        if (!empty($title)) {
            echo $args['before_title'] . $title . $args['after_title'];
        }

        // Query parameters
        $orderby = !empty($instance['orderby']) ? $instance['orderby'] : 'date';
        $posts_per_page = !empty($instance['posts_per_page']) ? absint($instance['posts_per_page']) : 5;

        $query_args = array(
            'posts_per_page' => $posts_per_page,
        );

        switch ($orderby) {
            case 'popular':
                $query_args['meta_key'] = 'wp_post_views_count';
                $query_args['orderby'] = 'meta_value_num';
                $query_args['order'] = 'DESC';
                break;

            case 'most_commented':
                $query_args['orderby'] = 'comment_count';
                $query_args['order'] = 'DESC';
                $query_args['comment_count'] = array('value' => 1, 'compare' => '>=');
                break;

            case 'recent_comments':
                $comments = get_comments(array(
                    'number' => $posts_per_page,
                    'status' => 'approve',
                ));
                $post_ids = array();

                foreach ($comments as $comment) {
                    if (!in_array($comment->comment_post_ID, $post_ids)) {
                        $post_ids[] = $comment->comment_post_ID;
                    }
                }

                $query_args['post__in'] = $post_ids;
                $query_args['orderby'] = 'post__in';
                break;

            case 'latest':
            default:
                $query_args['orderby'] = 'date';
                $query_args['order'] = 'DESC';
                break;
        }

        $query_args['option_orderby'] = $orderby;

        get_template_part('core/widget/post/widget-post-view', '', $query_args);

        echo $args['after_widget'];
    }

    public function form($instance) {
        $title = isset($instance['title']) ? $instance['title'] : __('Latest Post', 'jelajah-text');
        $orderby = isset($instance['orderby']) ? $instance['orderby'] : 'latest';
        $posts_per_page = isset($instance['posts_per_page']) ? $instance['posts_per_page'] : 5;
?>

        <p>
            <label for="<?php echo esc_attr($this->get_field_id('title')); ?>"><?php esc_attr_e('Judul:', 'jelajah-text'); ?></label>
            <input class="widefat" id="<?php echo esc_attr($this->get_field_id('title')); ?>" name="<?php echo esc_attr($this->get_field_name('title')); ?>" type="text" value="<?php echo esc_attr($title); ?>">
        </p>
        <p>
            <label for="<?php echo esc_attr($this->get_field_id('orderby')); ?>"><?php esc_attr_e('Order by:', 'jelajah-text'); ?></label>
            <select class="widefat" id="<?php echo esc_attr($this->get_field_id('orderby')); ?>" name="<?php echo esc_attr($this->get_field_name('orderby')); ?>">
                <option value="date" <?php selected($orderby, 'latest'); ?>><?php esc_html_e('Latest Date', 'jelajah-text'); ?></option>
                <option value="most_commented" <?php selected($orderby, 'most_commented'); ?>><?php esc_html_e('Most Commented', 'jelajah-text'); ?></option>
                <option value="recent_comments" <?php selected($orderby, 'recent_comments'); ?>><?php esc_html_e('Recent Comments', 'jelajah-text'); ?></option>
            </select>
        </p>
        <p>
            <label for="<?php echo esc_attr($this->get_field_id('posts_per_page')); ?>"><?php esc_attr_e('Posts Per Page:', 'jelajah-text'); ?></label>
            <input class="widefat" id="<?php echo esc_attr($this->get_field_id('posts_per_page')); ?>" name="<?php echo esc_attr($this->get_field_name('posts_per_page')); ?>" type="number" value="<?php echo esc_attr($posts_per_page); ?>" min="1">
        </p>
<?php
    }

    public function update($new_instance, $old_instance) {
        $instance = array();
        $instance['title'] = (!empty($new_instance['title'])) ? sanitize_text_field($new_instance['title']) : '';
        $instance['orderby'] = (!empty($new_instance['orderby'])) ? sanitize_text_field($new_instance['orderby']) : 'date';
        $instance['posts_per_page'] = (!empty($new_instance['posts_per_page'])) ? absint($new_instance['posts_per_page']) : 5;

        return $instance;
    }
}
