using System;
using UnityEngine;

/// <summary>
/// component to select and control base or army
/// </summary>
public sealed class SelectionHelper : MonoBehaviour
{
    [SerializeField]
    private Officer controlOfficer = null;
    [SerializeField]
    private Base controlBase = null;

    public Vector3 Target {
        get
        {
            if (controlOfficer != null)
                return controlOfficer.TargetPosition;
            return transform.position;
        }
    }

    public float Radius {
        get
        {
            if (controlOfficer != null)
                return controlOfficer.Radius;
            if (controlBase != null)
                return controlBase.Radius;
            return 0.5f;
        }
    }

    public float FireRange
    {
        get
        {
            if (controlOfficer != null)
                return controlOfficer.FireRange;
            if (controlBase != null)
                return controlBase.FireRange;
            return 0f;
        }
    }

    public ProjectBlueprint[] GetAvailableProjects()
    {
        if (controlOfficer != null)
            return controlOfficer.GetAvailableProjects();
        if (controlBase != null)
            return controlBase.GetAvailableProjects();
        return null;
    }

    public void StartProject(ProjectBlueprint project)
    {
        if (controlOfficer != null)
        {
            controlOfficer.StartProject(project);
            return;
        }
        if (controlBase != null)
        {
            controlBase.StartProject(project);
            return;
        }
        throw new NotImplementedException();
    }

    public int factionId {
        get {
            if (controlOfficer != null)
                return controlOfficer.FactionId;
            if (controlBase != null)
                return controlBase.FactionId;
            return -1;
        }
    }

    public Color Color
    {
        get
        {
            if (controlOfficer != null)
                return controlOfficer.Faction.Color;
            if (controlBase != null)
                return controlBase.Faction.Color;
            return Color.white;
        }
    }

    public string GetDescription() {
        if (controlOfficer != null) {
            return controlOfficer.GetDescription();
        }
        if (controlBase != null)
        {
            return controlBase.GetDescription();
        }
        if (this == null)
            return string.Empty;
        Debug.LogError("No selector");
        return "Empty Selection";
    }

    public bool CanMove() => controlOfficer != null;

    public void MoveTo(Vector3 position)
    {
        if (controlOfficer == null)
        {
            Debug.LogError("Selection can't move");
            return;
        }
        controlOfficer.MoveTo(position);
    }

    private void Reset()
    {
        controlOfficer = GetComponent<Officer>();
        controlBase = GetComponent<Base>();
    }
}
