using TMPro;
using UnityEditor;
using UnityEngine;
using UnityEngine.SceneManagement;

public class PaintTriangle : MonoBehaviour
{

    [Range(0,1)]
    public float sleep;
    [Range(0,1)]
    public float social;
    [Range(0,1)]
    public float selfRealisation;

    public float loseNumber;

    public Material mat;
    public Gradient stateColor;

    public float scale;

    public float[] iconsRanges;

    public MessageHandler messageHandler;

    public GameController gameController;


    public TextMeshProUGUI[] sleepingObjects;
    public TextMeshProUGUI[] selfObjects;
    public TextMeshProUGUI[] socialObjects;

    private Mesh mesh;

    public bool processing = true;

    [Header("Phrases")]
    public string[] midSleep;
    public string[] lowSleep;
    private bool midSleepLaunch;
    private bool lowSleepLaunch;
    
    public string[] midSocial;
    public string[] lowSocial;
    private bool midSocialLaunch;
    private bool lowSocialLaunch;
    
    public string[] midAuto;
    public string[] lowAuto;
    private bool midAutoLaunch;
    private bool lowAutoLaunch;
    
    
    private void Start()
    {
        gameObject.AddComponent<MeshFilter>();
        gameObject.AddComponent<MeshRenderer>().material = mat;
        mesh = GetComponent<MeshFilter>().mesh;
        

        transform.localScale = new Vector3(scale, scale, scale);
    }

    private void Update()
    {
        if (!processing)
            return;
        
        ChangePoints();

        if (Mathf.Min(sleep, social, selfRealisation) < loseNumber)
        {
            processing = false;
            gameController.FinalBlock();
            messageHandler.NewText("Do you want\nto live\nagain?", () => { SceneManager.LoadScene(1); },"YES",
                () =>
                {
                    #if UNITY_EDITOR
                        EditorApplication.isPlaying = false;
                    #else
                    Application.Quit();
                    #endif
                }, "NO");
        }

        if (sleep <= 0.5f && !midSleepLaunch)
        {
            midSleepLaunch = true;
            int rnd = Random.Range(0, 2);
            messageHandler.NewText(midSleep[rnd]);
        }
        
        if (sleep <= 0.2f && !lowSleepLaunch)
        {
            lowSleepLaunch = true;
            int rnd = Random.Range(0, 2);
            messageHandler.NewText(lowSleep[rnd]);
        }
        
        if (social <= 0.5f && !midSocialLaunch)
        {
            midSocialLaunch = true;
            int rnd = Random.Range(0, 2);
            messageHandler.NewText(midSocial[rnd]);
        }
        
        if (social <= 0.2f && !lowSocialLaunch)
        {
            lowSocialLaunch = true;
            int rnd = Random.Range(0, 2);
            messageHandler.NewText(lowSocial[rnd]);
        }
        
        if (selfRealisation <= 0.5f && !midAutoLaunch)
        {
            midAutoLaunch = true;
            int rnd = Random.Range(0, 2);
            messageHandler.NewText(midAuto[rnd]);
        }
        
        if (selfRealisation <= 0.2f && !lowAutoLaunch)
        {
            lowAutoLaunch = true;
            int rnd = Random.Range(0, 2);
            messageHandler.NewText(lowAuto[rnd]);
        }
    }

    public void IconsChanges(float s, float sR, float so)
    {
        if (!processing)
            return;
        
        foreach (TextMeshProUGUI ob in sleepingObjects)
        {
            ob.gameObject.SetActive(false);
        }
        
        foreach (TextMeshProUGUI ob in selfObjects)
        {
            ob.gameObject.SetActive(false);
        }
        
        foreach (TextMeshProUGUI ob in socialObjects)
        {
            ob.gameObject.SetActive(false);
        }

        EvaluateIcons(s, sleepingObjects);
        EvaluateIcons(sR, selfObjects);
        EvaluateIcons(so, socialObjects);
    }

    private void EvaluateIcons(float parameter, TextMeshProUGUI[] array)
    {
        if (parameter <= iconsRanges[0])
        {
            for (int index = 0; index < array.Length; index++)
            {
                TextMeshProUGUI ob = array[index];
                ob.text = "-";
                ob.gameObject.SetActive(true);
            }
        }
        else if (parameter <= iconsRanges[1])
        {
            for (int index = 0; index < array.Length-1; index++)
            {
                TextMeshProUGUI ob = array[index];
                ob.text = "-";
                ob.gameObject.SetActive(true);
            }
        }
        else if (parameter <= iconsRanges[2])
        {
            for (int index = 0; index < array.Length-2; index++)
            {
                TextMeshProUGUI ob = array[index];
                ob.text = "-";
                ob.gameObject.SetActive(true);
            }
        }
        else if (parameter >= iconsRanges[3])
        {
            for (int index = 0; index < array.Length-2; index++)
            {
                TextMeshProUGUI ob = array[index];
                ob.text = "+";
                ob.gameObject.SetActive(true);
            }
        }
        else if (parameter >= iconsRanges[4])
        {
            for (int index = 0; index < array.Length-1; index++)
            {
                TextMeshProUGUI ob = array[index];
                ob.text = "+";
                ob.gameObject.SetActive(true);
            }
        }
        else if (parameter >= iconsRanges[5])
        {
            for (int index = 0; index < array.Length; index++)
            {
                TextMeshProUGUI ob = array[index];
                ob.text = "+";
                ob.gameObject.SetActive(true);
            }
        }
    }

    private void ChangePoints()
    {
        mesh.Clear();

        // make changes to the Mesh by creating arrays which contain the new values
        mesh.vertices = new [] {new Vector3(0, 1 * sleep, 0), new Vector3(1 * social, -0.5f * social, 0), new Vector3(-1 * selfRealisation, -0.5f * selfRealisation, 0)};
        mesh.uv = new [] {new Vector2(0, 0), new Vector2(0, 1), new Vector2(1, 1)};
        mesh.triangles =  new [] {0, 1, 2};

        mat.color = stateColor.Evaluate(Mathf.Min(sleep, social, selfRealisation));
    }
}
