using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Gauge
{
    public TMPro.TextMeshPro NameText;
    public Transform ValueGauge;
    public Transform ProgGauge;
    public Theme Name;
    public float Value;
    public float PreviewValue;
}

public class Card : MonoBehaviour
{
    public GameObject ThemePrefab;
    public List<Gauge> Gauges = new List<Gauge>();
    float GoalScale = 0.7f;
    SpriteRenderer HoverSprite = null;
    TMPro.TextMeshPro PriceText;
    TMPro.TextMeshPro CardText;
    TMPro.TextMeshPro StarText;
    public int Price = 0;
    public int CardGain = 0;
    public int PriceMult = 1;
    public int CardMult = 1;
    public int StarGain = 0;
    public int StarMult = 1;

    static Color HoverColor = Color.white;
    static Color NotHoverColor = Color.black;
    //public Dictionary<Theme, float> Themes = new Dictionary<Theme, float>();

    bool ShowPreview = false;

    string GenerateCardName(bool IsSellable)
    {
        string CardName = "Missing Name";
        if(IsSellable)
        {
            // Beauty, Entertainment, Health, Creative, Security, Food, Count
            string[,] CardNames = new string[6, 6] {
                {"Beauty", "", "", "", "", ""} , // Beauty
                {"Clown paint", "Entertainment", "", "", "", ""} , // Entertainment
                {"Skin cream", "Magnetic bracelet", "Health", "", "", ""} , // Health
                {"Fantasy paint", "Painting gear", "Homemade tea", "Creative", "", ""} , // Creative
                {"Black eyeliner", "Laptop lock", "Pepper spray", "Homemade alarm", "Security", ""} , // Security
                {"Diet pils", "Chewing gum", "Apple", "Cookies", "Energy drink", "Food"}   // Food
            };

            /*
             * string[,] CardNames = new string[6, 6] {
                {"Beauty", "Clown paint", "Skin cream", "Fantasy paint", "Black eyeliner", "Diet pils"} , // Beauty
                {"Fashion glasses", "Entertainment", "Sport gear", "Painting gear", "Laptop lock", "Chewing gum"} , // Entertainment
                {"Sport towel", "Magnetic bracelet", "Health", "Homemade tea", "Pepper spray", "Apple"} , // Health
                {"Beauty", "Clown paint", "", "Creative", "Black eyeliner", "Diet pils"} , // Creative
                {"Beauty", "Clown paint", "", "Fantasy paint", "Security", "Diet pils"} , // Security
                {"Beauty", "Clown paint", "Skin cream", "Fantasy paint", "Black eyeliner", "Food"}   // Food
            };*/

            int FirstTheme = 0;
            int SecondTheme = 0;
            if (Gauges.Count > 0)
            {
                FirstTheme = (int)Gauges[0].Name;
                SecondTheme = FirstTheme;
            }
            if (Gauges.Count > 1)
            {
                SecondTheme = (int)Gauges[1].Name;
            }
            if (SecondTheme > FirstTheme) {
                int Tmp = FirstTheme;
                FirstTheme = SecondTheme;
                SecondTheme = Tmp;
            }
            CardName = CardNames[FirstTheme,SecondTheme];
        }
        else
        {
            string[] FirstNames = new string[] { "Alfredo", "Gertrude", "Emile", "Mafalda", "Tom", "Camilia", "George", "Janine", "Samir", "Nadia", "Leon", "Hilda" };
            string[] LastNames = new string[] { "ma","de","to","mu","stein","blum","pou","nia","so","dju","ka","nui","ni","ti","on","le" };

            string LastName = "";
            int LastSylCount = Random.Range(2, 5);
            for(int i=0; i<LastSylCount; ++i)
            {
                LastName = LastName + LastNames[Random.Range(0, LastNames.Length)];
            }
            LastName = LastName.Substring(0,1).ToUpper() + LastName.Substring(1);

            CardName = FirstNames[Random.Range(0, FirstNames.Length)] + " " + LastName;
        }
        return CardName;
    }

    // Start is called before the first frame update
    void Start()
    {
        List<Theme> AvailableThemes = new List<Theme>();
        for (int i = 0; i < (int)(Theme.Count); ++i)
        {
            AvailableThemes.Add((Theme)(i));
        }
        List<float> Values = new List<float>();
        for (int i = 0; i < 3; ++i)
        {
            Values.Add(Random.Range(1, 5) / 5.0f);
        }
        Values.Sort();
        Values.Reverse(); // Bouh pas beau

        for (int i = 0; i < Values.Count; ++i)
        {
            int RndPos = Random.Range(0, AvailableThemes.Count);
            Theme SelectedTheme = AvailableThemes[RndPos];
            AvailableThemes.RemoveAt(RndPos);
            GameObject NewThemeObj = GameObject.Instantiate(ThemePrefab.gameObject, transform.GetChild(0));
            NewThemeObj.transform.localPosition = new Vector3(0.0f, -2.8f - i*0.6f, -0.1f);
            Gauge NewGauge = new Gauge();
            NewGauge.Name = SelectedTheme;
            NewGauge.Value = Values[i];

            NewGauge.NameText = NewThemeObj.transform.GetChild(0).GetComponent<TMPro.TextMeshPro>();
            NewGauge.ValueGauge = NewThemeObj.transform.GetChild(1);
            NewGauge.ProgGauge = NewThemeObj.transform.GetChild(2);

            Gauges.Add(NewGauge);
        }
        HoverSprite = transform.GetChild(0).Find("Hover").gameObject.GetComponent<SpriteRenderer>();
        HoverSprite.color = NotHoverColor;
        PriceText = transform.GetChild(0).Find("Price").GetComponent<TMPro.TextMeshPro>();
        PriceText.gameObject.SetActive(false);
        CardText = transform.GetChild(0).Find("CardCount").GetComponent<TMPro.TextMeshPro>();
        CardText.gameObject.SetActive(false);
        StarText = transform.GetChild(0).Find("StarCount").GetComponent<TMPro.TextMeshPro>();
        StarText.gameObject.SetActive(false);
        SpriteRenderer SR = transform.GetChild(0).GetComponent<SpriteRenderer>();
        SpriteRenderer SR2 = transform.GetChild(0).Find("Pict").GetComponent<SpriteRenderer>();
        bool IsSellable = Price > 0;
        SR.color = new Color(IsSellable ? 1.0f : 0.0f, Random.value, Random.value, Random.value);
        int Theme1 = Gauges.Count > 0 ? (int)(Gauges[0].Name) + 1 : 1;
        int Theme2 = Gauges.Count > 1 ? (int)(Gauges[1].Name) + 1 : Theme1;
        SR2.color = IsSellable ? new Color(Theme1/7.0f, Theme2/7.0f, Random.value, Random.value) : new Color(0.0f, Random.value, Random.value, Random.value);
        TMPro.TextMeshPro NameText = transform.GetChild(0).Find("Name").GetComponent<TMPro.TextMeshPro>();
        NameText.text = GenerateCardName(IsSellable);
        UpdateThemes(true);
    }

    void UpdateThemes(bool Force)
    {
        for(int i=0; i<Gauges.Count; ++i)
        {
            Gauge Cur = Gauges[i];
            Cur.NameText.text = "<sprite index=" + ((int)(Cur.Name)+4) + ">" + Cur.Name.ToString();
            if(ShowPreview)
            {
                Cur.ValueGauge.localScale = new Vector3(Cur.PreviewValue, 1.0f, 1.0f);
                Cur.ProgGauge.localScale = new Vector3(Cur.Value, 1.0f, 1.0f);
            }
            else
            {
                Cur.ValueGauge.localScale = new Vector3(Cur.Value, 1.0f, 1.0f);
                float ProgValue = Force ? Cur.Value : Mathf.Lerp(Cur.ProgGauge.localScale.x, Cur.Value, Time.deltaTime * 2.0f);
                Cur.ProgGauge.localScale = new Vector3(ProgValue, 1.0f, 1.0f);
            }
        }
    }

    public void PreviewCard(Card Other)
    {
        for (int i = 0; i < Gauges.Count; ++i)
        {
            Gauge CurGauge = Gauges[i];
            CurGauge.PreviewValue = CurGauge.Value;
            for (int j = 0; j < Other.Gauges.Count; ++j)
            {
                Gauge OtherGauge = Other.Gauges[j];
                if (CurGauge.Name == OtherGauge.Name) // Same theme
                {
                    CurGauge.PreviewValue = Mathf.Max(0.0f, CurGauge.PreviewValue - OtherGauge.Value);
                }
            }
        }
        if(!ShowPreview)
        {
            //Debug.Log("Start Preview " + transform.name + " by " + Other.transform.name);
        }
        ShowPreview = true;        
    }

    public void StopPreview()
    {
        if (ShowPreview)
        {
            //Debug.Log("Stop Preview " + transform.name);
        }
        ShowPreview = false;
    }

    // Update is called once per frame
    void Update()
    {
        UpdateThemes(false);

        Transform Child0 = transform.GetChild(0);
        float Perc = 3.0f * Time.deltaTime;
        Child0.localScale = Vector3.Lerp(Child0.localScale, Vector3.one * GoalScale, Perc);

        if(Price>0)
        {
            PriceText.gameObject.SetActive(true);
            PriceText.text = Price + "<sprite index=2>";
        }
        else if(PriceMult>1)
        {
            PriceText.gameObject.SetActive(true);
            PriceText.text = "x" + PriceMult + "<sprite index=2>";
        }
        if(CardGain>0)
        {
            CardText.gameObject.SetActive(true);
            CardText.text = "+" + CardGain + "<sprite index=1>";
        }
        else if(CardMult>1)
        {
            CardText.gameObject.SetActive(true);
            CardText.text = "x" + CardMult + "<sprite index=1>";
        }
        if (StarGain > 0)
        {
            StarText.gameObject.SetActive(true);
            StarText.text = "+" + StarGain + "<sprite index=3>";
        }
        else if (StarMult > 1)
        {
            StarText.gameObject.SetActive(true);
            StarText.text = "x" + StarMult + "<sprite index=3>";
        }
    }

    public void StartHover()
    {
        if (HoverSprite) HoverSprite.color = HoverColor;
        GoalScale = 1.0f;
    }

    public void StopHover()
    {
        if (HoverSprite) HoverSprite.color = NotHoverColor;
        GoalScale = 0.7f;
        //ShowPreview = false;
    }
}
