<?php

// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

// Apply filters for all setting tabs
function eapsgt_admin_settings() {
	wp_nonce_field( basename( __FILE__ ), "nonce-admin" );
	wp_enqueue_script( 'animation-script', EAPSGT_PLUGIN_URL . '/assets/js/eapsgt_animation_settings.min.js', array( 'jquery' ), null, false );
	?>
    <div class="eapsgt-tabs">
        <input class="eapsgt-radio" type="radio" name="eapsgt-tabs"
               id="eapsgt-tabone" checked="checked">
        <label class="eapsgt-label"
               for="eapsgt-tabone"><?php esc_html_e( "Template", 'easy-animated-popup' ); ?></label>
        <div class="eapsgt-tab">
			<?php apply_filters( 'eapsgt_tab_template', '' ) ?>
        </div>

        <input class="eapsgt-radio" type="radio" name="eapsgt-tabs"
               id="eapsgt-tabtwo">
        <label class="eapsgt-label"
               for="eapsgt-tabtwo"><?php esc_html_e( "Basic settings", 'easy-animated-popup' ); ?></label>
        <div class="eapsgt-tab">
			<?php apply_filters( 'eapsgt_tab_basic_settings', '' ) ?>
        </div>

        <input class="eapsgt-radio" type="radio" name="eapsgt-tabs"
               id="eapsgt-tabthree">
        <label class="eapsgt-label"
               for="eapsgt-tabthree"><?php esc_html_e( "Animation settings", 'easy-animated-popup' ); ?></label>
        <div class="eapsgt-tab">
			<?php apply_filters( 'eapsgt_tab_animation', '' ) ?>
        </div>

        <input class="eapsgt-radio" type="radio" name="eapsgt-tabs"
               id="eapsgt-tabfour">
        <label class="eapsgt-label"
               for="eapsgt-tabfour"><?php esc_html_e( "Display", 'easy-animated-popup' ); ?></label>
        <div class="eapsgt-tab">
			<?php apply_filters( 'eapsgt_tab_display', '' ) ?>
        </div>
    </div>
	<?php
}

// Add a meta box for popup settings
function eapsgt_add_settings() {
	add_meta_box( "admin_settings_box", "PopUp Settings", "eapsgt_admin_settings", EAPSGT, "normal", "high", null );
	add_meta_box( 'submitdiv', __( 'Publish' ), 'post_submit_meta_box', EAPSGT, 'normal', 'low' );
}

add_action( "add_meta_boxes", "eapsgt_add_settings" );

// Save settings for popup
function eapsgt_save_settings( $post_id, $post, $update ) {
	if ( ( ! isset( $_POST["nonce1"] ) || ! wp_verify_nonce( $_POST["nonce1"], "animation-settings-filed" ) ) &&
	     ( ! isset( $_POST["nonce2"] ) || ! wp_verify_nonce( $_POST["nonce2"], "display-settings-field" ) )
	) {
		return $post_id;
	}

	if ( ! current_user_can( "edit_post", $post_id ) ) {
		return $post_id;
	}

	if ( defined( "DOING_AUTOSAVE" ) && DOING_AUTOSAVE ) {
		return $post_id;
	}

	$slug = "eapsgt";
	if ( $slug != $post->post_type ) {
		return $post_id;
	}

	if ( isset( $_POST["eapsgt-animationList"] ) ) {
		$meta_data_animation_value = sanitize_text_field($_POST["eapsgt-animationList"]);
		update_post_meta( $post_id, "_eapsgt-animationList", $meta_data_animation_value );
	}

	if ( isset( $_POST["eapsgt-duration_value"] ) ) {
		$meta_data_duration_value = sanitize_text_field($_POST["eapsgt-duration_value"]);
		update_post_meta( $post_id, "_eapsgt-duration_value", $meta_data_duration_value );
	}

	if ( isset( $_POST["eapsgt-trigger"] ) && ( isset( $_POST["eapsgt-delay_value"] ) || isset( $_POST["eapsgt-scroll_value"] ) ) ) {

        $meta_data_trigger_value = sanitize_text_field( $_POST["eapsgt-trigger"] );
        $meta_data_delay_value   = sanitize_text_field( $_POST["eapsgt-delay_value"] );
        $meta_data_scroll_value  = sanitize_text_field( $_POST["eapsgt-scroll_value"] );

        if ( $meta_data_trigger_value == "none" ) {
			update_post_meta( $post_id, "eapsgt-trigger", $meta_data_trigger_value );
			update_post_meta( $post_id, "eapsgt-delay_value", "" );
			update_post_meta( $post_id, "eapsgt-scroll_value", "" );
		}
		if ( $meta_data_trigger_value == "exit" ) {
			update_post_meta( $post_id, "eapsgt-trigger", $meta_data_trigger_value );
			update_post_meta( $post_id, "eapsgt-delay_value", "" );
			update_post_meta( $post_id, "eapsgt-scroll_value", "" );
		}
		if ( $meta_data_trigger_value == "time" ) {
			update_post_meta( $post_id, "eapsgt-trigger", $meta_data_trigger_value );
			update_post_meta( $post_id, "eapsgt-delay_value", $meta_data_delay_value );
			update_post_meta( $post_id, "eapsgt-scroll_value", "" );
		}
		if ( $meta_data_trigger_value == "scroll" ) {
			update_post_meta( $post_id, "eapsgt-trigger", $meta_data_trigger_value );
			update_post_meta( $post_id, "eapsgt-delay_value", "" );
			update_post_meta( $post_id, "eapsgt-scroll_value", $meta_data_scroll_value );
		}
		if ( $meta_data_trigger_value == "hover" ) {
			update_post_meta( $post_id, "eapsgt-trigger", $meta_data_trigger_value );
			update_post_meta( $post_id, "eapsgt-delay_value", "" );
			update_post_meta( $post_id, "eapsgt-scroll_value", "" );
		}
	}

    $meta_data_target_page = isset( $_POST["eapsgt-page_list"] ) ? (array) $_POST["eapsgt-page_list"] : array();
    $meta_data_target_page = array_map( 'sanitize_text_field', $meta_data_target_page );
    update_post_meta( $post_id, "_eapsgt-page_list", $meta_data_target_page );

    $meta_data_target_post = isset( $_POST["eapsgt-post_list"] ) ? (array) $_POST["eapsgt-post_list"] : array();
    $meta_data_target_post = array_map( 'sanitize_text_field', $meta_data_target_post );
    update_post_meta( $post_id, "_eapsgt-post_list", $meta_data_target_post );

    if ( isset( $_POST["eapsgt-frequency"] ) && ( isset( $_POST["eapsgt-frequency_value"] ) ) ) {
        $meta_data_frequency_value      = sanitize_text_field( $_POST["eapsgt-frequency"] );
        $meta_data_frequency_loop_value = sanitize_text_field( $_POST["eapsgt-frequency_value"] );

        if ( $meta_data_frequency_value == "firstTimeOnly" ) {
			update_post_meta( $post_id, "eapsgt-frequency", $meta_data_frequency_value );
			update_post_meta( $post_id, "eapsgt-frequency_value", "" );
		}
		if ( $meta_data_frequency_value == "everytime" ) {
			update_post_meta( $post_id, "eapsgt-frequency", $meta_data_frequency_value );
			update_post_meta( $post_id, "eapsgt-frequency_value", "" );
		}
		if ( $meta_data_frequency_value == "loop" ) {
			update_post_meta( $post_id, "eapsgt-frequency", $meta_data_frequency_value );
			update_post_meta( $post_id, "eapsgt-frequency_value", $meta_data_frequency_loop_value );
		}
	}
}

add_action( "save_post", "eapsgt_save_settings", 10, 3 );

function eapsgt_basic_settings_save( $post_id ) {
	if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
		return;
	}
	if ( $parent_id = wp_is_post_revision( $post_id ) ) {
		$post_id = $parent_id;
	}
	$fields = [
		'eapsgt-template-popup',
		'eapsgt-width',
		'eapsgt-height',
		'eapsgt-headline',
		'eapsgt-description-text-1',
		'eapsgt-description-text-2',
		'eapsgt-description-text-3',
		'eapsgt-button-text',
		'eapsgt-background-image',
		'eapsgt-logo-url',
		'eapsgt-logo-url-2',
		'eapsgt-button-url',
	];
	foreach ( $fields as $field ) {
		if ( array_key_exists( $field, $_POST ) ) {
		    if ($_POST['eapsgt-description-text-1'] || $_POST['eapsgt-description-text-2'] || $_POST['eapsgt-description-text-3']) {
                update_post_meta($post_id, $field, sanitize_textarea_field($_POST[$field]));
            } else if ($_POST['eapsgt-background-image'] || $_POST['eapsgt-logo-url'] || $_POST['eapsgt-logo-url-2'] || $_POST['eapsgt-button-url']) {
                update_post_meta($post_id, $field, esc_url_raw($_POST[$field]));
            } else {
                update_post_meta( $post_id, $field, sanitize_text_field( $_POST[ $field ] ) );
            }
		}
	}
}

add_action( 'save_post', 'eapsgt_basic_settings_save' );
