using System.Collections;
using UnityEngine;
using UnityEngine.EventSystems;

public class Puck : MonoBehaviour
{
    [HideInInspector]public Rigidbody2D rb;
    public bool isSelected;
    public LineRenderer lineRenderer;
    public Team team;
    public GameObject redTeamMarker;
    public GameObject blueTeamMarker;
    public Transform selectedMarker;
    public float markerRotateSpeed;
    public float markerBounceIntensity;
    public float markerBounceSpeed;
    Vector3 markerStartScale;
    Vector3 startPosition;
    float bounceSinA;
    void OnValidate()
    {
        UpdateTeam();
    }

    void Awake()
    {
        UpdateTeam();
        markerStartScale = selectedMarker.localScale;
        rb=GetComponent<Rigidbody2D>();
        startPosition = transform.position;
    }


    void UpdateTeam(){
        redTeamMarker.SetActive(team == Team.Red);
        blueTeamMarker.SetActive(team == Team.Blue);
    }

    void Start()
    {
        GameManager.OnSelectedPuckChanged += OnSelectedPuckChanged;
        GameManager.OnTeamChanged += OnTeamChanged;

        GameManager.instance.RegisterPuck(this);
    }

    void OnDestroy()
    {
        GameManager.OnSelectedPuckChanged -= OnSelectedPuckChanged;
        GameManager.OnTeamChanged -= OnTeamChanged;

        GameManager.instance.DisposePuck(this);
    }

    void Update()
    {
        if (isSelected && lineRenderer != null)
        {
            Vector3 worldEnd = Camera.main.ScreenToWorldPoint(new Vector3(GameManager.instance.curPosition.x, GameManager.instance.curPosition.y, Camera.main.nearClipPlane));

            Vector3 localEnd = transform.InverseTransformPoint(worldEnd);
            float clamp = GameManager.instance.puckDragClamp;
            localEnd = Vector2.ClampMagnitude(localEnd, clamp);

            // Only two points for the line
            lineRenderer.positionCount = 2;
            lineRenderer.SetPosition(0, Vector3.zero);
            lineRenderer.SetPosition(1, new Vector3(localEnd.x, localEnd.y, 0));
        }

        selectedMarker.gameObject.SetActive(GetMarkerVisibility());
        if(GameManager.SelectedTeam == team){
            selectedMarker.Rotate(new Vector3(0,0,1), markerRotateSpeed * Time.deltaTime);
            bounceSinA += Time.deltaTime * markerBounceSpeed;
            float bounce = Mathf.Sin(bounceSinA) * markerBounceIntensity;
            selectedMarker.localScale = markerStartScale + (Vector3.one * bounce);
        }
    }

    bool GetMarkerVisibility(){
        if(GameManager.isMoving){return false;}

        if(GameManager.SelectedTeam == team){
            if(GameManager.NoPuckSelected){
                return true;
            }else{
                return isSelected;
            }
        }

        return false;
    }

    void OnSelectedPuckChanged(Puck puck)
    {
        isSelected = puck == this;
        lineRenderer.enabled = isSelected;
    }

    void OnTeamChanged(Team team){

    }

    public void Reset(float duration)
    {
        StartCoroutine(CoroutineReset(duration));
    }

    IEnumerator CoroutineReset(float duration){
        float t=0;
        Vector3 pos1 = transform.position;
        Quaternion rot1 = transform.rotation;
        while(t<duration){
            t+=Time.deltaTime;
            transform.position = Vector3.Lerp(pos1, startPosition, t/duration);
            transform.rotation = Quaternion.Lerp(rot1, Quaternion.identity, t/duration);
            yield return null;
        }
    }
}

public enum Team{
    Red,
    Blue
}