<?php

class Wpadverts_Mas_Frontend {
    
    /**
     * Constructor
     * 
     * Creates an instance of the object.
     * 
     * @since 1.0.0
     * @return void
     */
    public function __construct() {
        add_action( "init", array( $this, "init" ), 25 ); 
    }
    
    /**
     * Init Action
     * 
     * This function is called by "init" action, registered in self::__construct().
     * 
     * @since 1.0.0
     * @return void
     */
    public function init() {
        
        if( adverts_config( "mas.details_show_top_notification") ) {
            add_action( 'adverts_tpl_single_top', array( $this, "details_notification_top" ), 5 );
        }
        
        if( adverts_config( "mas.details_show_bottom_notification") ) {
            add_action( 'adverts_tpl_single_bottom', array( $this, "details_notification_bottom" ), 5 );
        }
        
        if( adverts_config( "mas.details_hide_contact_options") ) {
            add_action( 'wp', array( $this, 'hide_contact_options' ) );
        }
        
        if( adverts_config( 'mas.list_show_sold_icon' ) ) {
            add_action( 'adverts_list_after_title', array( $this, 'after_title' ) );
        }
        
        add_action( 'adverts_sh_manage_actions_right', array( $this, 'manage_actions' ), 10, 2 );
        
        add_action( 'shortcode_atts_adverts_list', array( $this, 'list_atts' ), 10, 3 );
        add_action( 'adverts_list_query', array( $this, 'list_query' ), 10, 2 );
    }
    
    /**
     * Is item sold
     * 
     * Function checks if Advert identified by $post_id is marked as sold or not.
     * 
     * @since 1.0.0
     * 
     * @param   int     $post_id     Post ID
     * @return  boolean              True if marked as sold, false otherwise
     */
    public function is_sold( $post_id ) {
        
        $mas = get_post_meta( $post_id, Wpadverts_Mas::META, true );
        
        if( $mas == "1" ) {
            return true;
        } else {
            return false;
        }
    }
    
    /**
     * Displays Top Notification
     * 
     * This function is called by adverts_tpl_single_top action registered
     * in self::init().
     * 
     * @since 1.0.0
     * 
     * @param   type $post_id   Current Post ID
     * @return  void
     */
    public function details_notification_top( $post_id ) {
        
        if( ! $this->is_sold( $post_id ) ) {
            return;
        }
        
        $flash = array( "error" => array(), "info" => array(), "warn" => array() );
        $message = Wpadverts_Mas::get_default_notification();
        
        if( adverts_config( 'mas.details_top_notification_text' ) ) {
            $message = adverts_config( 'mas.details_top_notification_text' );
        } 
        
        $flash["warn"][] = array(
            "message" => $message,
            "icon" => $this->get_icon()
        );
        
        adverts_flash( apply_filters( "wpadverts_mas_details_notification_top", $flash ) );
    } 
    
    /**
     * Displays Bottom Notification
     * 
     * This function is called by adverts_tpl_single_bottom action registered
     * in self::init().
     * 
     * @since 1.0.0
     * 
     * @param   type $post_id   Current Post ID
     * @return  void
     */
    public function details_notification_bottom( $post_id ) {
        
        if( ! $this->is_sold( $post_id ) ) {
            return;
        }
        
        $flash = array( "error" => array(), "info" => array(), "warn" => array() );
        $message = Wpadverts_Mas::get_default_notification();
        
        if( adverts_config( 'mas.details_bottom_notification_text' ) ) {
            $message = adverts_config( 'mas.details_bottom_notification_text' );
        } 
        
        $flash["warn"][] = array(
            "message" => $message,
            "icon" => $this->get_icon()
        );
 
        adverts_flash( apply_filters( "wpadverts_mas_details_notification_bottom", $flash ) );
    } 
    
    /**
     * Displays "Sold" text on [adverts_list]
     * 
     * This function is called by adverts_list_after_title action registered
     * in self::init().
     * 
     * @since 1.0.0
     * 
     * @param   type $post_id   Current Post ID
     * @return  void
     */
    public function after_title( $post_id ) {
        
        if( ! $this->is_sold( $post_id ) ) {
            return;
        }
        
        echo ' <span class="wpadverts-list-item-marked-as-sold">' . __( 'sold', 'wpadverts-mark-as-sold' ) . '</span>';
    }
    
    /**
     * Hides contact information on Ad details page.
     * 
     * This function is executed by 'wp' action and disables contact information
     * and contact form if currently viewed page is 'advert' marked as sold.
     * 
     * @since 1.0.0
     * @return void
     */
    public function hide_contact_options() {
        
        if( ! is_singular( 'advert' ) ) {
            return;
        }
        
        if( ! $this->is_sold( get_the_ID() ) ) {
            return;
        }
        
        remove_action( 'adverts_tpl_single_bottom', 'adverts_single_contact_information' );
        remove_action( 'adverts_tpl_single_bottom', 'adext_contact_form' );
        remove_action( 'adverts_tpl_single_bottom', 'adext_bp_send_private_message_button', 50 );
    }
    
    /**
     * Add "Sold!" button in [adverts_manage].
     * 
     * This function is executed by 'adverts_list_after_title' action and adds
     * on [adverts_manage] list "Sold!" or "For Sale" buttons for each item.
     * 
     * @see adverts_list_after_title action
     * 
     * @param int       $post_id
     * @param string    $baseurl
     * @return void
     */
    public function manage_actions( $post_id, $baseurl = null ) {
        
        $href = admin_url("admin-ajax.php");
        $params = array(
            "action" => "wpadverts_mas",
            "id" => $post_id,
            "_ajax_nonce" => wp_create_nonce('adverts-mas')
        );
        
        if( $baseurl !== null ) {
            $params["redirect_to"] = ( $baseurl );
        }
        
        if( $this->is_sold( $post_id ) && adverts_config( 'mas.manage_allow_marking_for_sale' ) ) {
            // mark for sale
            include_once ADVERTS_PATH . '/includes/class-html.php';
            $icon = sprintf( '<span class="%s"></span>', $this->get_icon() );
            $button = Adverts_Html::build( "a", array(
                "href" => $href . "?" . http_build_query( array_merge( $params, array( "mark_as" => "for-sale") ) ),
                "class" => "adverts-manage-action",
                "title" => __( "Mark this item as for sale", "wpadverts-mark-as-sold" )
            ), $icon . __( "For Sale", "wpadverts-mark-as-sold" ) );
            
            echo $button;
        } 
        
        if( ! $this->is_sold( $post_id ) && adverts_config( 'mas.manage_allow_marking_as_sold' ) ) {
            // mark sold
            include_once ADVERTS_PATH . '/includes/class-html.php';
            $icon = sprintf( '<span class="%s"></span>', $this->get_icon() );
            $button = Adverts_Html::build( "a", array(
                "href" => $href . "?" . http_build_query( array_merge( $params, array( "mark_as" => "sold") ) ),
                "class" => "adverts-manage-action",
                "title" => __( "Mark this item as sold.", "wpadverts-mark-as-sold" )
            ), $icon . __("Sold", "wpadverts-mark-as-sold") );
            
            echo $button;
        }
    }
    
    /**
     * Returns icon name.
     * 
     * It should be one of WPAdverts bundled font icons, that is an icon with
     * name starting wtih 'adverts-icon-'.
     * 
     * 
     * @since   1.0.1
     * 
     * @param   string      Icon name
     * @return  string      Icon name
     */
    public function get_icon( ) {
        return apply_filters( "wpadverts_mark_as_sold_icon", "adverts-icon-dollar" );
    }
    
    /**
     * Adds hide_sold param to [adverts_list]
     * 
     * @since   1.0.3
     * 
     * @param   array   $out        
     * @param   array   $pairs      
     * @param   array   $atts
     * @return  array
     */
    public function list_atts( $out, $pairs, $atts ) {
        if( ! isset( $atts["hide_sold"] ) ) {
            $out["hide_sold"] = 0;
        } else {
            $out["hide_sold"] = absint( $atts["hide_sold"] );
        }
        
        return $out;
    }
    
    /**
     * Customizes [adverts_list] query.
     * 
     * @since   1.0.3
     * 
     * @param   array   $args       WP_Query args
     * @param   array   $params     [adverts_list] params
     * @return  array
     */
    public function list_query( $args, $params ) {
        if( ! isset( $params["hide_sold"] ) || $params["hide_sold"] != 1 ) {
            return $args;
        }
        
        if( ! isset( $args["meta_query"] ) ) {
            $args["meta_query"] = array();
        }
        
        $args["meta_query"][] = array(
            "key" => Wpadverts_Mas::META,
            "value" => '',
            "compare" => 'NOT EXISTS'
        );
        
        return $args;
    }
}
