<?php

/*
 * Shortcodes for cybersoldier
 */

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

new CyberSoldier_ShortCodes();
class CyberSoldier_ShortCodes
{

	public function __construct()
	{
		add_shortcode('cybersoldier_user_page', 	[$this, 'cybersoldier_user_page']);
		add_shortcode('cybersoldier_player_page', 	[$this, 'cybersoldier_player_page']);
		add_shortcode('cybersoldier_random_line', 	[$this, 'cybersoldier_random_battle_line']);
		add_shortcode('cybersoldier_battles_list', 	[$this, 'cybersoldier_battles_list']);
		add_shortcode('cybersoldier_soldiers_list', [$this, 'cybersoldier_soldiers_list']);
	}

	/**
	 * The shortcode to display the user settings
	 */
	function cybersoldier_user_page()
	{
		$current_user = wp_get_current_user();
		if (in_array('administrator', (array) $current_user->roles) || in_array('player', (array) $current_user->roles)) {
			wp_enqueue_style("cybersoldier-bootstrap");
			$role = get_role('player');
			$all_types = CyberSoldier_User::cybersoldier_character_items();
			$user_id = get_current_user_id();
			$body_color = get_user_meta($user_id, "cybersoldierbody_color", true);
			//error_log("$user_id body_color: " . $body_color);
			$body_color = $body_color ? $body_color : "EBA16C";
			$return_str = '<div class="cybersoldier_edit bootstrap-wrapper">
			<h2>Cybersoldier</h2>
				<p>To edit your userinformation, such as Username etc, go <a href="' . get_edit_user_link() . '">here</a>. That is the usual user-page for Wordpress with some Cybersoldier-things added.</p>
			<div class="cs-boxes-row">
			<div class="cs-boxes-col">
				<div id="svgintro" class="hasSVG">
					<object data="' . CYBERSOLDIER_PLUGIN_URL . '/character/body1.svg" type="image/svg+xml" id="c_body1">
						<!-- Not working -->
					</object>
				</div>
			</div>
			<div class="cs-boxes-col-twothird">
				<h2>cybersoldier items</h2>
					<div class="cs-boxes-row cs-boxes-col">
					<div class="items_top_icon" id="topicon_body">Body</div>';

			foreach ($all_types as $type => $items) {
				$type_esc = esc_attr($type);
				$return_str .= '<div class="items_top_icon" id="topicon_' . strtolower($type_esc) . '">' . $type_esc . '</div>';
			}

			$return_str .= '
				</div>
				<div class="ci_gray_boxes_wrap">
						<div class="ci_icons_box" id="ci_body" style="display:block;">
							<h4>Body</h4>
							<input class="jscolor" id="cybersoldierbody_color" value="' . esc_attr($body_color) . '">
							<div class="ci_icon"><img src="' . CYBERSOLDIER_PLUGIN_URL . '/character_icons/body1.png" id="cybersoldierbody_1" /></div>
						
					</div>';

			foreach ($all_types as $type => $items) :
				$type_lw = esc_attr(strtolower($type));
				$user_selected_color = get_user_meta($user_id, "cybersoldier" . $type_lw . "_color", true);
				$user_selected_color = $user_selected_color ? $user_selected_color : "FFFFFF";
				$user_selected_item = get_user_meta($user_id, "cybersoldier" . $type_lw, true);

				$return_str .= '<div class="cs-boxes-row">
							<div class="ci_icons_box" id="ci_' . $type_lw . '">
								<h4>' . esc_attr($type) . '</h4>
								<input class="jscolor" id="' . "cybersoldier" . $type_lw . '_color" value="' . esc_attr($user_selected_color) . '">';

				foreach ($items as $item) {
					$selected = $user_selected_item == $item->id ? "selected" : "";
					$icon = esc_attr($item->icon);
					$outid = esc_attr("cybersoldier" . $item->type . "_" . $item->id);
					$return_str .= '<div class="ci_icon ' . $selected . '"><img src="' . CYBERSOLDIER_PLUGIN_URL . '/character_icons/' . $icon . '" id="' . $outid . '" /></div>';
				}
				$return_str .= '<div class="ci_icon"><img src="' . CYBERSOLDIER_PLUGIN_URL . '/character_icons/remove.png" id="' . $type_lw . '_remove" /></div>';

				$return_str .= '</div></div>';

			endforeach;

			$return_str .= '</div>
			<h2>Your battles</h2>
			<div class="cs-boxes-row">';
			$battles = CyberSoldier_User::cybersoldier_users_battles($current_user->ID);
			foreach ($battles as $battle) :

				$start_user_info = CyberSoldier_User::cybersoldier_user_info($battle->post_author);
				$start_user_name = isset($start_user_info["user"]->display_name) ? esc_attr($start_user_info["user"]->display_name) : "";
				$battle_user_id = get_post_meta($battle->ID, "invited_user", true);
				$battle_user_info = CyberSoldier_User::cybersoldier_user_info($battle_user_id);
				$battle_user_name = isset($battle_user_info["user"]->display_name) ? esc_attr($battle_user_info["user"]->display_name) : "";
				$battle_ends = CyberSoldier_BattlePostType::cybersoldier_when_battle_ends($battle->ID);
				$who_went_last = CyberSoldier_BattlePostType::cybersoldier_who_went_last(get_the_ID());
				$is_in_battle = CyberSoldier_User::cybersoldier_in_battle($battle->ID, $current_user->ID);
				$my_turn = $is_in_battle && $current_user->ID != $who_went_last ? true : false;
				$my_turn_str = "";

				if ($my_turn && $battle_ends != "battle over")
					$my_turn_str = __("It's your turn", "cybersoldier");
				$battle_url = get_permalink($battle->ID);
				$return_str .= '<div class="cs-boxes-col">
				<div class="yourbattelsbox">
				<h4><a href="' . esc_attr($battle_url) . '">' . esc_attr($battle->post_title) . '</a></h4>
				<p><span class="yourturn">' . esc_attr($my_turn_str) . '</span><br />
				<strong>' . esc_attr($battle_ends) . '</strong>
				<br />' . esc_attr($battle->post_content) . '</p>
				<strong>' . $start_user_name . ' VS ' . $battle_user_name . '</strong>
				</div>
			</div>';
			endforeach;

			$return_str .= '</div>
			<!--<br /><br /><h2>Settings</h2>
				cybersoldier.com ID
				För att koppla sin gubbe till cs.com skicka upp met populära quotes, wordlrank, mest likes? Högst total score.
				Skicka top 5 quotes.
				http: on site url... ie MyOwnRapbattles.com
			-->
		</div>
		</div>
			<canvas id="cscanvas" width="250" height="400"></canvas>
 			<img id="user_image" />
		</div>';
		} else {
			$return_str =  __("Sorry bra, no matter what you girl says, you're not a playah...", "cybersoldier");
		}
		return $return_str;
	}


	/**
	 * The player page ie, the page show to the world, that is more often thant not just named cybersoldier.
	 */
	function cybersoldier_player_page()
	{
		wp_enqueue_style("cybersoldier-bootstrap");
		$the_user_id = get_query_var("csid", 0);
		$current_user = wp_get_current_user();
		if ($the_user_id == false || !is_numeric($the_user_id))
			return __("Player does not exist");

		$battle_user_info = CyberSoldier_User::cybersoldier_user_info($the_user_id);
		$user_description = get_user_meta($the_user_id, "description", true);

		$returnstr = "";
		$returnstr .= "<div class='cybersoldier_info_box'>
			<div class='cs-boxes-row'>
				<div class='cs-boxes-col-onethird'>
					<div class='cybersoldier_image'>
						<img src='" . esc_url($battle_user_info["image"]) . "' />
					</div>
				</div>";
		$returnstr .= "<div class='cs-boxes-col-twothird'>
		<h2>" . esc_attr($battle_user_info["user"]->display_name) . "</h2>" .
			esc_attr($user_description) . "<br /><br />
		<h4>Battles</h4>" . $battle_user_info["battle_list"] . "<br />";
		if (is_user_logged_in() && $the_user_id != $current_user->ID) {
			$returnstr .= "<a class='battle_me_bitch_button' href='"
				. admin_url('post-new.php?post_type=battle&csid='
					. esc_attr($the_user_id) . "'>Battle "
					. esc_attr($battle_user_info["user"]->display_name) . "</a>");
		} elseif ($the_user_id == $current_user->ID) {
			$returnstr .= "It's you...";
		} else {
			$returnstr .= sprintf(__("You have to be logged in to battle %s", "cybersoldier"), esc_attr($battle_user_info["user"]->display_name));
		}
		$returnstr .= "</div></div>";

		return $returnstr;
	}

	/**
	 * Random battle line
	 */
	function cybersoldier_random_battle_line($atts)
	{
		wp_enqueue_style("cybersoldier-bootstrap");
		$attributes = shortcode_atts(array(
			'number_of_lines' => 1,
			'battle_id' => false,
			'user_id' => false
		), $atts);

		$line = CyberSoldier_BattlePostType::cybersoldier_random_line($attributes["number_of_lines"], $attributes["battle_id"], $attributes["user_id"]);
		return $line;
	}

	/**
	 * Get a list of battles.
	 */
	function cybersoldier_battles_list($atts)
	{
		wp_enqueue_style("cybersoldier-bootstrap");
		$attributes = shortcode_atts(array(
			'user_id' => false,
			'numberposts' => 1,
			'content' => false,
			'images' => false,
		), $atts);

		$args = array(
			'numberposts' => $attributes["numberposts"],
			'offset' => 0,
			'category' => 0,
			'orderby' => 'post_date',
			'order' => 'DESC',
			'include' => '',
			'exclude' => '',
			'meta_key' => '',
			'meta_value' => '',
			'post_type' => 'battle',
			'post_status' => 'publish',
			'suppress_filters' => true
		);

		$recent_posts = wp_get_recent_posts($args, ARRAY_A);
		$return_str = "";
		foreach ($recent_posts as $battle) :
			$battle_id = $battle["ID"];
			$users = CyberSoldier_User::cybersoldier_users_in_battle($battle_id, false);
			$permalink = get_permalink($battle_id);
			$start_user = $users["start_user"];
			$start_user_color = get_user_meta($start_user["user"]->ID, "cybersoldierbody_color", true);
			$invited_user = $users["invited_user"];
			if ($invited_user)
				$invited_user_color = get_user_meta($invited_user["user"]->ID, "cybersoldierbody_color", true);
			else
				$invited_user_color = "";
			$show_images = $attributes["images"] == "true" ? true : false;
			$show_content = $attributes["content"] == "true" ? true : false;

			$return_str .= '<div class="cs-boxes-row battle_list_box bootstrap-wrapper clearfix">';
			if ($show_images) :
				$return_str .= '<div class="cs-boxes-col-onethird">
				<img src="' . ($start_user["image"] != "" ? esc_url($start_user["image"]) : CYBERSOLDIER_PLUGIN_URL . '/assets/nopic.png') . '" />
			</div>';
				if (wp_is_mobile()) {
					$return_str .= '<div class="cs-boxes-col-onethird">
				<img src="' . ($invited_user && $invited_user["image"] != "" ? esc_url($invited_user["image"]) : CYBERSOLDIER_PLUGIN_URL . '/assets/nopic.png') . '" />
			
		</div>';
				}
			endif;
			$return_str .= '<div class="' . ($show_images ? "cs-boxes-col-twothird" : "cs-boxes-col") . '"><h4><a href="' . esc_url($permalink) . '">' . esc_attr($battle["post_title"]) . '</a></h4><p>';
			$return_str .= '<div class="battle_list_user_title" style="color: #' . esc_attr($start_user_color) . '">' . esc_attr($start_user["user"]->display_name) . '</div>';
			if ($invited_user)
				$return_str .= 'vs <div class="battle_list_user_title" style="color: #' . esc_attr($invited_user_color) . '">' . esc_attr($invited_user["user"]->display_name) . '</div>';
			else
				$return_str .= 'vs <div class="battle_list_user_title">N/A</div>';

			if ($show_content)
				$return_str .= $battle["post_content"];

			$return_str .= "<br /><a href='" . esc_url($permalink) . "'>" . __("Check out this battle", "cybersoldier") . "</a>";

			$return_str .= '</div>';
			if ($show_images && !wp_is_mobile()) :
				$return_str .= '<div class="cs-boxes-col-onethird">
					<img src="' . ($invited_user && $invited_user["image"] != "" ? esc_url($invited_user["image"]) : CYBERSOLDIER_PLUGIN_URL . '/assets/nopic.png') . '" />
				</div>';
			endif;
			$return_str .= '</div>';

		endforeach;
		return $return_str;
	}


	/**
	 * TO get a list of cybersoldiers for like the first page on cybersoldier.com
	 */
	function cybersoldier_soldiers_list($atts)
	{
		wp_enqueue_style("cybersoldier-bootstrap");
		$attributes = shortcode_atts(array(
			'numberposts' => -1,
			'content' => false,
			'admin_is_player' => get_option('cybersoldier_admin_is_player', false),
			'images' => true,
			'hide_no_image' => true,
			'random' => true,
		), $atts);

		$return_str = "<div class='cs-boxes-row nowrap'>";

		$role_in = array("player");
		if ($attributes["admin_is_player"])
			$role_in[] = "administrator";

		$args = array('role__in' => $role_in);
		$users = get_users($args);
		$transparency = 0.1;
		$from_top = 0;
		$counter = 0;
		$rotation = 30;

		foreach ($users as $user) :
			$user_info = CyberSoldier_User::cybersoldier_user_info($user->ID);
			$show_images = $attributes["images"] == "true" ? true : false;
			$show_content = $attributes["content"] == "true" ? true : false;
			$user_color = get_user_meta($user->ID, "cybersoldierbody_color", true);
			if ($attributes["hide_no_image"] && $user_info["image"] == "") {
				continue;
			}

			if ($attributes["random"]) {
				if ($counter % 7 == 0) {
					$transparency += 0.1;
					$from_top += 30;
					$rotation -= $rotation>0 ? 5 : 0;
				}
				$width = $transparency * 40;
				$rand_str = "width: $width%; transform: rotate(" . rand(-$rotation, $rotation) . "deg); position: absolute; top: " . $from_top . "px; left: " . rand(-10, 80) . "%;";
				$img_css = "filter: brightness($transparency);";
				$return_str .= '<div class="cs-boxes-col-small"><div class="users_list_box" style="' . $rand_str . '">';
				$return_str .= '<a href="' . esc_url($user_info["user_url"]) . '"><img src="' . esc_url($user_info["image"]) . '" style="' . $img_css . '" /></a>';
				
			} else {
				$return_str .= '<div class="cs-boxes-col-small"><div class="users_list_box">';

				if ($show_images && $user_info["image"] != "")
					$return_str .= '<a href="' . esc_url($user_info["user_url"]) . '"><img src="' . esc_url($user_info["image"]) . '" /></a>';
				else if ($show_images)
					$return_str .= '<a href="' . esc_url($user_info["user_url"]) . '"><img src="' . CYBERSOLDIER_PLUGIN_URL . '/assets/nopic.png" /></a>';

				$return_str .= '<h4 style="color: #' . esc_attr($user_color) . '"><a href="' . esc_url($user_info["user_url"]) . '">' . esc_attr($user->display_name) . '</a></h4>';
			}
			$return_str .= '</div>
			</div>';

			$counter++;

		endforeach;
		$return_str .= "</div'>";

		return $return_str;
	}
}
