<?php
/**
 * @tag my_ninja_forms_processing
 * @callback my_ninja_forms_processing_callback
 */
add_action( 'my_ninja_forms_processing', 'my_ninja_forms_processing_callback' );

/**
 * @param $form_data array
 * @return void
 */

function my_ninja_forms_processing_callback( $form_data ) {
	if ( !is_user_logged_in() ) {
		return 0;
	}	
	call_user_func('dump_to_file_local', '/var/www/lab/newfile.txt', 'my_ninja_forms_processing_callback...', 'clear');

	// FastSnipe Object
	$fastsnipeObj = array(
		1 => array(
			'price' => 2250,
			'day_end' => 7,
			'color' => '#f8dd4d',
		),
		2 => array(
			'price' => 1800,
			'day_end' => 14,
			'color' => 'hotpink',
		),
		3 => array(
			'price' => 1100,
			'day_end' => 21,
			'color' => '#fd9f02',
		),
		4 => array(
			'price' => 500,
			'day_end' => 35,
			'color' => '#87cefa',
		),
	);			
	
    $form_id       = $form_data[ 'form_id' ];
	$form_settings = $form_data[ 'settings' ];
    $form_title    = $form_data[ 'settings' ][ 'title' ];
    $form_fields   =  $form_data[ 'fields' ];	
	
    foreach( $form_fields as $field ){
        $field_id    = $field[ 'id' ];
        $field_key   = $field[ 'key' ];
        $field_value = $field[ 'value' ];        

        if( 'ich_brauche_einen_termin_1579378475182' == $field[ 'key' ] ) {						
			if( $field_value == 'sofort' ) {
				$switch_termintype = 'sofort';				
			}
			else if ( $field_value == 'zeitraum' ) {
				$switch_termintype = 'zeitraum';								
			}
        }
		else if( 'wochentag_start_text' == $field[ 'key' ] ) {
			$wochentag_start_text = $field_value;
		}
		
    }  
	call_user_func('dump_to_file_local', '/var/www/lab/newfile.txt', 'switch_termintype: ' . $switch_termintype);
	
	if ( $switch_termintype == 'sofort' ) {
		[$bool, $user_balance, $price, $is_premium] = subtract_fastcoins_by_start_date_local( $fastsnipeObj, 'sofort' );
	}
	else if ( $switch_termintype == 'zeitraum' ) {
		call_user_func('dump_to_file_local', '/var/www/lab/newfile.txt', 'wochentag_start_text: ' . $wochentag_start_text);
		[$bool, $user_balance, $price, $is_premium] = subtract_fastcoins_by_start_date_local( $fastsnipeObj, 'zeitraum', $wochentag_start_text );		
	}	
}

function subtract_fastcoins_by_start_date_local( $fastsnipeObj, $termintype_switch, $wochentag_start_text = null ) {	
	[$bool, $user_balance, $price, $is_premium] = check_premium_local( $fastsnipeObj, $termintype_switch, $wochentag_start_text );

	if ( $bool ) {
		call_user_func('dump_to_file_local', '/var/www/lab/newfile.txt', 'transfer legit. subtracting amount...');			
		$user_id = get_current_user_id();
		mycred_subtract( 'fastsnipe', $user_id, $price, 'Forms booking completed' );
		call_user_func('dump_to_file_local', '/var/www/lab/newfile.txt', 'subtracted ' . $price . ' from ' . $user_balance);
	}
	else if ( $bool === 0 ) {
		call_user_func('dump_to_file_local', '/var/www/lab/newfile.txt', 'not enough coins for transfer');
	}
	else if ( $bool === null ) {
		call_user_func('dump_to_file_local', '/var/www/lab/newfile.txt', 'no premium day');
	}
	
	return $rArr;
}

function DateDiffFromNow_local($d2) {
	$d1 = new DateTime();
	$d2 = new DateTime($d2);
	$dDiff = $d1->diff($d2);
	return $dDiff->format('%r%a');
}  

function check_premium_local( $fastsnipeObj, $fswitch, $wochentag_start_text ) {
	$user_id = get_current_user_id();
	$user_balance = mycred_get_users_balance( $user_id );
	call_user_func('dump_to_file_local', '/var/www/lab/newfile.txt', 'Balance: ' . $user_balance);
    
	if ( $fswitch == 'zeitraum' ) {
		$i = DateDiffFromNow_local($wochentag_start_text);	
	}
	else if ( $fswitch == 'sofort' ) {
		$i = 0;
	}
    call_user_func('dump_to_file_local', '/var/www/lab/newfile.txt', 'i: ' . $i);
    
    if( $price = get_premium_price_local($fastsnipeObj, $i) ) {
		$is_premium = 1;
		call_user_func('dump_to_file_local', '/var/www/lab/newfile.txt', 'price: ' . $price);
        if ( can_afford_local($user_balance, $price) ) {
            $bool = 1;
        }
        else {
             $bool = 0;
        }
    }
    else {
		$is_premium = 0;
        $bool = null;
    }
    
    return array($bool, $user_balance, $price, $is_premium);
}

function can_afford_local($user_balance, $price) {
    $can_afford = 0;
    $diff = $user_balance-$price;
	call_user_func('dump_to_file_local', '/var/www/lab/newfile.txt', 'price diff: ' . $diff);
    if( $diff >= 0 ) {
        $can_afford = 1;
    }
    
    return $can_afford;
}

function get_premium_price_local($fastsnipeObj, $i) {
    $price = 0;
    foreach ($fastsnipeObj as $key => $value) {
        $day_end = $fastsnipeObj[$key]['day_end'] - 2;
        if ($i <= $day_end) {
            $price = $fastsnipeObj[$key]['price'];
            break;
        }
    }
    return $price;
}

function dump_to_file_local($file, $data, $method = '') {
	if ($method == 'clear') {
		$f = @fopen($file, "r+");
		if ($f !== false) {
			ftruncate($f, 0);
			fclose($f);
		}

		$timestamp = "\n[" . date('m/d/Y h:i:sa', time()) . "]\n";
		file_put_contents($file, $timestamp, FILE_APPEND | LOCK_EX);
	}

	if (is_array($data) || is_object($data)) {
		$data = print_r($data, true);
	}

	file_put_contents($file, $data.PHP_EOL , FILE_APPEND | LOCK_EX);
}