<?php

namespace auctionforge\inc\widgets;

use auctionforge\inc\helpers\LotHelper;
use auctionforge\inc\helpers\SettingsHelper;
use auctionforge\inc\helpers\View;
use auctionforge\inc\model\Auction;
use auctionforge\inc\model\Lot;
use auctionforge\inc\model\LotMetaData;
use WP_Widget;

class LotsWidget extends WP_Widget {
    const MODE_DEFAULT = 'default';
    const MODE_ARCHIVE = 'archive';
    const MODE_UPCOMING = 'upcoming';

    private $lot_per_page_default = '5';
    private $mode = 'default';
    private $tmp = 'list';
    private $defaultArgs = array();

    function __construct() {
        parent::__construct(
            'BP_LotsWidget',
            'BP Lots',
            array( 'description' => 'BP Lots' )
        );
    }

    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.lots', array(
            'title' => $title,
            'lots' => $lots,
            'args' => $args
        ));
    }

    public function form( $instance ) {
        $title = '';
        $lot_auctions = '';
        $lot_per_page = $this->lot_per_page_default;

        if ( isset( $instance[ 'title' ] ) ) {
            $title = $instance[ 'title' ];
        }

        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'] : $this->lot_per_page_default;
        return $instance;
    }

    public function lot_list_shortcode( $atts ) {
        /** @var $limit string */
        /** @var $title string */
        /** @var $mode string */
        /** @var $tmp string */
        /** @var $loadmore string */

        $atts = shortcode_atts( array(
            'title' => __('Lot List', 'auctionforge'),
            'limit' => $this->lot_per_page_default,
            'mode' => $this->mode,
            'tmp' => $this->tmp,
            'loadmore' => 1,
            'mobile_cols' => 2
        ), $atts );
        extract($atts);

        $isDefault = ($mode != self::MODE_ARCHIVE && $mode != self::MODE_UPCOMING);
        $this->setDefaultArgs($limit);

        $lots = apply_filters('bp_lot_widgets_filter', null, $atts);

        if (is_null($lots)) {
            if ($isDefault) {
                $lots = $this->getLotListDefault();
            } else {
                $lots = $this->getLotList($mode);
            }

            if ($isDefault && empty($lots)) {
                $lots = Lot::getLots($limit);
            }
        }

        do_action('bp_lot_widgets_action', $lots);

        if ($atts['tmp'] == 'slider') {
            $template = 'widgets.slider';
        } else {
            $template = 'shortcodes.lots';
        }

        $showEstimatedPriceInItemsList = false;
        $showEstimatedPrice = SettingsHelper::isChecked('force_show_estimated_price');
        if ($showEstimatedPrice) {
            $showEstimatedPriceInItemsList = $showEstimatedPrice;
        }
        if (is_tax('auction_number') && is_archive()) {
            $auction = get_queried_object();
            $showEstimatedPriceInItemsList = SettingsHelper::getTermOption($auction->term_id, 'auction_showEstimatedPriceInItemsList', false);
            $displayStartPriceInCatalog = SettingsHelper::getTermOption($auction->term_id, 'auction_displayStartPriceInCatalog');
            $hiddenPrice = LotHelper::isHiddenLots($auctionDay);
        } else {
            $displayStartPriceInCatalog = true;
            $hiddenPrice = false;
        }

        return View::render('templates.' . $template, array(
            'title' => $title,
            'lots' => $lots,
            'limit' => $limit,
            'hiddenPrice' => $hiddenPrice,
            'showEstimatedPriceInItemsList' => $showEstimatedPriceInItemsList,
            'displayStartPriceInCatalog' => $displayStartPriceInCatalog,
            'loadmore' => intval($loadmore),
            'mobile_cols' => intval($atts['mobile_cols'])
        ));
    }

    public function getLotList($mode)
    {
        $args = $this->getDefaultArgs();

        if ($mode == self::MODE_ARCHIVE) {
            $auctions = Auction::getArchiveAuction();
        } else {
            $auctions = Auction::getActiveAuction();
        }

        $slugs = $this->pluckSlug($auctions);
        $args = $this->mergeArgs($args, $slugs);

        $args = apply_filters('bp_lot_list_mode_args_filter', $args, $mode);

        return get_posts($args);
    }

    public function getLotListDefault()
    {
        $args = $this->getDefaultArgs();
        $auctions = Auction::getActiveAuction();

        $slugs = $this->pluckSlug($auctions);
        $args = $this->mergeArgs($args, $slugs);

        $args = apply_filters('bp_lot_list_args_filter', $args);

        return get_posts($args);
    }

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

    public function widget_load() {
        register_widget($this);
    }

    /**
     * @return array
     */
    public function getDefaultArgs()
    {
        return $this->defaultArgs;
    }

    /**
     * @param  array  $defaultArgs
     */
    private function setDefaultArgs($limit)
    {
        $this->defaultArgs = array(
            "numberposts" => $limit,
            "post_status" => ["publish"],
            LotMetaData::BP_QUERY => [
                "bp_field" => [
                    [
                        "value" => 1,
                        "field" => "featured",
                        "compare" => "="
                    ]
                ],
            ],
            "post_type" => "lot",
            "bp_orderby" => Lot::VALUE_NUM_BS,
            "order" => "ASC",
            "suppress_filters" => false
        );
    }

    private function pluckSlug($auctions)
    {
        $slugs = [];
        if (!empty($auctions)) {
            foreach ($auctions as $auction) {
                $slugs[] = $auction->slug;
            }
        }
        return $slugs;
    }

    private function mergeArgs($args, $slugs)
    {
        return array_merge($args, [
            'tax_query' => [
                [
                    'taxonomy' => 'auction_number',
                    'field' => 'slug',
                    'terms' => $slugs
                ]
            ]
        ]);
    }
}
$lots = new LotsWidget();
$lots->init();
