using UnityEngine;
using System.Collections;

public class Walls : MonoBehaviour {
    public static Walls Instance;
    public Transform wall;
    private Transform[] walls;
    private SpriteRenderer[] wallRenderers;
    private bool intro;
    private float introStart;
    float progress;

    private int currentType;
    private int differentTypes = 4;
    private bool expand;

    void Awake()
    {
        Instance = this;
    }

    void Start()
    {
        currentType = Random.Range(0, differentTypes);
        progress = 1;
        introStart = Time.time;
        intro = true;
        walls = new Transform[6];
        wallRenderers = new SpriteRenderer[6];

        for (int i = 0; i < 6; i++)
        {
            float angle = i * Mathf.PI / 3;
            Transform w = Transform.Instantiate(wall);
            w.SetParent(transform);

            Vector3 tmp = new Vector3(Mathf.Sin(angle), Mathf.Cos(angle), 0);
            w.localPosition = tmp * 4.9f;
            w.rotation = Quaternion.Euler(0, 0, - angle / Mathf.PI * 180);
            wallRenderers[i] = w.GetComponent<SpriteRenderer>();
            wallRenderers[i].color = new Color(1, 1, 1, 0);
            walls[i] = w;
        }
    }

    public void Restart()
    {
        transform.localScale = Vector3.one;
        for (int i = 0; i < 6; i++)
        {
            float angle = i * Mathf.PI / 3;
            walls[i].rotation = Quaternion.Euler(0, 0, -angle / Mathf.PI * 180);
            walls[i].localScale = Vector3.one;
        }
        currentType = Random.Range(0, differentTypes);
        intro = true;
        progress = 1;
    }

    public void Expand()
    {
        expand = true;
    }

    void Update()
    {
        if (!intro)
        {
            if (expand)
            {
                float tmp = transform.localScale.x;
                transform.localScale = Vector3.one * Mathf.Lerp(tmp, 1.5f, Time.deltaTime * 4);
            }
            return;
        }

        progress = Mathf.Lerp(progress, -0.05f, Time.deltaTime * 1.5f);
        if (progress < 0)
        {
            progress = 0;
            intro = false;
            GameManager.Instance.IntroDone();
        }

        for (int i = 0; i < 6; i++)
        {
            float angle = i * Mathf.PI / 3;
            Vector3 tmp = new Vector3(Mathf.Sin(angle), Mathf.Cos(angle), 0);
            Vector3 tmpTangent = new Vector3(- Mathf.Cos(angle), Mathf.Sin(angle), 0);
            wallRenderers[i].color = new Color(1, 1, 1, 1 - progress);
            switch (currentType)
            {
                case 0:
                    walls[i].position = tmp * 4.9f + tmpTangent * 5 * progress;
                    break;
                case 1:
                    walls[i].position = tmp * Mathf.Lerp(3f, 4.9f, 1 - progress);
                    walls[i].rotation = Quaternion.Euler(0, 0, (progress * 0.5f - angle / Mathf.PI) * 180);
                    
                    break;
                case 2:
                    walls[i].position = tmp * 4.9f * (1 - progress);
                    break;
                case 3:
                    walls[i].position = tmp * 4.9f * (1 - progress);
                    walls[i].localScale = Vector3.one * (1 - progress) * (1 - progress);
                    break;
                default:
                    break;
            }
        }
    }
}
