using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using NDream.AirConsole;
using Newtonsoft.Json.Linq;
public class PlayerShipController : ShipController {

	public static PlayerShipController instance;
	public GameObject DamageSmoke;
	private bool pressingLeft;
	private bool pressingRight;

	public int NavigatorID = -1;
	public int SteermanID = -1;
	public int GunnerID = -1;

	public float SpeedIncreaseIntervall = 15F;
	private float lastIncrease = 0;
	bool allSet() {
		if(NavigatorID != -1 && SteermanID != -1 && GunnerID != -1) {
			return true;
		}
		return false;
	}

	public void SetWindDir(int fromID, Vector2 dir) {
		var data = new {
				action = "wind",
				winddirx = dir.x,
				winddiry = dir.y
			};
		AirConsole.instance.Message(fromID,data);
	}

	public void SetWaveDir(int fromID, Vector2 dir) {
		var data = new {
				action = "wave",
				wavedirx = dir.x,
				wavediry = dir.y
			};
		AirConsole.instance.Message(fromID,data);
	}

	public override void UpdateDamageVisual() {
		switch(Health) {
			case 4:
			DamageSmoke.SetActive(true);
			break;
			case 3:
			Sails[0].SetActive(false);
			break;
			case 2:
			Sails[1].SetActive(false);
			break;
			case 1:
			Sails[2].SetActive(false);
			break;
		}
	}

	public void Reset() {
		transform.position = Vector3.zero;
		Health = 5;
		DamageSmoke.SetActive(false);
		foreach(GameObject s in Sails) {
			s.SetActive(true);
		}
		setScreenToAll("choice");
		GunnerID = -1;
		SteermanID = -1;
		NavigatorID = -1;

	}

	public override void Awake() {
		base.Awake();
		if(PlayerShipController.instance == null) {
			PlayerShipController.instance = this;
		}

		 AirConsole.instance.onMessage += OnMessage;
		 AirConsole.instance.onConnect += OnConnect; 
	}


	public void TakeGunner(int id) {
		if(GunnerID == -1) {
			if(NavigatorID == id) { LeaveNavigator(id); }
			if(SteermanID == id) { LeaveSteerman(id); }
			GunnerID = id;
		}

		if(allSet()) {
			UIScreen.GetScreen<GameHUDScreen>().Show();
		}
		setSeatMark("gun-choice",true,id);
		setSeatLock("gun-choice",true);
	}

	public void LeaveGunner(int id) {
		if(GunnerID == id) {
			GunnerID = -1;
		}
		setSeatMark("gun-choice",false,id);
		setSeatLock("gun-choice",false);
	}

	public void TakeNavigator(int id) {
		if(NavigatorID == -1) {
			if(GunnerID == id) { LeaveGunner(id); }
			if(SteermanID == id) { LeaveSteerman(id); }
			NavigatorID = id;
		}

		if(allSet()) {
			UIScreen.GetScreen<GameHUDScreen>().Show();
		}
		setSeatMark("nav-choice",true,id);
		setSeatLock("nav-choice",true);
	}

	public void LeaveNavigator(int id) {
		if(NavigatorID == id) {
			NavigatorID = -1;
		}
		setSeatMark("nav-choice",false,id);
		setSeatLock("nav-choice",false);
	}

	public void TakeSteerman(int id) {
		if(SteermanID == -1) {
			if(NavigatorID == id) { LeaveNavigator(id); }
			if(GunnerID == id) { LeaveGunner(id); }
			SteermanID = id;
		}

		if(allSet()) {
			UIScreen.GetScreen<GameHUDScreen>().Show();
		}
		setSeatMark("steer-choice",true,id);
		setSeatLock("steer-choice",true);
	}
	public void LeaveSteerman(int id) {
		if(SteermanID == id) {
			SteermanID = -1;
		}
		setSeatMark("steer-choice",true,id);
		setSeatLock("steer-choice",false);
	}

	

	void OnConnect(int fromID) {
		/*
		var data = new { 
			 show_view_id = "view-0"
		};
		AirConsole.instance.Message(fromID,data);
		*/
	}

	void OnMessage(int fromID, JToken data) {
		string inputtype = (string) data["inputtype"];
		

		switch(inputtype) {
			case "seat":
			string seat = (string) data["seat"];
			processSeatChoice(seat,fromID);
			break;
			case "steering":
			string dir = (string) data["ship"];
			bool pressed = (bool) data["pressed"];
			processSteering(dir,pressed);			
			break;
			case "shoot":
			float rot = (float) data["rot"];
			rot = rot*Mathf.Rad2Deg;
			Debug.Log("Rot: "+rot);
			ProcessShoot(rot);
			break;
			default:
			break;
		}
	}

	void ProcessShoot(float rot) {
		Vector3 dir = Quaternion.AngleAxis(rot+90, Camera.main.transform.forward*-1) * Camera.main.transform.right*-1;
		//Debug.DrawRay(Camera.main.transform.position,dir,Color.yellow,5F);
		Shoot(dir);
	}

	void processSteering(string dir,bool pressed) {
		switch(dir) {
			case "left":
			pressingLeft = pressed;
			break;
			case "right":
			pressingRight = pressed;
			break;
		}
	}

	void processSeatChoice(string seat,int id) {
		switch(seat) {
			case "navigator":
			if(NavigatorID == id) {
				LeaveNavigator(id);
			} else {
				TakeNavigator(id);
			}
			break;
			case "gunner":
			if(NavigatorID == id) {
				LeaveGunner(id);
			} else {
				TakeGunner(id);
			}
			break;
			case "steerman":
			if(NavigatorID == id) {
				LeaveSteerman(id);
			} else {
				TakeSteerman(id);
			}
			break;
		}
	}

	public void DisplayMobileScreens() {
		setScreen(GunnerID,"gunner");
		setScreen(NavigatorID,"navigator");
		setScreen(SteermanID,"steerman");
	}

	void setScreen(int targetID, string screen) {
		var message = new {
				action = "view",
				viewid = screen
			};
			
			AirConsole.instance.Message(targetID, message);
	}

	void setSeatLock(string seatpos, bool lockedbool) {
		int l = (lockedbool) ? 1 : 0;
		var message = new {
				action = "lockseat",
				seat = seatpos,
				locked = l
			};
			
			AirConsole.instance.Broadcast(message);
	}

	void setSeatMark(string seatpos, bool lockedbool,int deviceID) {
		int l = (lockedbool) ? 1 : 0;
		var message = new {
				action = "markseat",
				seat = seatpos,
				marked = l
			};
			
			AirConsole.instance.Message(deviceID,message);
	}

	void setScreenToAll(string screen) {
		var message = new {
				action = "view",
				viewid = screen
			};
			
			AirConsole.instance.Broadcast(message);
	}


	public void Update() {
		if(Input.GetKey(KeyCode.LeftArrow) || pressingLeft) {
			TurnLeft();
		}

		if(Input.GetKey(KeyCode.RightArrow) || pressingRight) {
			TurnRight();
		}

		if(Input.GetMouseButtonDown(0)) {
			Vector3 mpos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
			mpos.y = 0;
			Vector3 zeroPos = transform.position;
			zeroPos.y = 0;

			Vector3 dir = mpos-zeroPos;
			Shoot(dir.normalized);

		}

		if(Time.time > lastIncrease+SpeedIncreaseIntervall) {
			lastIncrease = Time.time;
			Speed += Mathf.Pow(Speed*0.05F,2F);
			SpeedIncreaseIntervall = SpeedIncreaseIntervall-Mathf.Pow(SpeedIncreaseIntervall*0.05F,2F);
		}
	}

	

	public override void DestroyMe() {
		GameManager.Instance.GameOver();
	}

}
