using Godot;
using System;

[GlobalClass]
public partial class GameResource : Resource
{
        [ExportCategory("Texts")]
        [Export] public string identifier = "_new-resource";
        [Export] public string title = "New Resource";
        [Export(PropertyHint.MultilineText)] public string description = "Resource Description";

        [ExportCategory("Values")]
        [Export] public int value = 0;
        [Export] public int proxy = 0;
        [Export] public int max = -1;

        [ExportCategory("Flags")]
        [Export] public bool critical = false;
        [Export] public bool visible = false;
        [Export] public bool docked = false;

        public GameResource()
        {
                identifier = "_new-resource";
                title = "New Resource";
                description = "Resource Description";

                value = 0;
                proxy = 0;
                max = -1;

                critical = false;
                visible = false;
                docked = false;
        }

        public GameResource(string id, string t, string d, int v, bool crit = false, bool vis = true, bool dock = true)
        {
                identifier = id;
                title = t;
                description = d;
                value = v;
                proxy = v;
                max = v;
                critical = crit;
                visible = vis;
                docked = dock;
        }

        public GameResource(string id, string t, string d, int v, int m, bool crit = false, bool vis = true, bool dock = true)
        {
                identifier = id;
                title = t;
                description = d;
                value = v;
                proxy = v;
                max = m;
                critical = crit;
                visible = vis;
                docked = dock;
        }

        public void SetValue(int v)
        {
                GD.Print("Called SetValue: " + v.ToString());
                if (max > 0 && v >= max)
                {
                        GD.Print("Over Max");
                        value = max;
                }
                else if (v <= 0)
                {
                        GD.Print("setting to less than 0");
                        value = 0;
                }
                else
                {
                        GD.Print("Setting to value");
                        value = v;
                }

                ResetProxy();
        }

        public void ModValue(int d)
        {
                GD.Print("Called ModValue: " + d.ToString());
                SetValue(value + d);
        }

        public void SetProxy(int v)
        {
                if (max > 0 && v >= max)
                {
                        proxy = max;
                }
                else if (v <= 0)
                {
                        proxy = 0;
                }
                else
                {
                        proxy = v;
                }
        }

        public void ModProxy(int d)
        {
                SetProxy(proxy + d);
        }

        public void SetMax(int v)
        {
                max = v;
        }

        public void ModMax(int d)
        {
                SetMax(max + d < -1 ? -1 : max + d);
        }

        public void Pay()
        {
                SetValue(proxy);
        }

        public void ResetProxy()
        {
                SetProxy(value);
        }

        public bool TestCost(int v)
        {
                return (proxy + v >= 0) && (proxy + v <= max);
        }

}