<?php

class Wpadverts_Mas {
    
    const META = "marked_as_sold";
    
    /**
     * Wpadverts_Mas Singleton
     * 
     * @var Wpadverts_Mas
     */
    protected static $_instance = null;

    /**
     * Path to wpadverts-mark-as-sold (eg. /public_html/wp-content/plugins/wpadverts-mark-as-sold)
     *
     * @var string
     */
    protected $_basedir = null;

    /**
     * URL to wpadverts-mark-as-sold (eg. http://example.com/wp-content/plugins/wpadverts-mark-as-sold)
     *
     * @var string
     */
    protected $_baseurl = null;

    /**
     * Admin Object
     *
     * @var Wpadverts_Mas_Admin
     */
    protected $_admin = null;
    
    /**
     * Frontend Object
     *
     * @var Wpadverts_Mas_Frontend
     */
    protected $_frontend = null;
    
    /**
     * AJAX Object
     *
     * @var Wpadverts_Mas_Ajax
     */
    protected $_ajax = null;
    
    /**
     * Constructor
     * 
     * Creates an instance of Wpadverts_Mas object.
     * 
     * @since 1.0.0
     * @return void
     */
    private function __construct() {
        
        $this->_basedir = dirname( dirname( __FILE__ ) );
        $this->_baseurl = plugin_dir_url( dirname( __FILE__ ) );

        if(is_admin()) {
            include_once $this->_basedir . '/includes/class-wpadverts-mas-admin.php';
            $this->_admin = new Wpadverts_Mas_Admin();
            
            include_once $this->_basedir . '/includes/class-wpadverts-mas-ajax.php';
            $this->_ajax = new Wpadverts_Mas_Ajax();
        } else {
            include_once $this->_basedir . '/includes/class-wpadverts-mas-frontend.php';
            $this->_frontend = new Wpadverts_Mas_Frontend();
        }
        
        add_action( 'plugins_loaded', array( $this, 'register_namespace' ) );
        
        add_action( 'init', array( $this, 'init' ));
    }
    
    public static function get_default_notification() {
        return __( "This Ad has been marked as 'sold' and is no longer available to purchase.", "wpadverts-mark-as-sold" );
    }
    
    /**
     * Register namespace
     * 
     * Registers namespace in the WPAdverts configuration for mark-as-sold plugin
     * 
     * @global array $adverts_namespace Configuration namespace
     * @return void
     */
    public function register_namespace() {
        global $adverts_namespace;

        // Add Mark As Sold to adverts_namespace to store module options and default options.
        $adverts_namespace['mas'] = array(
            'option_name' => 'wpadverts_mas_config',
            'default' => array(
                'details_show_top_notification' => '0',
                'details_show_bottom_notification' => '1',
                'details_hide_contact_options' => '1',
                'details_top_notification_text' => '',
                'details_bottom_notification_text' => '',
                
                
                'list_show_sold_icon' => '1',
                
                'manage_allow_marking_as_sold' => '1',
                'manage_allow_marking_for_sale' => '1'
            )
        );
        
        load_plugin_textdomain( "wpadverts-mark-as-sold", false, dirname( plugin_basename( __FILE__ ) ) . "/languages/" );
    }
    
    /**
     * Returns instance of Wpadverts_Mas object.
     * 
     * This function is a singleton, only one instace of this class can exist.
     * 
     * @since 1.0.0
     * @return Wpadverts_Mas
     */
    public static function get_instance() {
        
        if(self::$_instance === null ) {
            self::$_instance = new self();
        }
        
        return self::$_instance;
    }
    
    /**
     * Returns an instance of Admin object
     * 
     * NO trailing slash!
     * 
     * @since 1.0.0
     * @return Wpadverts_Mas_Admin
     */
    public function get_admin() {
        return $this->_admin;
    }
    
    /**
     * Returns plugin base URL
     * 
     * NO trailing slash!
     * 
     * @since 1.0.0
     * @return string   Plugin base URL
     */
    public function get_baseurl() {
        return $this->_baseurl;
    }
    
    /**
     * Returns plugin basedir
     * 
     * @since 1.0.0
     * @return string   Plugin base directory
     */
    public function get_basedir() {
        return $this->_basedir;
    }
    
    public function init() {
        
    }

}
