using UnityEngine;

public sealed class SelectionView : MonoBehaviour
{
    [SerializeField]
    private FireRangeView fireRange = null;
    [SerializeField]
    private FireRangeView selectionRange = null;
    [SerializeField]
    private MeshRenderer selectionRenderer = null;
    [SerializeField]
    private Transform target = null;

    private Transform pivot = null;
    private SelectionHelper selection = null;

    public void SetSelection(SelectionHelper newSelection = null)
    {
        if (newSelection == null)
        {
            selection = null;
            pivot = null;
            fireRange.Hide();
            selectionRange.Hide();
            target.gameObject.SetActive(false);
            return;
        }
        selection = newSelection;
        pivot = newSelection.transform;
        fireRange.SetRange(newSelection.FireRange, newSelection.FireRange * 1.1f);
        fireRange.Show();
        selectionRange.SetRange(newSelection.Radius + 0.3f, newSelection.Radius + 0.3f + 0.05f);
        selectionRange.Show();
        transform.position = pivot.position;
        selectionRenderer.material.color = newSelection.Color;
        target.position = newSelection.Target;
        target.gameObject.SetActive(true);
    }
    public void SetObject(Transform transform = null)
    {
        if (transform == null)
        {
            selection = null;
            pivot = null;
            fireRange.Hide();
            selectionRange.Hide();
            target.gameObject.SetActive(false);
            return;
        }
        selection = null;
        pivot = transform;
        selectionRange.SetRange(0.5f, 0.55f);
        selectionRange.Show();
        transform.position = pivot.position;
        selectionRenderer.material.color = Color.gray;
        target.gameObject.SetActive(false);
    }

    private void Start()
    {
        SetSelection(null);
    }

    private void Update()
    {
        if (pivot == null)
            return;
        transform.position = pivot.position;
        if (selection != null)
            target.position = selection.Target;
    }
}
