<?php

namespace WPAdverts\Stripe;

class Core {
    
    /**
     * Singleton - an instance of Core class
     *
     * @var \WPAdverts\Stripe\Core
     */
    protected static $_instance = null;
    
    /**
     * Instance of the Admin class
     *
     * @var \WPAdverts\Stripe\Admin
     */
    public $admin = null;
    
    /**
     * Instance of the Ajax class
     *
     * @var \WPAdverts\Stripe\Ajax
     */
    public $ajax = null;
    
    /**
     * Instance of the Gateway class
     *
     * @var \WPAdverts\Core\Gateway
     */
    public $gateway = null;
    
    /**
     * Path to wpadverts-stripe directory
     *
     * @var string
     */
    public $basedir = null;
    
    /**
     * URL to wpadverts-stripe directory
     *
     * @var string
     */
    public $baseurl = null;
    
    /**
     * Class constructor
     * 
     * @since 1.0.0
     * @return void
     */
    public function __construct() {
        $this->basedir = dirname( dirname( __FILE__ ) );
        $this->baseurl = plugin_dir_url( dirname( __FILE__ ) );
        
        add_action( "init", array( $this, "init" ) );
        add_action( "plugins_loaded", array( $this, "adverts_namespace" ) );
        add_action( "plugins_loaded", array( $this, "plugins_loaded" ) );
        
        $this->load( 'class-gateway.php' );
        $this->gateway = new Gateway();
    }
    
    /**
     * Function executed when Stripe integration is initiated.
     * 
     * @since 1.0.0
     * @return void
     */
    public function run() {
        
    }
    
    /**
     * Returns an instance of WPAdverts\Stripe\Core
     * 
     * @since 1.0.0
     * @return \Wpadverts\Stripe\Core
     */
    public static function get_instance() {
        if( self::$_instance === null ) {
            self::$_instance = new self();
        }
        
        return self::$_instance;
    }
    
    /**
     * Loads a file from wpadverts-stripe/includes/ directory.
     * 
     * The valid argument is a name of a file (with a extension) from
     * wpadverts-stripe/includes directory.
     * 
     * @param string $file      File name
     * @return void
     */
    public function load( $file ) {
        include_once $this->basedir . '/includes/' . $file;
    }
    
    /**
     * Returns absolute path to wpadverts-stripe directory
     * 
     * @since 1.0.0
     * @return string   Absolute Path to wpadverts-stripe directory
     */
    public function get_basedir() {
        return $this->basedir;
    }
    
    /**
     * Returns absolute URL to wpadverts-stripe directory
     * 
     * @since 1.0.0
     * @return string   Absolute URL to wpadverts-stripe directory
     */
    public function get_baseurl() {
        return $this->baseurl;
    }
    
    /**
     * WPAdverts Stripe Init Function
     * 
     * This function is called by "init" action registered in self::__construct().
     * 
     * @since 1.0
     * @return void
     */
    public function init() {
        
        if( defined( "ADVERTS_PATH" ) ) {
            include_once ADVERTS_PATH . 'includes/class-updates-manager.php';
            $manager = new \Adverts_Updates_Manager(
                "wpadverts-stripe/wpadverts-stripe.php", 
                "wpadverts-stripe", 
                "1.1.2"
            );
            $manager->connect();
        }
        
        if( is_admin() ) {
            $this->init_admin();
        } else {
            $this->init_frontend();
        }
    }
    
    /**
     * WPAdverts Stripe Frontend Init Function
     * 
     * The function registers only actions and filters used in the frontend.
     * 
     * This function is called by "init" action registered in self::__construct().
     * 
     * @since 1.0
     * @return void
     */
    public function init_frontend() {
        
        wp_register_script(
            'wpadverts-stripe-lib',
            'https://js.stripe.com/v3/',
            array(),
            ''
        );
        
        wp_register_script(
            'wpadverts-stripe-main',
            $this->get_baseurl() . 'assets/js/stripe.js',
            array( 'jquery', 'wpadverts-stripe-lib', 'adext-payments' ),
            '1.1'
        );
        
        wp_register_style(
            'wpadverts-stripe-main',
            $this->get_baseurl() . 'assets/css/stripe.css',
            '1.1'
        );

        wp_localize_script(
            'wpadverts-stripe-main',
            'wpadverts_stripe_main_lang',
            array(
                'publishable_key' => adverts_config( 'stripe.publishable_key' ),
                'ajaxurl' => admin_url( 'admin-ajax.php' )
            )
        );
    }
    
    /**
     * WPAdverts Stripe Admin Init Function
     * 
     * The function registers only actions and filters used in the wp-admin.
     * 
     * This function is called by "init" action registered in self::__construct().
     * 
     * @since 1.0
     * @return void
     */
    public function init_admin() {
        $this->load( 'class-admin.php' );
        $this->admin = new Admin();
        
        $this->load( 'class-ajax.php' );
        $this->ajax = new Ajax();
    }
    
    /**
    * Adds default addon configuration to $adverts_namespace
    * 
    * @global array $adverts_namespace
    * 
    * @access public
    * @since 1.0
    * @return void
    */
    public function adverts_namespace() {
        
        if( ! defined( "ADVERTS_PATH" ) ) {
            // Run only if WPAdverts is active
            return;
        }

        global $adverts_namespace;

        // Add Stripe to adverts_namespace, in order to store module options and default options
        $adverts_namespace['stripe'] = array(
            'option_name' => 'wpadverts_stripe_config',
            'default' => array(
                'publishable_key'               => '',
                'secret_key'                    => '',
                'custom_title'                  => '',
                'send_receipts'                 => '1',
                'statement_descriptor'          => '',
                'live_confirm'                  => '0',
                'payment_intent__succeeded'     => ''
                
            )
        );
    }
    
    /**
     * Plugins loaded action
     * 
     * This action is executed by plugins_loaded action.
     * 
     * @return void
     */
    public function plugins_loaded() {
        if( ! defined( "ADVERTS_PATH" ) ) {
            // Run only if WPAdverts is active
            return;
        }
    }
    
    public static function get_customer_meta_key_name() {
        $pkey = adverts_config( 'stripe.publishable_key' );
        
        if( stripos( $pkey, "pk_test_") === 0 ) {
            $meta_key = "wpadverts_stripe_customer_id__test";
        } else {
            $meta_key = "wpadverts_stripe_customer_id";
        }
        
        return apply_filters( "wpadverts_stripe_get_customer_meta_key_name", $meta_key );
    }
}

