<?php

if( !class_exists( 'PS_AEDL_Quota' ) ):
class PS_AEDL_Quota {

    private static $instance = null;
    private $has_transient = false;
    private $quota = 0;
    private $quota_status = 0;
    private $quota_duration = 0;
    private $quota_enabled = false;

    const QUOTA_STATUS = 'ps_aedl_quota_status';
    const TRANSIENT = 'ps_aedl_quota';

    /**
     * Get the instance of the class
     * 
     * @since 1.0.0
     * @version 1.0.0
     */
    public static function get_instance() {

        if( null == self::$instance ) {

            self::$instance = new self;

        }

        return self::$instance;

    }

    /**
     * constructor of the class
     * 
     * @since 1.0.0
     * @version 1.0.0
     */
    public function __construct() {
        
        $settings = get_option( PS_AEDL_Settings::OPTION );
        $this->quota_duration = psaedl_extract_value_from_option( $settings, 'quota_duration' );
        $this->quota = psaedl_extract_value_from_option( $settings, 'quota_value' );
        $this->quota_enabled = psaedl_extract_value_from_option( $settings, 'enable_quota' );

        if( $this->quota_enabled ) {
            $this->has_transient = get_transient( self::TRANSIENT );
            $this->quota_status = get_option( self::QUOTA_STATUS, 1 );
            add_filter( 'wp_mail', array( $this, 'handle_quota' ) );

        }

    }

    /**
     * Handle quota, Should emails send or keep in queue
     * 
     * @since 1.0.0
     * @version 1.0.0
     */
    public function handle_quota( $args ) {

        //Reset quota status if quota duration is passed
        if( !$this->has_transient ) {

            $seconds = $this->get_quota_duration();

            set_transient( self::TRANSIENT, true, $seconds );
            $this->reset_quota_status();

        }

        //Don't send emails, quota has been reached
        if( 
            ( $this->has_transient
            && 
            $this->quota_status >= $this->quota ) 
        ) {

            add_filter( 'post_smtp_send_email', '__return_false' );
            add_filter( 'post_smtp_do_send_email', '__return_false' );
            add_filter( 'post_smtp_log_status', array( $this, 'set_log_status' ) );

        }


        if( $this->quota_status < $this->quota ) {

            $this->update_quota_status();

        }

        return $args;

    }

    /**
     * Get quota duration in seconds
     * 
     * @since 1.0.0
     * @version 1.0.0
     */
    public function get_quota_duration() {

        $seconds = 0;

        switch( $this->quota_duration ) {

            case 'h':
                $seconds = 3600;
                break;

            case 'd':
                $seconds = 86400;
                break;

            case 'w':
                $seconds = 604800;
                break;

            case 'm':
                $seconds = 2592000;
                break;

        }

        return $seconds;

    }

    /**
     * Update quota status
     * 
     * @since 1.0.0
     * @version 1.0.0
     */
    public function update_quota_status() {

        $this->quota_status = get_option( SELF::QUOTA_STATUS );
        $this->quota_status = $this->quota_status ? $this->quota_status : 0;

        $this->quota_status++;

        update_option( SELF::QUOTA_STATUS, $this->quota_status );

    }

    /**
     * Reset quota status
     * 
     * @since 1.0.0
     * @version 1.0.0
     */
    public function reset_quota_status() {

        update_option( SELF::QUOTA_STATUS, 1 );

    }

    /**
     * Set log status | Filter Callback
     * 
     * @since 1.0.0
     * @version 1.0.0
     */
    public function set_log_status() {

        return 'In Queue';

    }

}

PS_AEDL_Quota::get_instance();    

endif;