<?

// RollupPrefs

function testKey($key) {
	// https://developers.google.com/maps/documentation/geocoding/start
	$streamContext = stream_context_create([ "ssl" => ["verify_peer" => false, "verify_peer_name" => false] ]);
	$result = json_decode(file_get_contents("https://maps.googleapis.com/maps/api/geocode/json?key=".$key."&latlng=0,4", false, $streamContext));
	if ($result->status == "REQUEST_DENIED") { return 0; } else { return 1; }
	}

function makeWindowString($p) {

	$conf = "
	# Set window title
	*.title = Preferences
	*.floating = 1
	
	# Allowed Filetypes
	allowed.type = textfield
	allowed.mandatory = 1
	allowed.label = Allowed filetypes
	allowed.default = ".implode(", ",$p['allowed'])."
	allowed.placeholder = jpg, jpeg, png, mov, mp4, m4v, dng, heic
	allowed.width = 400
	allowed.tooltip = Process files matching these extensions

	# Photo template
	pt.type = textfield
	pt.mandatory = 1
	pt.label = Photo renaming template
	pt.default = ".$p['pt']."
	pt.placeholder = %DTO_%MODEL_%PLACE_%SW_%FILE.%EXT
	pt.width = 400
	pt.tooltip = Rename photo files according to this template

	# Video template
	vt.type = textfield
	vt.mandatory = 1
	vt.label = Video renaming template
	vt.default = ".$p['vt']."
	vt.placeholder = %DTO_%DUR_%PLACE_%SW_%FILE.%EXT
	vt.width = 400
	vt.tooltip = Rename video files according to this template
	
	# API Key
	key.type = textfield
	key.label = Google Geocoding API key
	key.default = ".$p['key']."
	key.placeholder = xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
	key.width = 400
	key.tooltip = An API key is required for reverse-geocoding

	# Limit
	limit.type = textfield
	limit.mandatory = 1
	limit.label = Max files to process without warning
	limit.default = ".$p['limit']."
	limit.width = 80

	# Maxlength
	maxlength.type = textfield
	maxlength.mandatory = 1
	maxlength.label = Max input filename length
	maxlength.tooltip = Use this value to skip files that have already been renamed
	maxlength.default = ".$p['maxlength']."
	maxlength.width = 80

	# Dry run
	dry_run.type = checkbox
	dry_run.label = Dry run (don't rename files)
	dry_run.default = ".$p['dry_run']."

	# Buttons
	#gb.type = button
	#gb.label = Detect Key...
	cb.type = cancelbutton
	db.type = defaultbutton
	db.label = Save
	";
	
	return $conf;
	
	}

// Read Prefs

$prefs_file = "/Users/".get_current_user()."/Library/Preferences/org.profiteroles.Rollup.php";
$p = unserialize(file_get_contents($prefs_file));

// Launch Pashua and parse results

$path = __DIR__."/Pashua.app/Contents/MacOS/Pashua";
$raw = shell_exec("echo ".escapeshellarg(makeWindowString($p))." | ".escapeshellarg($path)." - ");
$result = array();
foreach (explode("\n", $raw) as $line) {
	preg_match('/^(\w+)=(.*)$/', $line, $matches);
    if (empty($matches) or empty($matches[1])) {
    	continue;
        }
    $result[$matches[1]] = $matches[2];
    }
    
// User cancelled

if (@$result['cb']) { echo 1; die; }

// Write Prefs

$result['allowed'] = explode(", ",$result['allowed']);
file_put_contents($prefs_file,serialize($result));

// Test API Key

if (@$result['key'] && ($result['key'] != $p['key'])) {
	echo testKey($result['key']);
	} else {
	echo 1;
	}

?>