using Godot;
using System;

public partial class ResourceBar : ResourceUIElement
{
        [Export] public Color highlight;
        [Export] public StyleBox sb_highlight;
        [Export] public Color error;
        [Export] public StyleBox sb_error;
        [Export] public double duration;
        [Export] public int loops;

        private Label l_name;
        private Label l_counter;
        private Label l_proxy;
        private TextureRect icon;
        private ProgressBar realBar;
        private ProgressBar proxyBar;

        private bool critical;

        private Tween tween;

        public override void _Ready()
        {
                // Labels
                l_name = GetNode<Label>("Margin/VBox/HBox/RotatedName/Panel/Margin/Name");
                l_counter = GetNode<Label>("Margin/VBox/HBox/VBox/Counter/MarginContainer/Label");
                l_proxy = GetNode<Label>("Margin/VBox/HBox/VBox/Proxy/Label");
                icon = GetNode<TextureRect>("Margin/VBox/Icon");
                realBar = GetNode<ProgressBar>("Margin/VBox/HBox/VBox/Bars/Overlap/ValueBar");
                proxyBar = GetNode<ProgressBar>("Margin/VBox/HBox/VBox/Bars/Overlap/ProxyBar");

                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;
                proxyBar.AddThemeStyleboxOverride("fill", crit ? sb_error : sb_highlight);
        }

        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)
        {
                l_counter.Text = value.ToString() + "/" + max.ToString();
                l_proxy.Text = proxy.ToString();
                realBar.MaxValue = max;
                proxyBar.MaxValue = max;
                realBar.Value = value;
                proxyBar.Value = proxy;

                blink_color(critical ? error : highlight);
        }

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