<?php

if( !class_exists( 'PS_AEDL_Settings' ) ):

class PS_AEDL_Settings {

    const SECTION = 'ps_aedl_section';
    const OPTION = 'ps_aedl_option';

    private static $instance = null;

    /**
     * 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;

    }

    /**
     * Instance of the class
     * 
     * @since 1.0.0
     * @version 1.0.0
     */
    public function __construct() {

        add_filter( 'post_smtp_admin_tabs', array( $this, 'add_tab' ), 11 );
        add_action( 'post_smtp_settings_menu', array( $this, 'section' ) );
        add_filter( 'post_smtp_sanitize', array( $this, 'sanitize' ), 10, 3 );

    }


    /**
     * Add tab to Post SMTP Admin | Filter Callback
     * 
     * @since 1.0.0
     * @version 1.0.0
     */
    public function add_tab( $tabs ) {
   
        $tabs['advanced_delivery'] = __( 'Advanced Delivery & Logs', 'ps-aedl' );

        return $tabs;

    }


    /**
     * Section | Action Callback
     * 
     * @since 1.0.0
     * @version 1.0.0
     */
    public function section() {

        $settings = get_option( self::OPTION );
        $enable_advance_delivery = psaedl_extract_value_from_option( $settings, 'enable_advance_delivery' );
        $emails_per_batch = psaedl_extract_value_from_option( $settings, 'emails_per_batch' );
        $batch_duration = psaedl_extract_value_from_option( $settings, 'batch_duration' );
        $enable_auto_retry = psaedl_extract_value_from_option( $settings, 'enable_auto_retry' );
        $times_to_retry = psaedl_extract_value_from_option( $settings, 'times_to_retry' );
        $delete_logs_days = psaedl_extract_value_from_option( $settings, 'delete_logs_days' );
        $enable_quota = psaedl_extract_value_from_option( $settings, 'enable_quota' );
        $quota_value = psaedl_extract_value_from_option( $settings, 'quota_value' );
        $quota_duration = psaedl_extract_value_from_option( $settings, 'quota_duration' );

        $quota = array(
            'h' => __( 'Per Hour', 'ps-aedl' ),
            'd' => __( 'Per Day', 'ps-aedl' ),
            'w' => __( 'Per Week', 'ps-aedl' ),
            'm' => __( 'Per Month', 'ps-aedl' ),
        );

        ?>
        <section id="advanced_delivery">
            <input type="hidden" name="ps_aedl_settings_submitted" value="1" />
            <h2><?php _e( 'Advanced Delivery', 'ps-aedl' ); ?></h2>
            <b>
                With this feature, you can send emails from the backend, which reduces the load times on the front end. Whenever a form is submitted, or an email request is triggered, the email is logged in the backend and queued to be sent according to the settings specified.
            </b>
            <table>
                <tr>
                    <td><?php _e( 'Enable Advanced Delivery', 'ps-aedl' ); ?></td>
                    <td>
                        <label class="ps-switch-1"> 
                            <input type="checkbox" value="1" <?php echo esc_attr( checked( $enable_advance_delivery, 1 ) ); ?> name="enable_advance_delivery" />
                            <span class="ps-switch-slider ps-switch-round"></span>
                        </label>
                    </td>
                </tr>
            </table>
            <h2><?php _e( 'Batch Settings', 'ps-aedl' ); ?></h2>
            <table>
                <tr>
                    <td><?php _e( 'Emails per batch', 'ps-aedl' ); ?></td>
                    <td>
                        <input type="number" value="<?php echo esc_attr( $emails_per_batch ); ?>" name="emails_per_batch" />
                    </td>
                </tr>
                <tr>
                    <td><?php _e( 'Duration between batch (Seconds)', 'ps-aedl' ); ?></td>
                    <td>
                        <input type="number" value="<?php echo esc_attr( $batch_duration ); ?>" name="batch_duration" />
                    </td>
                </tr>
            </table>
            <h2><?php _e( 'Quota Settings', 'ps-aedl' ); ?></h2>
            <table>
                <tr>
                    <td><?php _e( 'Enable Quota', 'ps-aedl' ); ?></td>
                    <td>
                        <label class="ps-switch-1"> 
                            <input type="checkbox" value="1" <?php echo esc_attr( checked( $enable_quota, 1 ) ); ?> name="enable_quota" />
                            <span class="ps-switch-slider ps-switch-round"></span>
                        </label>
                    </td>
                </tr>
                <tr>
                    <td><?php _e( 'Email quota value', 'ps-aedl' ); ?></td>
                    <td>
                        <input type="number" value="<?php echo esc_attr( $quota_value ); ?>" name="quota_value" />
                    </td>
                </tr>
                <tr>
                    <td><?php _e( 'Email quota duration', 'ps-aedl' ); ?></td>
                    <td>
                        <select name="quota_duration" style="width: 179.2px;">
                            <?php
                            foreach( $quota as $key => $value ) {

                                $selected = ( $quota_duration == $key ) ? 'selected' : '';

                                echo '<option value="' . $key . '" ' . $selected . '>' . $value . '</option>';

                            }
                            ?>
                        </select>
                    </td>
                </tr>
            </table>
            <table>
                <h2><?php _e( 'Auto Retry', 'ps-aedl' ); ?></h2>
                <tr>
                    <td><?php _e( 'Enable Auto Retry', 'ps-aedl' ); ?></td>
                    <td>
                        <label class="ps-switch-1"> 
                            <input type="checkbox" value="1" <?php echo esc_attr( checked( $enable_auto_retry, 1 ) ); ?> name="enable_auto_retry" />
                            <span class="ps-switch-slider ps-switch-round"></span>
                        </label>
                    </td>
                </tr>
                <tr>
                    <td><?php _e( 'Times to Retry', 'ps-aedl' ); ?></td>
                    <td>
                        <input type="number" value="<?php echo esc_attr( $times_to_retry ); ?>" name="times_to_retry" />
                    </td>
                </tr>
            </table>
            <table>
                <h2><?php _e( 'Logs', 'ps-aedl' ); ?></h2>
                <tr>
                    <td><?php _e( 'Delete logs after days', 'ps-aedl' ); ?></td>
                    <td>
                        <input type="number" value="<?php echo esc_attr( $delete_logs_days ); ?>" name="delete_logs_days" />
                    </td>
                </tr>
            </table>
        </section>
      <?php

    }


    /**
     * Sanitize the Settings | Filter Callback
     * 
     * @since 1.0.0
     * @version 1.0.0
     */
    public function sanitize( $input, $option, $section ) {

        if( ! isset( $_POST['ps_aedl_settings_submitted'] ) ) {
            return $input;
        }

        $data = array();
        $existing_data = get_option( self::OPTION, array() );

        $data['enable_advance_delivery'] = isset( $_POST['enable_advance_delivery'] ) ? 1 : 0;
        $data['emails_per_batch'] = isset( $_POST['emails_per_batch'] ) ? sanitize_text_field( $_POST['emails_per_batch'] ) : psaedl_extract_value_from_option( $existing_data, 'emails_per_batch' );
        $data['batch_duration'] = isset( $_POST['batch_duration'] ) ? sanitize_text_field( $_POST['batch_duration'] ) : psaedl_extract_value_from_option( $existing_data, 'batch_duration' );
        $data['enable_auto_retry'] = isset( $_POST['enable_auto_retry'] ) ? 1 : 0;
        $data['times_to_retry'] = isset( $_POST['times_to_retry'] ) ? sanitize_text_field( $_POST['times_to_retry'] ) : psaedl_extract_value_from_option( $existing_data, 'times_to_retry' );
        $data['delete_logs_days'] = isset( $_POST['delete_logs_days'] ) ? sanitize_text_field( $_POST['delete_logs_days'] ) : psaedl_extract_value_from_option( $existing_data, 'delete_logs_days' );
        $data['enable_quota'] = isset( $_POST['enable_quota'] ) ? 1 : 0;
        $data['quota_value'] = isset( $_POST['quota_value'] ) ? sanitize_text_field( $_POST['quota_value'] ) : psaedl_extract_value_from_option( $existing_data, 'quota_value' );
        $data['quota_duration'] = isset( $_POST['quota_duration'] ) ? sanitize_text_field( $_POST['quota_duration'] ) : psaedl_extract_value_from_option( $existing_data, 'quota_duration' );

        if( empty( $data['quota_duration'] ) ) {
            $data['quota_duration'] = 'd';
        }

        update_option( self::OPTION, $data );

        return $input;

    }

}

PS_AEDL_Settings::get_instance();

endif;