<?php

class Wpadverts_Mas_Admin {
    
    public function __construct() {
        add_action( 'init', array( $this, 'updates_manager' ) );
        add_action( 'save_post', array( $this, 'save_post' ), 16, 2 );
        add_action( 'post_submitbox_misc_actions', array( $this, 'meta_box' ), 16 );
        add_filter( 'display_post_states', array( $this, 'post_state' ), 2000 );
    }
    
    /**
     * Handles the updates manager
     * 
     * @since 1.0.0
     * @return void
     */
    public function updates_manager() {
        
        if( ! defined( "ADVERTS_PATH" ) ) {
            return;
        }

        include_once ADVERTS_PATH . 'includes/class-updates-manager.php';
        $manager = new Adverts_Updates_Manager(
            "wpadverts-mark-as-sold/wpadverts-mark-as-sold.php", 
            "wpadverts-mark-as-sold", 
            "1.0.3"
        );
        $manager->connect();
    }
    
    /**
     * Returns config form scheme
     * 
     * @since 1.0.0
     * @return array    Config Form Scheme
     */
    public function get_page_options_form_scheme() {
        return array(
            "name" => "",
            "action" => "",
            "field" => array(
                array(
                    "name" => "_mas_ad_details",
                    "type" => "adverts_field_header",
                    "order" => 10,
                    "label" => __( 'Ad Details Pages', 'wpadverts-mark-as-sold' )
                ),
                array(
                    "name" => "show_notification",
                    "type" => "adverts_field_checkbox",
                    "label" => __("Show 'Sold' Notification", "wpadverts-mark-as-sold"),
                    "options" => array(
                        array(
                            "value" => "top",
                            "text" => __("On Top - above Advert gallery.", "wpadverts-mark-as-sold")
                        ),
                        array(
                            "value" => "bottom",
                            "text" => __("At The Bottom - below Advert description.", "wpadverts-mark-as-sold")
                        )
                    ),
                    "order" => 10,
                    "max_choices" => 2
                ),
                array(
                    "name" => "details_hide_contact_options",
                    "type" => "adverts_field_checkbox",
                    "label" => __("Contact Options", "wpadverts-mark-as-sold"),
                    "options" => array(
                        array(
                            "value" => "1",
                            "text" => __("Hide contact details (or contact form) for 'sold' Ads.", "wpadverts-mark-as-sold")
                        )
                    ),
                    "order" => 10
                ),
                array(
                    "name" => "details_top_notification_text",
                    "type" => "adverts_field_text",
                    "label" => __("Top Notification Text", "wpadverts-mark-as-sold"),
                    "attr" => array(
                        "placeholder" => Wpadverts_Mas::get_default_notification()
                    ),
                    "order" => 10
                ),
                array(
                    "name" => "details_bottom_notification_text",
                    "type" => "adverts_field_text",
                    "label" => __("Bottom Notification Text", "wpadverts-mark-as-sold"),
                    "attr" => array(
                        "placeholder" => Wpadverts_Mas::get_default_notification()
                    ),
                    "order" => 10
                ),
                array(
                    "name" => "_mas_adverts_list",
                    "type" => "adverts_field_header",
                    "order" => 10,
                    "label" => __( 'Shortcode [adverts_list]', 'wpadverts-mark-as-sold' )
                ),
                array(
                    "name" => "list_show_sold_icon",
                    "type" => "adverts_field_checkbox",
                    "label" => __("'Sold' Icon", "wpadverts-mark-as-sold"),
                    "options" => array(
                        array(
                            "value" => "1",
                            "text" => __("Show 'sold' icon next to Ad title.", "wpadverts-mark-as-sold")
                        )
                    ),
                    "order" => 10
                ),
                array(
                    "name" => "_mas_adverts_manage",
                    "type" => "adverts_field_header",
                    "order" => 10,
                    "label" => __( 'Shortcode [adverts_manage]', 'wpadverts-mark-as-sold' )
                ),
                array(
                    "name" => "manage_allow_marking",
                    "type" => "adverts_field_checkbox",
                    "label" => __("Buttons", "wpadverts-mark-as-sold"),
                    "options" => array(
                        array(
                            "value" => "sold",
                            "text" => __("Users can mark items as 'sold'.", "wpadverts-mark-as-sold")
                        ),
                        array(
                            "value" => "for-sale",
                            "text" => __("Users can mark items as 'for sale'.", "wpadverts-mark-as-sold")
                        )
                    ),
                    "order" => 10,
                    "max_choices" => 2
                ),

            ),

        );
    }
    
    /**
     * Renders options page
     * 
     * This function is executed when trying to view wp-admin / Classifieds / Options
     * / Mark As Sold panel.
     * 
     * @since 1.0.0
     * @return void
     */
    public function page_options() {
        
        wp_enqueue_style( 'adverts-admin' );
        
        $flash = Adverts_Flash::instance();
        $error = array();
        
        $options = get_option ( "wpadverts_mas_config", array() );

        if( $options === null || empty( $options ) ) {
            $options = adverts_config( "mas.ALL" );
        }

        $options["show_notification"] = array();
        if( isset( $options['details_show_top_notification'] ) && $options['details_show_top_notification'] ) {
            $options["show_notification"][] = "top";
        }
        if( isset( $options['details_show_bottom_notification'] ) && $options['details_show_bottom_notification'] ) {
            $options["show_notification"][] = "bottom";
        }
        
        $options["manage_allow_marking"] = array();
        if( isset( $options['manage_allow_marking_as_sold'] ) && $options['manage_allow_marking_as_sold'] ) {
            $options["manage_allow_marking"][] = "sold";
        }
        if( isset( $options['manage_allow_marking_for_sale'] ) && $options['manage_allow_marking_for_sale'] ) {
            $options["manage_allow_marking"][] = "for-sale";
        }
          
        if( isset( $options['list_show_sold_icon'] ) && $options['list_show_sold_icon'] ) {
            $options["list_show_sold_icon"] = array( 0 => 1 );
        }
        
        if( isset( $options['details_hide_contact_options'] ) && $options['details_hide_contact_options'] ) {
            $options["details_hide_contact_options"] = array( 0 => 1 );
        }
        
        $form = new Adverts_Form( $this->get_page_options_form_scheme() );
        $form->bind( $options );

        $button_text = __("Update Options", "adverts");

        if( isset( $_POST ) && !empty( $_POST ) ) {
            
            $form->bind( stripslashes_deep( $_POST ) );
            $valid = $form->validate();

            $_notify = $form->get_value( "show_notification" );
            $_manage = $form->get_value( "manage_allow_marking" );
            
            $tosave = array(
                'details_show_top_notification' => is_array( $_notify ) && in_array( "top", $_notify ) ? "1" : "0",
                'details_show_bottom_notification' => is_array( $_notify ) && in_array( "bottom", $_notify ) ? "1" : "0",
                'details_hide_contact_options' => $form->get_value( "details_hide_contact_options" ) ? "1" : "0",
                'details_top_notification_text' => trim( $form->get_value( "details_top_notification_text" ) ),
                'details_bottom_notification_text' => trim( $form->get_value( "details_bottom_notification_text" ) ),
                
                'list_show_sold_icon' => $form->get_value( "list_show_sold_icon" ) ? "1" : "0",
                
                'manage_allow_marking_as_sold' => is_array( $_manage ) && in_array( "sold", $_manage ) ? "1" : "0",
                'manage_allow_marking_for_sale' => is_array( $_manage ) && in_array( "for-sale", $_manage ) ? "1" : "0"
            );
            
            if($valid) {

                update_option("wpadverts_mas_config", $tosave);
                $flash->add_info( __("Settings updated.", "adverts") );
            } else {
                $flash->add_error( __("There are errors in your form.", "adverts") );
            }
        }
        
        include Wpadverts_Mas::get_instance()->get_basedir() . '/admin/options.php';
    }
    
    public function meta_box() {
        global $post;
    
        // Do this for adverts only.
        if($post->post_type != 'advert') {
            return;
        }

        ?>

        <div class="misc-pub-section">
            <label>
                <?php wp_nonce_field( "wpadverts_mas_admin", "_mark_as_sold_nonce", false ) ?>
                <input type="checkbox" name="<?php echo esc_attr(Wpadverts_Mas::META) ?>" value="1" <?php checked( get_post_meta( $post->ID, Wpadverts_Mas::META, true ), "1" ) ?>/> 
                <?php _e( "This item is marked as sold.", "wpadverts-mark-as-sold" ) ?>
            </label>
        </div>

        <?php
    }
    
    public function save_post($ID = false) {
        global $pagenow;
        
        $nonce = 'wpadverts_mas_admin';
        $nonce_action = '_mark_as_sold_nonce';

        if ( ! in_array($pagenow, array("post.php", "post-new.php") ) ) {
            return;
        }

        if ( ! isset( $_POST[$nonce_action] ) || ! wp_verify_nonce( $_POST[$nonce_action], $nonce ) ) {
            return;
        }

        $post = get_post( $ID );
        
        if ( ! $post || $post->post_type != 'advert' ) {
            return;
        }

        if ( defined( "DOING_AJAX" ) && DOING_AJAX ) {
            return;
        }

        if( adverts_request( Wpadverts_Mas::META ) == "1" ) {
            update_post_meta( $ID, Wpadverts_Mas::META, "1" );
        } else {
            delete_post_meta( $ID, Wpadverts_Mas::META );
        }

        return;
    }
    
    public function post_state( $states ) {
        global $post;

        if( $post && $post->post_type == 'advert' && get_post_meta( $post->ID, Wpadverts_Mas::META, true ) == "1" ) {

            $span = new Adverts_Html("span", array(
                "class" => "dashicons dashicons-awards",
                "title" => __( 'Sold!', 'wpadverts-mark-as-sold' ),
                "style" => "font-size: 20px"
            ));
            $span->forceLongClosing(true);

            $states[] = $span->render();
        }

        return $states;
    }
}
