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

public class SaijaScript : MonoBehaviour {

    public AudioClip ded;
    public GameObject puffObj;
    public Sprite deadSprite;

    public float moveSpeed;
    public float riseSpeed;
    public int saijaState;
    public float fadeSpeed;
    public float spawnTime;
    float spawnTimer;

    public List<GameObject> saijaList;
    
    public Vector3 moveVect;
    Vector3 startPos;
    
    int moveDir = 1;
    
    Rigidbody rb;

    SpriteRenderer sr;
    Collider c;


	// Use this for initialization
	void Start () {

        GameObject[] s_list = GameObject.FindGameObjectsWithTag("saija");
        for(int i=0; i<3; i++)
        {
            saijaList.Add(s_list[i]);
        }
        rb = GetComponent<Rigidbody>();
        saijaState = 0;

        startPos = transform.position;

        sr = GetComponent<SpriteRenderer>();
        c = GetComponent<Collider>();
	}
	
	// Update is called once per frame
	void Update () {

        switch(saijaState) {
            case 0:
                // Inactive state
                break;
                
            case 1:
                
                if (sr.color.a < 1f)
                {
                    sr.color = new Color(1f, 1f, 1f, sr.color.a + fadeSpeed);
                    c.enabled = true;
                }
                moveVect = Vector3.zero;

                if(moveDir == -1)
                {
                    transform.position = new Vector3(transform.position.x, transform.position.y, -1f);
                }
                
                break;
                
            case 2:
                
                moveVect = new Vector3(moveDir * moveSpeed, 0f, 0f);

                break;

            case 3:

                if(sr.color.a > 0f)
                {
                    sr.color = new Color(1f,1f,1f, sr.color.a - fadeSpeed);
                }
                if(spawnTimer < spawnTime)
                {
                    spawnTimer += Time.deltaTime;
                } else
                {
                    transform.position = startPos;
                    transform.eulerAngles = new Vector3(0f, 0f, 0f);
                    rb.constraints = ~RigidbodyConstraints.FreezeAll;
                    saijaState = 1;

                    MasterScript m = GameObject.Find("MasterObj").GetComponent<MasterScript>();

                    m.cameraPlace = 0;
                    m.camLerping = true;
                    m.camLerped = 0f;
                    m.camLerpTarget = new Vector3(-5.5f, 0.02f, -10f);
                    m.camInitiPos = new Vector3(Camera.main.transform.position.x, 0.02f, -10f);

                }
                break;

            case 4:

                if(sr.color.a > 0f)
                {
                    sr.color = new Color(1f, 1f, 1f, sr.color.a - fadeSpeed);
                }
                transform.position += Vector3.up * riseSpeed;


                break;
        }
	}
    
    void FixedUpdate() {
        if (saijaState != 4)
        {
            rb.MovePosition(transform.position + moveVect);
        } else
        {
            rb.useGravity = false;
        }
    }

    public void Victory()
    {
        sr.sprite = deadSprite;
        saijaState = 4;
        GetComponent<AudioSource>().PlayOneShot(ded, 1f);
    }

    public void Die()
    {
        Instantiate(puffObj, transform.position, transform.rotation);
        spawnTimer = 0f;
        saijaState = 3;
        rb.constraints = RigidbodyConstraints.FreezeAll;
        c.enabled = false;
    }
    
    void OnCollisionEnter(Collision col) {

        if(col.gameObject.tag == "spikes") {
            foreach (GameObject g in saijaList)
            {
                SaijaScript s = g.GetComponent<SaijaScript>();
                s.Die();
            }
        }
        
        if(col.gameObject.tag == "wall") {
            moveDir *= -1;
            float x = transform.localScale.x * (-1);
            transform.localScale = new Vector3(x, 1f, 1f);
            if(moveDir == 1) {
                transform.position = new Vector3(transform.position.x, transform.position.y, 0f);
            } else {
                transform.position = new Vector3(transform.position.x, transform.position.y, -1f);
            }
            
        }
    }

    void OnTriggerEnter(Collider c)
    {
        if (c.gameObject.name == "Tamagushi" && saijaState == 1)
        {
            foreach (GameObject g in saijaList)
            {
                SaijaScript s = g.GetComponent<SaijaScript>();
                s.saijaState = 2;
            }
        }
    }
}
