<?php

namespace auctionforge\inc\widgets;

use auctionforge\inc\helpers\View;
use auctionforge\inc\model\Lot;
use WP_Widget;

class GalleryWidget extends WP_Widget {
    function __construct() {
        parent::__construct(
            'BP_GalleryWidget',
            'BP Gallery',
            array( 'description' => 'BP Slider' )
        );
    }

    public function widget( $args, $instance ) {
        $title = apply_filters( 'widget_title', $instance['title'] );
        $lot_per_page = $instance['lot_per_page'];
        
        $lots = Lot::getLots($lot_per_page);

        echo View::render('templates.widgets.slider', array(
            'title' => $title,
            'lots' => $lots,
            'args' => $args
        ));
    }

    public function form( $instance ) {
        if ( isset( $instance[ 'title' ] ) ) {
            $title = $instance[ 'title' ];
        }
        if ( isset( $instance[ 'lot_auctions' ] ) ) {
            $lot_auctions = $instance[ 'lot_auctions' ];
        }
        if ( isset( $instance[ 'lot_per_page' ] ) ) {
            $lot_per_page = $instance[ 'lot_per_page' ];
        }
        echo View::render('admin.view.widgets.lots', array(
            'obj' => $this,
            'title' => $title,
            'lot_auctions' => $lot_auctions,
            'lot_per_page' => $lot_per_page
        ));
    }

    public function update($new_instance, $old_instance)
    {
        $instance = array();
        $instance['title'] = (!empty($new_instance['title'])) ? strip_tags($new_instance['title']) : '';
        $instance['lot_auctions'] = (!empty($new_instance['lot_auctions'])) ? strip_tags($new_instance['lot_auctions']) : '';
        $instance['lot_per_page'] = (is_numeric($new_instance['lot_per_page'])) ? $new_instance['lot_per_page'] : '5';
        return $instance;
    }

    public function init() {
        add_action('widgets_init', array($this, 'widget_load'));
    }

    public function widget_load() {
        register_widget($this);
    }
}
$gallery = new GalleryWidget();
$gallery->init();
