using UnityEngine;
using UnityEngine.UI;

namespace ScrambleKit.UI
{
    public class GenericHudPip : MonoBehaviour {

        public int Number;
        public Sprite None;
        public Sprite Empty;
        public Sprite Full;

        public MonoBehaviour ScriptToTrack;
        public string ValueFieldName;
        public string MaxFieldName;
        
        private System.Reflection.FieldInfo valueField;
        private System.Reflection.FieldInfo maxField;
        private Image image;

        void Update()
        {
            int val = GetValue();
            int max = GetMax();

            if (val >= Number)
                image.sprite = Full;
            else if (max >= Number)
                image.sprite = Empty;
            else
                image.sprite = None;
        }

        void Start()
        {
            ReflectFields();
            image = transform.GetComponent<Image>();
        }

        private void ReflectFields()
        {
            valueField = ScriptToTrack.GetType().GetField(ValueFieldName);
            maxField = ScriptToTrack.GetType().GetField(MaxFieldName);
        }

        private int GetValue()
        {
            return (int)valueField.GetValue(ScriptToTrack);
        }

        private int GetMax()
        {
            return (int)maxField.GetValue(ScriptToTrack);
        }

    }

}