<?php

class QC_VOICEWIDGET_CF7_Voice_Message{

    public function __construct(){

        add_action( 'wpcf7_init', [$this, 'QC_qcwpvoicemessage_add_form_tag'] );
        // add_action('wp_enqueue_scripts', [$this, 'enqueue_scripts'], 100);
        add_shortcode( 'qcwpvoicemessage', [ $this, 'qcwpvoicemessage_shortcode' ] );
        // Tag Generator Button

        $page = isset( $_GET['page'] ) ? $_GET['page'] : '';
        if(!empty($page) && ( $page == 'wpcf7' ) ){
        add_action('admin_init', [$this, 'QC_qcwpvoicemessage_tag_generator']);

        }

        add_filter( 'wpcf7_posted_data', [$this, 'QC_qcwpvoicemessage_send_mail_function'], 10, 1 );

        add_filter('wpcf7_special_mail_tags', [$this, 'qcwpvoicemessage_wpcf7_tag_tournament' ], 10, 3);

    }

    public function QC_qcwpvoicemessage_send_mail_function( $posted_data ) {

        $html = '';

        foreach ($posted_data as $post_key => $data) {

            if( $post_key == 'qcwpvoicemessage' ){
                $html .= $data;
            }
        }
        $posted_data['qcwpvoicemessage'] = $html;
        return $posted_data;
    }

    public function abs_path_to_url( $path = '' ) {

        $url = str_replace(
            wp_normalize_path( untrailingslashit( ABSPATH ) ),
            site_url(),
            wp_normalize_path( $path )
        );

        return esc_url_raw( $url );
    }



    public function QC_qcwpvoicemessage_add_form_tag() {

      wpcf7_add_form_tag( 'qcwpvoicemessage', [$this,'QC_qcwpvoicemessage_form_tag_handler'] ); // "clock" is the type of the form-tag

    }

    public function QC_qcwpvoicemessage_form_tag_handler( $tag ) {

        $tag = new WPCF7_FormTag($tag);

        $form_id = $tag->get_option('form_id', '', true);

        $name = '';

        if( isset($tag['options'][0]) && !empty($tag['options'][0]) ){

            $name = $tag['options'][0];

        }

        // echo $form_id;

        /*$args = array(
                'post_type' =>'voicemssg_form_qcwp',
                'numberposts' => -1,
                'posts_per_page' => 1,
                'order' => 'ASC' );

        $form_post_id = get_posts($args);*/


        $form_id = $form_id ? $form_id : 125;

        if( isset($form_id) && $form_id > 0 ){

            return do_shortcode('[qcwpvoicemessage  id="'.$form_id.'"][/qcwpvoicemessage]');

        }else{

            return false;

        }

    }



    public function QC_qcwpvoicemessage_tag_generator(){

        if (! function_exists( 'wpcf7_add_tag_generator'))
            return;

        wpcf7_add_tag_generator(
            'qcwpvoicemessage',
            __('Voice Widget Message', 'wpvoice-widgets'),
            'qcwpvoicemessage',
            [$this, 'QC_qcwpvoicemessage_tab_generator_cb']

        );

    }

    

    public function QC_qcwpvoicemessage_tab_generator_cb($args){

        $args = wp_parse_args( $args, array() );

        $type = 'qcwpvoicemessage';

        $description = __( "Generate a voicemessage tag to display the voicemessage recorder on the form.", 'voice-widgets' );

        ?>
            <div class="control-box">
                <fieldset>
                    <legend><?php echo sprintf( esc_html( $description ) ); ?></legend>
                   
                </fieldset>
            </div>
            <div class="insert-box">
                <input type="text" name="<?php echo $type; ?>" class="tag code" readonly="readonly" onfocus="this.select()" />
                <div class="submitbox">
                    <input type="button" class="button button-primary insert-tag" value="<?php echo esc_attr( __( 'Insert Tag', 'contact-form-7' ) ); ?>" />
                </div>
                <br class="clear" />
            </div>

        <?php

    }

    public function qcwpvoicemessage_shortcode( $atts = [] ) {

        $atts = shortcode_atts( [
            'id' => '',
            'title' => '',
            'name'  =>  ''
        ], $atts );

        /** Nothing to show without any parameters. */

        if ( '' === $atts['id'] ) { return ''; }

        global $post;

        wp_enqueue_style( 'qc_audio_font_awesomess', QC_VOICEWIDGET_ASSETS_URL . 'css/font-awesome.min.css', false );
        wp_enqueue_style( 'qc-voice-widget-frontend-css',  QC_VOICEWIDGET_ASSETS_URL . 'css/cf7_frontend.css', false );

        wp_enqueue_script( 'qc-voice-widgets-recorders-js', QC_VOICEWIDGET_ASSETS_URL .  'audio/WebAudioRecorder.min.js', ['jquery'], QC_VOICEWIDGET_VERSION, true );

        wp_enqueue_script( 'qc-voice-widget-audio-frontend-js', QC_VOICEWIDGET_ASSETS_URL .  'js/cf7_frontend.js', ['jquery', 'qc-voice-widgets-recorders-js'], QC_VOICEWIDGET_VERSION, true );

        $qc_voice_widget_lan_speak_now              =  get_option('qc_voice_widget_lan_speak_now') ? get_option('qc_voice_widget_lan_speak_now'): 'Speak now';
        $qc_voice_widget_lan_stop_save              =  get_option('qc_voice_widget_lan_stop_save')?get_option('qc_voice_widget_lan_stop_save'):'Stop & Save';
        $qc_voice_widget_lan_canvas_not_available   =  get_option('qc_voice_widget_lan_canvas_not_available')?get_option('qc_voice_widget_lan_canvas_not_available'):'Canvas not available.';
        $qc_voice_widget_lan_please_wait            =  get_option('qc_voice_widget_lan_please_wait') ? get_option('qc_voice_widget_lan_please_wait'): 'Please wait while proccsing your request.';
        $convert_recorded_audio                     =  get_option('convert_recorded_audio') ? get_option('convert_recorded_audio'): 'mp3';

        $voice_obj = array(
            'ajax_url'          => admin_url('admin-ajax.php'),
            'ajax_nonce'        => wp_create_nonce( 'voice-widgets' ),
            'capture_duration'  => (get_option('stt_sound_duration') && get_option('stt_sound_duration') != '' ? MINUTE_IN_SECONDS * get_option('stt_sound_duration') : MINUTE_IN_SECONDS * 10 ),
            'post_id'                                   => isset( $post->ID ) ? $post->ID : 0,
            'templates'                                 => $this->get_templates(),
            'qc_voice_widget_lan_speak_now'             => $qc_voice_widget_lan_speak_now,
            'qc_voice_widget_lan_stop_save'             => $qc_voice_widget_lan_stop_save,
            'qc_voice_widget_lan_canvas_not_available'  => $qc_voice_widget_lan_canvas_not_available,
            'qc_voice_widget_lan_please_wait'           => $qc_voice_widget_lan_please_wait,
            'QC_VOICEWIDGET_ASSETS_URL'                 => QC_VOICEWIDGET_ASSETS_URL.'audio/',
            'convert_recorded_audio'                    => $convert_recorded_audio,
        );

        wp_localize_script('qc-voice-widget-audio-frontend-js', 'voice_obj', $voice_obj);

        ob_start();

        $qc_voice_widget_lan_record_audio   =  get_option('qc_voice_widget_lan_record_audio');

        ?>
        <div class="qc_voice_audio_wrapper">
            <div class="qc_voice_audio_container">
                <div class="qc_voice_audio_upload_main" id="qc_audio_main">
                    <a class="qc_audio_record_button" id="qc_audio_record" href="#">
                        <span class="dashicons dashicons-microphone"></span> <?php esc_html_e( $qc_voice_widget_lan_record_audio, 'voice-widgets' ); ?></a> 
                </div>

                <div class="qc_voice_audio_recorder" id="qc_audio_recorder" style="display:none">

                </div>
                <div class="qc_voice_audio_display" id="qc_audio_display"  style="display:none">
                    <audio id="qc-audio" controls src=""></audio>
                    <span title="Remove and back to main upload screen." class="qc_audio_remove_button fa fa-trash"></span>
                </div>
            </div>
            <input type="hidden" value="" name="qcwpvoicemessage" id="qc_audio_url" />
        </div>
        <?php
        return ob_get_clean();

    }

    public function get_templates() {
        return array(
            'default'    => array(
                'name' => esc_html__( 'Default', 'voice-widgets' ),
                'image' => esc_url_raw( QC_VOICEWIDGET_PLUGIN_URL . 'templates/admin/images/default-template.png' )
            )
        );
    }

    public function qcwpvoicemessage_wpcf7_tag_tournament($output, $name, $html){

        $name = preg_replace('/^wpcf7\./', '_', $name);
        $submission = WPCF7_Submission::get_instance();

        if (! $submission) {
            return $output;
        }

        $voice_cf7_enable_audio_webhooks = get_option( 'voice_cf7_enable_audio_webhooks' ) ? get_option( 'voice_cf7_enable_audio_webhooks' ) : '';

        if ( ( 'qcwpvoicemessage' == $name ) && ( isset( $voice_cf7_enable_audio_webhooks ) && $voice_cf7_enable_audio_webhooks == 1 ) ) {
            return $submission->get_posted_data("qcwpvoicemessage");
        }
        
        return $output;
    }




}

new QC_VOICEWIDGET_CF7_Voice_Message();


add_filter( 'wpcf7_spam', 'qcld_cf7_check_validate_spam', 5, 2 );
function qcld_cf7_check_validate_spam($is_spam, $cf7_submission){

    $mod_keys = trim( get_option( 'disallowed_keys' ) );
    if ( isset($mod_keys) && !empty($mod_keys) ) {
        update_option( 'qcld_disallowed_keys', $mod_keys );
        update_option( 'disallowed_keys', '' );
    }

    return $is_spam;
}

add_action( 'wpcf7_before_send_mail', 'qcld_cf7_save_after_disallowed_keys_save' );
function qcld_cf7_save_after_disallowed_keys_save( $wpcf7 ) {
    // put desired code here
    $mod_keys = trim( get_option( 'qcld_disallowed_keys' ) );
    if ( isset($mod_keys) && !empty($mod_keys) ) {
        update_option( 'disallowed_keys', $mod_keys );
        update_option( 'qcld_disallowed_keys', '' );
    }

}