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

public class hole : MonoBehaviour
{
    public List<GameObject> shapes;
    List<GameObject> shapes2= new List<GameObject>();
    public int rightOne;
    float fromit;
    float lastTime;
    Vector3 firstpos;
    public bool isrun = true;
    List<int> ids = new List<int>();
    BoxCollider c;
    // Start is called before the first frame update
    void Start()
    {
        c = gameObject.AddComponent<BoxCollider>();
        firstpos = new Vector3(transform.position.x, 0, transform.position.z);
        
        Color color = GetComponentsInChildren<Cube>()[0].COLOR;

        transform.position = new Vector3(0, 4.5f, transform.position.z);
        int i = 0;
        int j = rightOne;
        int chanment = GameState.defc;
        while (i < 90)
        {
            if (GameState.defc > 0 && chanment <= GameState.defc)
            {
                j = Random.Range(0, 5);
                chanment++;
            }
            else
            {
                j = rightOne;
                chanment = 1;
            }
            shapes2.Add(Instantiate<GameObject>(shapes[j], transform) as GameObject);
            ids.Add(j);
            shapes2[i].transform.localPosition = new Vector3(fromit, 0, 0);
            fromit += 5;
            if(j == rightOne)
            {
                Renderer c = shapes2[i].GetComponentsInChildren<Renderer>()[0];
                c.material.color = color;
            }
            Destroy(shapes2[i].GetComponentsInChildren<Cube>()[0]);
            i++;
        }
        i = 0;
        //while (i < ids.Count) { print(ids[i]);i++; };
    }

    // Update is called once per frame
    void Update()
    {
        lastTime += Time.deltaTime;
        if (isrun)
        {
            if (lastTime >= 0.5f/GameState.speed)
            {
                transform.localPosition -= Vector3.right * 5;
                lastTime = 0;
            }
        }
    }
    
    public bool pressit()
    {
        int i=0;
        bool ret =false;
        while (i<ids.Count)
        {
            if (shapes2[i].transform.position.x == 0)
            {
                GameObject g= Instantiate<GameObject>(shapes2[i].gameObject, transform.parent) as GameObject;
                g.transform.position = new Vector3(0, 1, transform.position.z);
                if(rightOne == ids[i])
                {
                    ret = true;
                    g.transform.position = new Vector3(0, 0, transform.position.z);
                }
            }
            i++;
        }
        foreach (GameObject h in shapes2) Destroy(h);
        isrun = false;
        if (!isrun)
        {
            c.size = new Vector3(1, 6, 1);
        }
        return ret;
    } 
    public void toORIG()
    {
        transform.position = firstpos;
    }
}
