using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemyShipController : ShipController {

	private Vector3 targetPos { get { return getTargetPos(); } }
	private bool left;
	public float AdjustSpeed = 3F;
	public float AdjustRot = 1F;

	public float ShotInterval = 3F;
	private float lastShot;
	Vector3 getTargetPos() {
		Vector3 pos = Vector3.zero;
		if(left) {
			pos = PlayerShipController.instance.transform.position+PlayerShipController.instance.transform.right*-6; 
		} else {
			pos = PlayerShipController.instance.transform.position+PlayerShipController.instance.transform.right*6;
		}

		//Vector3 res = PlayerShipController.instance.transform.TransformPoint()
		return pos;
	}

	public override void FixedUpdate()
	{
		if(Vector3.Distance(transform.position,targetPos) > 10F) {
			base.FixedUpdate();
			Vector3 dir = targetPos-transform.position;
			Quaternion lookDir = Quaternion.LookRotation(dir);
			transform.rotation = lookDir;
			Speed = InitialSpeed;
		} else {
			Speed = Mathf.Lerp(Speed,PlayerShipController.instance.Speed,Time.deltaTime*AdjustSpeed);
			transform.rotation = Quaternion.Lerp(transform.rotation,PlayerShipController.instance.transform.rotation,Time.deltaTime*AdjustRot);
			
			if(Vector3.Distance(transform.position,PlayerShipController.instance.transform.position) <= 7F) {
				if(Time.time >= lastShot + ShotInterval) {
					Shoot(Vector3.zero);
				}
			}
		}
	}

	public override void Shoot(Vector3 dir) {
		Vector3 aiDir = (PlayerShipController.instance.transform.position-transform.position).normalized; 
		lastShot = Time.time;
		base.Shoot(aiDir);
	}




	public void SetPlayerPos() {
		if(transform.position.x < PlayerShipController.instance.transform.position.x) {
			//Left
			left = true;
		} else {
			//Right
			left = false;
		}
	}

	public override void UpdateDamageVisual() {

	}

	
	
}
