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

public class OptionsSpawner : MonoBehaviour
{
    public static OptionsSpawner instance { get; private set; }
    void Awake()
    {
        instance = this;
    }
    public int goodSpawnRate=2;
    public int badSpawnRate=5;
    public Transform optionGood;
    public Transform optionBad;
    public Vector2 SpawnDistanceMin;
    public Vector2 SpawnDistanceMax;
    public float SpawnTimer = 5;

    float t;
    void Update()
    {
        if (!PlayerController.Started) { return; }
        if(PlayerController.t.position.y < 20){
            goodSpawnRate = 2;
            badSpawnRate = 1;
        }else if(PlayerController.t.position.y < 90){
            goodSpawnRate = 2;
            badSpawnRate = 2;
        }else if(PlayerController.t.position.y < 120){
            goodSpawnRate = 1;
            badSpawnRate = 3;
        }else{
            goodSpawnRate = 1;
            badSpawnRate = 5;
        }
        if (t < SpawnTimer)
        {
            t += Time.deltaTime;
        }
        else
        {
            t = 0;

            List<Vector3> positions= new List<Vector3>();
            for (int i = 0; i < goodSpawnRate; i++)
            {
                float newGoodRate = 1.05f + GetRandomRateDiff();
                Vector3 position = PlayerController.t.position + new Vector3(PlayerController.t.right.x * Random.Range(SpawnDistanceMin.x, SpawnDistanceMax.x), PlayerController.t.right.y * Random.Range(SpawnDistanceMin.y, SpawnDistanceMax.y));
                // foreach(Vector3 pos in positions){
                //     if(Vector3.Distance(pos, position) < 3){
                //         position += new Vector3(10,0,0);
                //     }
                // }
                SpawnGood(newGoodRate);
            }
            for (int i = 0; i < badSpawnRate; i++)
            {
                Vector3 position = PlayerController.t.position + new Vector3(PlayerController.t.right.x * Random.Range(SpawnDistanceMin.x, SpawnDistanceMax.x), PlayerController.t.right.y * Random.Range(SpawnDistanceMin.y, SpawnDistanceMax.y));
                // foreach(Vector3 pos in positions){
                //     if(Vector3.Distance(pos, position) < 3){
                //         position += new Vector3(10,0,0);
                //     }
                // }

                float newBadRate = 0.95f - GetRandomRateDiff();
                SpawnBad(newBadRate,position);
            }
        }
    }


    float GetRandomRateDiff()
    {
        float newRandom = Random.Range(1f, 100f);

        return 1- Mathf.Sqrt(newRandom) / 10f;
    }

    void SpawnGood(float rate, Vector3? position = null)
    {
        if (position == null)
        {
            position = PlayerController.t.position + new Vector3(PlayerController.t.right.x * Random.Range(SpawnDistanceMin.x, SpawnDistanceMax.x), PlayerController.t.right.y * Random.Range(SpawnDistanceMin.y, SpawnDistanceMax.y));
        }
        Transform newGood = Instantiate(optionGood, (Vector3)position, Quaternion.identity);
        newGood.GetComponentInChildren<TextMesh>().text = $"+{((rate - 1f) * 100).ToString("n0")}%";
        newGood.name += "," + rate;
    }

    void SpawnBad(float rate, Vector3? position = null)
    {
        if (position == null)
        {
            position = PlayerController.t.position + new Vector3(PlayerController.t.right.x * Random.Range(SpawnDistanceMin.x, SpawnDistanceMax.x), PlayerController.t.right.y * Random.Range(SpawnDistanceMin.y, SpawnDistanceMax.y));
        }
        Transform newBad = Instantiate(optionBad, (Vector3)position, Quaternion.identity);
        newBad.GetComponentInChildren<TextMesh>().text = $"-{((1f - rate) * 100).ToString("n0")}%";
        newBad.name += "," + rate;
    }

    public void Reposition()
    {
        // optionGood.position = PlayerController.instance.transform.position + (SpawnDistance * 1.5f);
        // optionBad.position = PlayerController.instance.transform.position + (SpawnDistance);

    }
}
