<?php

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

/**
 * Class Realia_Widget_Properties
 *
 * @class Realia_Widget_Properties
 * @package Realia/Classes/Widgets
 * @author Pragmatic Mates
 */
class Realia_Widget_Properties extends WP_Widget {
    /**
     * Initialize widget
     *
     * @access public
     * @return void
     */
    function Realia_Widget_Properties() {
        parent::__construct(
            'properties_widget',
            __( 'Properties', 'realia' ),
            array(
                'description' => __( 'Displays properties in multiple ways.', 'realia' ),
            )
        );
    }

    /**
     * Frontend
     *
     * @access public
     * @param array $args
     * @param array $instance
     * @return void
     */
    function widget( $args, $instance ) {
        $query = array(
            'post_type'         => 'property',
            'posts_per_page'    => ! empty( $instance['count'] ) ? $instance['count'] : 3,
            'tax_query'         => array(
                'relation'      => 'AND',
            ),
        );

        if ( ! empty( $instance['attribute'] ) ) {
            if ( $instance['attribute'] == 'featured' ) {
                $query['meta_query'][] = array(
                    'key'       => REALIA_PROPERTY_PREFIX . 'featured',
                    'value'     => 'on',
                    'compare'   => '==',
                );
            } elseif ( $instance['attribute'] == 'reduced' ) {
                $query['meta_query'][] = array(
                    'key'       => REALIA_PROPERTY_PREFIX . 'reduced',
                    'value'     => 'on',
                    'compare'   => '==',
                );
            }
        }

        // Contracts / Property Types / Locations
        $property_taxonomies = array( 'contracts', 'locations', 'property_types' );

        foreach( $property_taxonomies as $property_taxonomy ) {
            $$property_taxonomy = array();

            if ( ! empty( $instance[ $property_taxonomy ] ) && is_array( $instance[ $property_taxonomy ] ) ) {
                $$property_taxonomy = $instance[ $property_taxonomy ];
            }

            if ( ! empty( $$property_taxonomy ) ) {
                $query['tax_query'][] = array(
                    'taxonomy'  => $property_taxonomy,
                    'field'     => 'id',
                    'terms'     => $$property_taxonomy,
                );
            }
        }

        query_posts( $query );
        include Realia_Template_Loader::locate( 'widgets/properties' );

        wp_reset_query();
    }

    /**
     * Update
     *
     * @access public
     * @param array $new_instance
     * @param array $old_instance
     * @return array
     */
    function update( $new_instance, $old_instance ) {
        return $new_instance;
    }

    /**
     * Backend
     *
     * @access public
     * @param array $instance
     * @return void
     */
    function form( $instance ) {
        include Realia_Template_Loader::locate( 'widgets/properties-admin' );
    }
}