<?php
global $wpdb;

require_once 'src/facebook.php';

if (isset($_POST['smAuth'])) {
	$id = trim($_POST['apikey']);
	$secret = trim($_POST['apisecret']);

	update_post_meta(111111113, $campaign . 'smFbID', $id);
	update_post_meta(111111113, $campaign . 'smFbSecret', $secret);

	update_post_meta(111111113, $campaign . 'smTwConsumerKey', trim($_POST['tw_consumer_key']));
	update_post_meta(111111113, $campaign . 'smTwConsumerSecret', trim($_POST['tw_consumer_secret']));
	update_post_meta(111111113, $campaign . 'smTwAccessToken', trim($_POST['tw_access_token']));
	update_post_meta(111111113, $campaign . 'smTwTokenSecret', trim($_POST['tw_token_secret']));
	update_post_meta(111111113, $campaign . 'smTwOwnerId', trim($_POST['tw_owner_id']));

	update_post_meta(111111113, $campaign . 'smIGUser', trim($_POST['ig-user']));
	update_post_meta(111111113, $campaign . 'smIGPassword', trim($_POST['ig-password']));

	update_post_meta(111111113, $campaign . 'smPnEmail', trim($_POST['pinterest_email']));
	if (!empty($_POST['pinterest_password'])) {
		update_post_meta(111111113, $campaign . 'smPnPassword', trim($_POST['pinterest_password']));
	}
	update_post_meta(111111113, $campaign . 'smPnBoard', trim($_POST['selected_pinterest_board']));

	update_post_meta(111111113, $campaign . 'smRdPage', trim($_POST['selected_reddit_subpage']));

	update_post_meta(111111113, $campaign . 'smRdID', trim($_POST['reddit_app_id']));
	update_post_meta(111111113, $campaign . 'smRdSecret', trim($_POST['reddit_app_secret']));

	$successmessage = "<strong>Settings Saved</strong>";
}

$id = get_post_meta(111111113, $campaign . 'smFbID', true);
$secret = get_post_meta(111111113, $campaign . 'smFbSecret', true);

$tw_consumer_key = get_post_meta(111111113, $campaign . 'smTwConsumerKey', true);
$tw_consumer_secret = get_post_meta(111111113, $campaign . 'smTwConsumerSecret', true);
$tw_access_token = get_post_meta(111111113, $campaign . 'smTwAccessToken', true);
$tw_token_secret = get_post_meta(111111113, $campaign . 'smTwTokenSecret', true);
$tw_owner_id = get_post_meta(111111113, $campaign . 'smTwOwnerId', true);

$smIGUser = get_post_meta(111111113, $campaign . 'smIGUser', true);
$smIGPassword = get_post_meta(111111113, $campaign . 'smIGPassword', true);

$pinterest_email = get_post_meta(111111113, $campaign . 'smPnEmail', TRUE);
$pinterest_password = get_post_meta(111111113, $campaign . 'smPnPassword', TRUE);
$selected_pinterest_board = get_post_meta(111111113, $campaign . 'smPnBoard', true);

$reddit_app_id = get_post_meta(111111113, $campaign . 'smRdID', TRUE);
$reddit_app_secret = get_post_meta(111111113, $campaign . 'smRdSecret', TRUE);
$selected_reddit_subpage = get_post_meta(111111113, $campaign . 'smRdPage', true);

echo "<br /><form method='post' action=''>
    <table>
      <tr><td>Facebook User ID: </td><td ><input type='text' name='apikey' value='" . $id . "'></td></tr>
      <tr><td>Facebook Session ID: </td><td ><input type='text' name='apisecret' value='" . $secret . "'></td></tr>
      <tr><td>&nbsp;</td></tr>

      <tr><td>Twitter Consumer Key: </td><td ><input type='text' name='tw_consumer_key' value='" . $tw_consumer_key . "'></td></tr>
      <tr><td>Consumer Secret:</td><td ><input type='text' name='tw_consumer_secret' value='" . $tw_consumer_secret . "'></td></tr>
      <tr><td>Access Token: </td><td ><input type='text' name='tw_access_token' value='" . $tw_access_token . "'></td></tr>
      <tr><td>Access Token Secret: </td><td ><input type='text' name='tw_token_secret' value='" . $tw_token_secret . "'></td></tr>
      <tr><td>Owner ID: </td><td ><input type='text' name='tw_owner_id' value='" . $tw_owner_id . "'></td></tr>
      <tr><td>&nbsp;</td></tr>

      <tr><td>Instagram User: </td><td ><input type='text' name='ig-user' value='" . $smIGUser . "'></td></tr>
      <tr><td>Instagram Password: </td><td ><input type='password' name='ig-password' value='" . $smIGPassword . "'></td></tr>
      <tr><td>&nbsp;</td></tr>

      <tr class='table-gap'><td>Pinterest Email: </td><td ><input type='text' name='pinterest_email' value='" . $pinterest_email . "' size='40'></td></tr>
      <tr><td>Pinterest Password: </td><td ><input type='password' name='pinterest_password' value='' size='40'></td></tr>

      <tr class='table-gap'><td>Reddit App ID: </td><td ><input type='text' name='reddit_app_id' value='" . $reddit_app_id . "'></td></tr>
      <tr><td>Reddit App Secret: </td><td ><input type='text' name='reddit_app_secret' value='" . $reddit_app_secret . "'></td></tr>";

if (!empty(get_post_meta(111111113, $campaign . 'smRdRefreshToken', TRUE))) {
	$subreddits = wpsmMySubreddits($campaign);
	if (isset($subreddits)) {
		echo '<tr class="table-gap"><td>Select Reddit Page</td><td><select name = "selected_reddit_subpage"><option></option>';
		$subredditsArray = array();
		foreach ($subreddits as $page) {
			// $url = $page['data']['url'];
			$name = trim($page['data']['display_name']);

			if ($name == $selected_reddit_subpage) {
				echo "<option value = '" . $name . "' selected = 'selected'>" . $name . "</option>";
			} else {
				echo "<option value = '" . $name . "'>" . $name . "</option>";
			}

			array_push($subredditsArray, $name);
		}
		echo '</select></td></tr>';
		update_post_meta(111111113, $campaign . 'smTempSubReddits', json_encode($subredditsArray));
	} else {
		echo '<tr class="table-gap"><td>Reddit Token Expired</td></tr>';
		update_post_meta(111111113, $campaign . 'smTempSubReddits', json_encode(array()));
	}
}
if (!empty($pinterest_email) && !empty($pinterest_password)) {
	$response = wpsmGetPinterestBoard($pinterest_email, $pinterest_password);

	echo '<tr class="table-gap"><td>Select Pinterest Board</td>';
	if (is_array($response)) {
		echo '<td><select name="selected_pinterest_board"><option></option>';
		foreach ($response as $key => $boardName) {
			if ($selected_pinterest_board == $key) {
				echo "<option value='" . $key . "' selected='selected'>" . $boardName . "</option>";
			} else {
				echo "<option value='" . $key . "'>" . $boardName . "</option>";
			}
		}
		echo '</select></td>';
		update_post_meta(111111113, $campaign . 'smTempPinterestBoards', json_encode($response));
	} else {
		echo "<td>$response</td>";
	}
	echo '</tr>';
}

echo "<tr><td>";
echo "</td><td><input name='smAuth' type='submit' value='Save' class = 'button button-primary'></td>";

if (!empty($reddit_app_id)) {
	echo "<td><a href = '" . plugin_dir_url(__FILE__) . "reddit-oauth.php?campaign=$campaign" . "' target='_blank'>Auth Reddit</a></td>";
}

if (!empty($smIGUser) && !empty($smIGPassword)) {
  $instagram_cron = plugin_dir_url(__FILE__) . 'instagramcron.php';
  echo "<td><a href = '" . $instagram_cron . "' target='_blank'>Auth Instagram</a></td>";
}

echo "</tr>";

echo '<tr><td></td><td>' . $successmessage . '</td></tr>
    </table>
  </form><hr />';

$redditcallback = plugin_dir_url(__FILE__) . "reddit-oauth.php?campaign=$campaign";

echo '<h2>Valid OAuth Redirect URIs</h2>';
echo "<table>
    <tr><td>Copy following URL to your Reddit app callback field</td><td colspan='2'><code>" . $redditcallback . "</code></td></tr>
      <tr></tr>
  </table>";
