using Godot;
using System;

public partial class ResourceItem : ResourceUIElement
{
        [Export] public Color highlight;
        [Export] public Color error;
        [Export] public double duration;
        [Export] public int loops;

        private Label l_name;
        private Label l_counter;
        private TextureRect icon;

        private bool critical;

        private Tween tween;

        public override void _Ready()
        {
                // Labels
                l_name = GetNode<Label>("Margin/VBoxContainer/NamePanel/Margin/Name");
                l_counter = GetNode<Label>("Margin/VBoxContainer/CountPanel/Count");
                icon = GetNode<TextureRect>("Margin/VBoxContainer/Icon");

                tween = CreateTween();
                set_data("Placeholder", new PlaceholderTexture2D());
                set_color(false);
                set_values(1, 1, 1);
        }

        private void blink_color(Color col)
        {
                TweenPresets.ResetTween(this, ref tween);
                TweenPresets.BlinkProperty(ref tween, l_name, col, duration, loops);
        }

        public override void set_color(bool crit)
        {
                critical = crit;
        }

        public override void set_data(string _name, Texture2D _icon)
        {
                l_name.Text = _name;
                icon.Texture = _icon;
        }

        public override void set_values(int value, int proxy, int max)
        {
                string txt = value.ToString();
                if (value != proxy)
                {
                        txt += " (" + proxy.ToString() + ")";
                }
                l_counter.Text = txt;

                blink_color(critical ? error : highlight);
        }

        public override void alert()
        {
                blink_color(error);
        }
}
