<?php

/**
 * Holding all the AJAX calls on the admin side
 */

if( ! defined( 'ABSPATH' ) ) {
    return;
}

add_action( 'wp_ajax_giveasap_activate_integration', 'giveasap_activate_integration_ajax' );
add_action( 'wp_ajax_giveasap_deactivate_integration', 'giveasap_deactivate_integration_ajax' );

/**
 * Activating the Integration through AJAX
 * @return void 
 */
function giveasap_activate_integration_ajax() {

    if( ! isset( $_POST['nonce'] ) 
        || ! wp_verify_nonce( $_POST['nonce'], 'gasap-admin' ) ) {
        
        wp_send_json_error( array( 'message' => __( 'Something went wrong!', 'giveasap' ) ) );
        die();
    }

    if( ! isset( $_POST['integration'] ) ) {
        wp_send_json_error( array( 'message' => __( 'No Integration Sent', 'giveasap' ) ) );
        die();
    }

    $integration = $_POST['integration'];

    $active_integrations = get_option( 'giveasap_active_integrations', array() );
    
    if( ! isset( $active_integrations[ $integration ] ) ) {
        $integrations = giveasap_get_integrations(); 
        
        if( isset( $integrations[ $integration ] ) ) {
            $active_integrations[ $integration ] = $integrations[ $integration ];
            
            $integration_settings = get_option( $integration, array() );
            if( isset( $integration_settings['active'] ) && '0' == $integration_settings['active'] ) {
                $integration_settings['active'] = '1';
            }
            update_option( $integration, $integration_settings );
            do_action( 'giveasap_' . $integration . '_integration_activated' );
            update_option( 'giveasap_active_integrations', $active_integrations );
            wp_send_json_success( array( 'message' => __( 'Activated', 'giveasap' ) ) );
            die();
        }
        
    } else {
        wp_send_json_success( array( 'message' => __( 'Already Activated', 'giveasap' ) ) );
        die();
    }

    
    wp_send_json_error( array( 'message' => __( 'Nothing Happened', 'giveasap' ) ) );
    die();
}

/**
 * Deactivating the Integration through AJAX
 * @return void 
 */
function giveasap_deactivate_integration_ajax() {

    if( ! isset( $_POST['nonce'] ) 
        || ! wp_verify_nonce( $_POST['nonce'], 'gasap-admin' ) ) {
        
        wp_send_json_error( array( 'message' => __( 'Something went wrong!', 'giveasap' ) ) );
        die();
    }

    if( ! isset( $_POST['integration'] ) ) {
        wp_send_json_error( array( 'message' => __( 'No Integration Sent', 'giveasap' ) ) );
        die();
    }

    $integration = $_POST['integration'];

    $active_integrations = get_option( 'giveasap_active_integrations', array() );
    
    if( isset( $active_integrations[ $integration ] ) ) {
        unset( $active_integrations[ $integration ] );
        $integration_settings = get_option( $integration, array() );

        if( isset( $integration_settings['active'] ) && '1' == $integration_settings['active'] ) {
            $integration_settings['active'] = 0;
        }
        update_option( $integration, $integration_settings );
        do_action( 'giveasap_' . $integration . '_integration_deactivated' );
        update_option( 'giveasap_active_integrations', $active_integrations );
        wp_send_json_success( array( 'message' => __( 'Deactivated', 'giveasap' ) ) );
        die();
    } else {
        wp_send_json_error( array( 'message' => __( 'Not Activated', 'giveasap' ) ) );
        die();
    }

    wp_send_json_error( array( 'message' => __( 'Nothing Happened', 'giveasap' ) ) );
    die();
}

add_action( 'wp_ajax_sg_get_giveaway_form_fields', 'sg_get_giveaway_form_fields_ajax' );

/**
 * Get form fields for a Giveaway.
 *
 * @return void
 */
function sg_get_giveaway_form_fields_ajax() {
	if( ! isset( $_GET['nonce'] )
	    || ! wp_verify_nonce( $_GET['nonce'], 'gasap-admin' ) ) {

		wp_send_json_error( array( 'message' => __( 'Something went wrong!', 'giveasap' ) ) );
		die();
	}

	if( ! isset( $_GET['giveaway'] ) ) {
		wp_send_json_error( array( 'message' => __( 'No Giveaway provided', 'giveasap' ) ) );
		die();
	}

	$giveaway = $_GET['giveaway'];

	$form_fields = sg_get_form_fields( $giveaway );

	if ( $form_fields ) {
		$html = '';
		ob_start();
		foreach( $form_fields as $field ) {
			sg_render_form_field( $field );
		}
		$html = ob_get_clean();
		wp_send_json_success( $html );
		die();
	}

	wp_send_json_error( array( 'message' => __( 'Nothing Happened', 'giveasap' ) ) );
	die();
}

add_action( 'wp_ajax_sg_get_available_giveaways', 'sg_get_available_giveaways_ajax' );

/**
 * Get available Giveaways.
 *
 * @return void
 */
function sg_get_available_giveaways_ajax() {

    $giveaways = sg_get_giveaways( array(
        'post_status' => array( 'publish', 'giveasap_ended', 'giveasap_winners', 'giveasap_notified' )
    ));

    if ( $giveaways ) {
        wp_send_json_success( $giveaways );
    } else {
        wp_send_json_error( __( 'No Giveaways found. Please create some first', 'giveasap' ) );
    }
    wp_die();
}

add_action( 'wp_ajax_sg_get_giveaways', 'sg_get_giveaways_ajax' );

/**
 * Get available Giveaways.
 *
 * @return void
 */
function sg_get_giveaways_ajax() {

	if ( ! isset( $_REQUEST['search'] ) ) {
		wp_send_json_error();
	}

	$giveaways = sg_get_giveaways( array(
		'post_status' => array( 'publish', 'giveasap_ended', 'giveasap_winners', 'giveasap_notified' ),
		's' => isset( $_REQUEST['search'] ) ? $_REQUEST['search'] : ''
	));

	if ( $giveaways ) {
		wp_send_json_success( $giveaways );
	} else {
		wp_send_json_error( __( 'No Giveaways found. Please create some first', 'giveasap' ) );
	}
	wp_die();
}
