using UnityEngine;
using System;
using System.Collections.Generic;
using mgaia.Utils;

public class AttributeManager
{
    private Dictionary<Indicators, float> _mIndicatorValues = new Dictionary<Indicators, float>();
    private Dictionary<Attributes, float> _mAttributeValues = new Dictionary<Attributes, float>();

    public delegate void IndicatorHandler(Indicators indicator, float value);

    public event IndicatorHandler OnIndicatorChanged;

    public AttributeManager()
    {
        foreach (Indicators indicator in Enum.GetValues(typeof(Indicators)))
        {
            SetIndicator(indicator, GameConstants.STARTING_INDICATOR_VALUE);
        }
        foreach (Attributes attribute in Enum.GetValues(typeof(Attributes)))
        {
            SetAttribute(attribute, GameConstants.STARTING_ATTRIBUTE_VALUE);
        }
    }

    public float GetIndicator(Indicators indicator)
    {
        if (_mIndicatorValues.ContainsKey(indicator))
        {
            return _mIndicatorValues[indicator];
        }
        else
        {
            return 0;
        }
    }

    private void SetIndicator(Indicators indicator, float value)
    {
        if (indicator == Indicators.Wealthy || indicator == Indicators.Warpower)
        {
            value *= 3;
        }

        _mIndicatorValues[indicator] = Mathf.Clamp(value, GameConstants.MIN_INDICATOR_VALUE, GameConstants.MAX_INDICATOR_VALUE);
        if (OnIndicatorChanged != null)
        {
            OnIndicatorChanged(indicator, value);
        }
        Debug.Log("Atualizado o valor de " + indicator + " para " + GetIndicator(indicator));
    }

    public float GetAttribute(Attributes attribute)
    {
        if (_mAttributeValues.ContainsKey(attribute))
        {
            return _mAttributeValues[attribute];
        }
        else
        {
            return 0;
        }
    }

    private void SetAttribute(Attributes attribute, float value)
    {
        _mAttributeValues[attribute] = Mathf.Clamp(value, GameConstants.MIN_ATTRIBUTE_VALUE, GameConstants.MAX_ATTRIBUTE_VALUE);
        Debug.Log("Atualizado o valor de " + attribute + " para " + GetAttribute(attribute));
    }

    public void ConsumeChoice(Choice choice)
    {
        choice.ChangedAttributes.ForEach(kvp =>
        {
            SetAttribute(kvp.Key, GetAttribute(kvp.Key) + kvp.Value);
        });
        choice.ChangedIndicators.ForEach(kvp =>
        {
            SetIndicator(kvp.Key, GetIndicator(kvp.Key) + kvp.Value);
        });
    }

    public void ConsumeResponseLetter(ResponseLetter responseLetter)
    {
        responseLetter.ChangedAttributes.ForEach(kvp =>
        {
            SetAttribute(kvp.Key, GetAttribute(kvp.Key) + kvp.Value);
        });
        responseLetter.ChangedIndicators.ForEach(kvp =>
        {
            SetIndicator(kvp.Key, GetIndicator(kvp.Key) + kvp.Value);
        });
    }

    public AttributeInfluenceLevel GetAttributeInfluenceLevel(Attributes attribute)
    {
        if (GetAttribute(attribute) <= 0.33f)
        {
            return AttributeInfluenceLevel.Low;
        }
        else if ((GetAttribute(attribute) <= 0.66f))
        {
            return AttributeInfluenceLevel.Medium;
        }
        else
        {
            return AttributeInfluenceLevel.High;
        }
    }

    private void LogAttributesAndIndicators()
    {
        foreach (Indicators indicator in Enum.GetValues(typeof(Indicators)))
        {
            Debug.Log(indicator + ": " + GetIndicator(indicator));
        }
        foreach (Attributes attribute in Enum.GetValues(typeof(Attributes)))
        {
            Debug.Log(attribute + ": " + GetAttribute(attribute));
        }
    }
}