/*
 * +================================================================================+
 *      __  ___  ______________  __  __  ______________   ____
 *     / / / / |/ /  _/_  __/\ \/ / / / / /_  __/  _/ /  / __/
 *    / /_/ /    // /  / /    \  / / /_/ / / / _/ // /___\ \
 *    \____/_/|_/___/ /_/     /_/  \____/ /_/ /___/____/___/
 *
 * +================================================================================+
 *
 *    "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 DesktopTargetPlatform : ITargetPlatform {

	private float mouseDeltaX, mouseDeltaY;

	private float mouseSens = 0.3f;

	private int xAxisSetting = 1;
	private int yAxisSetting = 1;

	public float deadZone = 0.2f;

	public void onStart() {
		updateCameraSensitivity();
	}

	public void onUpdate() {
	}

	public void onDeviceChange() {
		
	}

	public void updateCameraSensitivity() {
	}

	public bool isIdle() {
		if(hasAxisInput(0.5F) || Jump || Attack || Start || Back) {
			return false;
		} else {
			return true;
		}
	}

	public bool hasAxisInput(float deadZone) {
		if(Mathf.Abs(Input.GetAxis("Horizontal")) < deadZone && Mathf.Abs(Input.GetAxis("Vertical")) < deadZone) {
			return false;
		}

		return true;
	}

	private float getSecondaryAxisHorizontal() {
		if(Input.GetAxis("mouse x") > 0) {
			return Input.GetAxis("mouse x")*mouseSens;
		} else if(Input.GetAxis("mouse x") < 0) {
			return Input.GetAxis("mouse x")*mouseSens; 
		} else if(Input.GetKey(KeyCode.Keypad4)) {
			return -1; } 
		else if(Input.GetKey(KeyCode.Keypad6)) { 
			return 1; } 
		else { 
			return 0;}
	}

	private float getSecondaryAxisVertical() {
		if(Input.GetAxis("mouse y") > 0) {
			return Input.GetAxis("mouse y")*mouseSens;
		} else if(Input.GetAxis("mouse y") < 0) {
			return Input.GetAxis("mouse y")*mouseSens; 
		} else if(Input.GetKey(KeyCode.Keypad8)) {
			return -1; } 
		else if(Input.GetKey(KeyCode.Keypad5)) { 
			return 1; } 
		else { 
			return 0;}
	}

	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 "Desktop";								}	}
	public float AxisHorizontal {			get {			return Input.GetAxis("Horizontal");				}	}
	public float AxisVertical {				get {			return Input.GetAxis("Vertical");				}	}


	public float SecondaryAxisHorizontal {	get {			return getSecondaryAxisHorizontal()*xAxisSetting;			}	}
	public float SecondaryAxisVertical {	get {			return getSecondaryAxisVertical()*yAxisSetting;				}	}
	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 Input.GetKey(KeyCode.Space);				}	}
	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; } }											


}
