using System.Linq;
using System.Text;
using UnityEngine;
using UnityEngine.UI;

public sealed class NavPointUIPanel : MonoBehaviour
{
    [SerializeField]
    private Text statusText = null;
    [SerializeField]
    private Text jumpText = null;
    [SerializeField]
    private Button jumpButton = null;
    [SerializeField]
    private Text debugText = null;

    private StringBuilder builder = new StringBuilder(5);

    public void ShowPoint(NavigationPoint target, NavigationPoint current, StarShip starShip) {
        if (target == null) {
            gameObject.SetActive(false);
        }
        else
        {
            gameObject.SetActive(true);
            ShowStatus(target, current, starShip);
        }
    }

    private void ShowStatus(NavigationPoint target, NavigationPoint current, StarShip starShip)
    {
        var distance = Vector3.Distance(target.SystemPostition, current.SystemPostition);
        var cost = starShip.GetFuelCost(distance);
        var canReach = starShip.MaxJumpRadius >= distance && starShip.Fuel >= cost;
        jumpButton.interactable = canReach && target != current;
        if (canReach)
        {
            jumpText.text = $"Jump\n{cost} F";
        }
        else
        {
            jumpText.text = $"Can't reach\n{cost} F";
        }
        statusText.text = GetStantionInfo(target);
        debugText.text = GetDebugInfo(target);
    }

    private string GetStantionInfo(NavigationPoint target)
    {
        builder.Clear();
        var stantion = target.Stantions.First();
        builder.Append($"{stantion.Name} ({stantion.TypeId})");

        var production = stantion.GetProductionItems();
        if (production.Length > 0)
        {
            builder.AppendLine();
            builder.Append("overflow: <color=green>");
            builder.Append(string.Join(", ", production));
            builder.Append("</color>");
        }
        var consumption = stantion.GetConsumptionItems();
        if (consumption.Length > 0)
        {
            builder.AppendLine();
            builder.Append("need: <color=red> ");
            builder.Append(string.Join(", ", consumption));
            builder.Append("</color>");
        }

        var modifiers = stantion.GetPublicModifiers();
        if (modifiers.Length > 0) {
            builder.AppendLine();
            builder.Append(string.Join(", ", modifiers));
        }

        return builder.ToString();
    }

    private string GetDebugInfo(NavigationPoint target)
    {
        builder.Clear();
        var stantion = target.Stantions.First();
        foreach (var res in stantion.Type.Economy.ResourceIds) {
            builder.AppendLine(res + " " + stantion.Resources[res] + " " + stantion.LocalCostPercentModifiers[res]);
        }
        foreach (var mod in stantion.Modifiers)
        {
            builder.AppendLine(mod.PublicId + " " + mod.ResourceId + " " + mod.Percent + " " + mod.EndTurn);
        }
        return builder.ToString();
    }

    public void Update()
    {
        if (Input.GetKeyDown(KeyCode.F1)) {
            debugText.gameObject.SetActive(!debugText.gameObject.activeSelf);
        }
    }
}
