/*
 * +================================================================================+
 *      __  ___  ______________  __  __  ______________   ____
 *     / / / / |/ /  _/_  __/\ \/ / / / / /_  __/  _/ /  / __/
 *    / /_/ /    // /  / /    \  / / /_/ / / / _/ // /___\ \
 *    \____/_/|_/___/ /_/     /_/  \____/ /_/ /___/____/___/
 *
 * +================================================================================+
 *
 *    "THE BEER-WARE LICENSE"
 *    -----------------------
 *
 *    As long as you retain this notice you can do whatever you want with this
 *    stuff. However it is expressly forbidden to sell or commercially distribute
 *    Unity Utils or parts of it apart from your games.
 *    If you meet any of the authors some day, and you think this stuff is
 *    worth it, you can buy them a beer in return.
 *
 *    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 *    THE SOFTWARE.
 *
 * +================================================================================+
 *
 *    Authors:
 *    Michael Müller (mik@mikmueller.ch)
 *
 */

using UnityEngine;
using System.Collections;

public class MobileTargetPlatform : ITargetPlatform {

	private float mouseDeltaX, mouseDeltaY;

	private float mouseSens = 0.3f;

	private int xAxisSetting = 1;
	private int yAxisSetting = 1;
	public float deadZone = 0.2f;

	private OnScreenStick stickLeft, stickRight;

	public void onStart() {
		
		stickLeft = GameObject.Find ("StickLeft").GetComponent<OnScreenStick>();
		stickRight = GameObject.Find ("StickRight").GetComponent<OnScreenStick>();

		updateCameraSensitivity();
	}

	public void onUpdate() {
		
	}
	
	
	public void onDeviceChange() {
		
	}
	
	public bool isIdle() {
		if (stickLeft.Axis.x < deadZone && stickLeft.Axis.x > -deadZone && stickLeft.Axis.y < deadZone && stickLeft.Axis.y > -deadZone) {
			return true;
		} else {
			return false;
		}
	}
	
	public bool hasAxisInput(float deadZone) {
		if (stickLeft.Axis.x < deadZone && stickLeft.Axis.x > -deadZone && stickLeft.Axis.y < deadZone && stickLeft.Axis.y > -deadZone) {
			return false;
		} else {
			return true;
		}
	}
	
	public bool hasAxisInputRight(float deadZone) {
		if (stickRight.Axis.x < deadZone && stickRight.Axis.x > -deadZone && stickRight.Axis.y < deadZone && stickRight.Axis.y > -deadZone) {
			return false;
		} else {
			return true;
		}
	}
	
	void Update() {
		
	}
	
	public void updateCameraSensitivity() {
	}
	
	

	
	private bool getAnyTouch() {
		Touch[] t = Input.touches;
		bool res = false;
		
		if (t.Length > 0) {
			res = true;
		}
		
		return res;
		
	}
	
	
	
	
	
	private bool getRightScreenTouch() {
		
		Touch[] t = Input.touches;
		bool res = false;
		
		foreach (Touch touch in t) {
			if(Mathf.Abs(touch.position.x) >= Screen.width/2) {
				res = true;
			} else {
				res = false;
			}
		}
		return res;
	}
	
	private bool getLeftScreenTouch() {
		
		Touch[] t = Input.touches;
		bool res = false;
		
		foreach (Touch touch in t) {
			if(Mathf.Abs(touch.position.x) < Screen.width/2) {
				res = true;
			} else {
				res = false;
			}
		}
		return res;
	}
	
	
	private bool IsSingleTapRight() {
		Touch[] t = Input.touches;
		foreach (Touch touch in t) {
			if(touch.phase == TouchPhase.Ended) {
				if(!hasAxisInputRight(0.2F) && Mathf.Abs(touch.position.x) >= Screen.width/2) {
					return true;
				}
			}
		}
		return false;
	}
	
	private bool IsSingleTapLeft() {
		Touch[] t = Input.touches;
		foreach (Touch touch in t) {
			if(touch.phase == TouchPhase.Ended) {
				if(!hasAxisInput(0.2F) && Mathf.Abs(touch.position.x) < Screen.width/2) {
					return true;
				}
			}
		}
		return false;
	}



	public void updateAxis() {
		if(PlayerPrefs.GetInt("xaxis") == 0) {
			xAxisSetting = 1;
		} else if (PlayerPrefs.GetInt("xaxis") == 1) {
			xAxisSetting = -1;
		}

		if(PlayerPrefs.GetInt("yaxis") == 0) {
			yAxisSetting = 1;
		} else if (PlayerPrefs.GetInt("yaxis") == 1) {
			yAxisSetting = -1;
		}
	}



	//Interface implementations
	public string PlatformName {			get {			return "Mobile";								}	}
	public float AxisHorizontal {			get {			return stickLeft.Axis.x;				}	}
	public float AxisVertical {				get {			return stickLeft.Axis.y;				}	}


	public float SecondaryAxisHorizontal {	get {			return stickRight.Axis.x;			}	}
	public float SecondaryAxisVertical {	get {			return stickRight.Axis.y;				}	}
	public bool CameraReset {				get {			return Input.GetKey(KeyCode.Keypad0) || Input.GetMouseButton(2);			}	}

	public float ZoomInOut {				get {			return Input.GetAxis("Mouse ScrollWheel");		}	}

	public bool Jump {						get {			return IsSingleTapRight(); 				}	}
	public bool JumpDown {					get {			return Input.GetKeyDown(KeyCode.Space);			}	}
	public Texture2D JumpImage {			get {			return Resources.Load("InputImages/"+PlatformName+"/Jump",typeof(Texture2D)) as Texture2D; } }

	public bool Interaction {				get {			return (Input.GetKey(KeyCode.E) || Input.GetMouseButton(1)) ? true : false;				}	}
	public bool InteractionDown {			get {			return (Input.GetKeyDown(KeyCode.E) || Input.GetMouseButtonDown(1)) ? true : false;			}	}
	public Texture2D InteractionImage {		get {			return Resources.Load("InputImages/"+PlatformName+"/Interaction",typeof(Texture2D)) as Texture2D; } }											


	public bool Attack {					get {			return (Input.GetKey(KeyCode.Q) || Input.GetMouseButton(0)) ? true : false;					}	}
	public bool AttackDown {				get {			return (Input.GetKeyDown(KeyCode.Q) || Input.GetMouseButtonDown(0)) ? true : false;			}	}
	public Texture2D AttackImage {			get {			return Resources.Load("InputImages/"+PlatformName+"/Attack",typeof(Texture2D)) as Texture2D; } }											


	public bool Fart {						get {			return Input.GetKey(KeyCode.F);					}	}
	public bool FartDown {					get {			return Input.GetKeyDown(KeyCode.F);				}	}
	public Texture2D FartImage {			get {			return Resources.Load("InputImages/"+PlatformName+"/Fart",typeof(Texture2D)) as Texture2D; } }											


	public bool NextPlayer {				get {			return Input.GetKeyDown(KeyCode.LeftShift);		}	}
	public Texture2D NextPlayerImage {		get {			return Resources.Load("InputImages/"+PlatformName+"/NextPlayer",typeof(Texture2D)) as Texture2D; } }											

	public bool PreviousPlayer {			get {			return Input.GetKeyDown(KeyCode.RightShift);	}	}
	public Texture2D PreviousPlayerImage {	get {			return Resources.Load("InputImages/"+PlatformName+"/PreviousPlayer",typeof(Texture2D)) as Texture2D; } }											

	public bool Start {						get {			return Input.GetKeyDown(KeyCode.Escape);		}	}
	public Texture2D StartImage {			get {			return Resources.Load("InputImages/"+PlatformName+"/Start",typeof(Texture2D)) as Texture2D; } }											

	public bool Back {						get {			return Input.GetKeyDown(KeyCode.Backspace);		}	}
	public Texture2D BackImage {			get {			return Resources.Load("InputImages/"+PlatformName+"/Back",typeof(Texture2D)) as Texture2D; } }											


}
