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

enum CharacterControlMode
{
	DEFAULT,
	AIMING,
}

[RequireComponent(typeof(NodalPathfinding2DAgent))]
[RequireComponent(typeof(LineRenderer))]
public abstract class PlayableCharacter : Actor
{
	public float shotRange = 5.0f;
	public LayerMask shotBlockMask;

	protected LineRenderer shotRangeRenderer;
	[SerializeField]
	protected LineRenderer pathRenderer;

	private float r = 0.0f;

	public bool isSelected = false;

	private Transform target;

	protected override void Start()
	{
		base.Start();
		if (shotRangeRenderer == null)
			shotRangeRenderer = GetComponent<LineRenderer>();
		if (pathRenderer == null)
			pathRenderer = GetComponentInChildren<LineRenderer>();
		if (selectRing == null)
			selectRing = transform.Find("SelectRing").gameObject;
	}

	protected virtual void Update()
	{
		if (isSelected)
		{
			r = Mathf.Min(shotRange, r + shotRange * Time.deltaTime);
			Vector3[] vertices = new Vector3[361];
			for (int i = 0; i < 360; i++)
			{
				Vector2 direction = (Quaternion.Euler(0, 0, i) * Vector2.right).normalized;
				RaycastHit2D hit = Physics2D.Raycast(transform.position, direction, r, shotBlockMask);

				if (hit.collider != null)
					vertices[i] = hit.point;
				else
					vertices[i] = transform.position + new Vector3(direction.x, direction.y) * r;
			}
			vertices[360] = vertices[0];

			shotRangeRenderer.SetPositions(vertices);

		}
	}

	public override void OnSelected()
	{
		isSelected = true;
		shotRangeRenderer.enabled = true;
		selectRing?.SetActive(true);
		r = 0;
	}

	public override void OnUnselected()
	{
		isSelected = false;
		shotRangeRenderer.enabled = false;
		selectRing?.SetActive(false);
	}

	public override void MoveTo(Vector2 destination)
	{
		if (isSelected)
			agent.MoveTo(destination);
	}

	public void DrawPath()
	{

	}

	protected abstract void DefaultControl();
	protected abstract void AimingControl();
}
