using UnityEngine;

public class CabaneFusee : InteractableObject {

    [SerializeField]
    private Camera cameraLookAtMe = null;

    private const float cabaneSpeed = 5f;

    private GameObject[] burners = null;

    private bool decolling = false;

    protected override void Start() {
        base.Start();
        cameraLookAtMe.enabled = false;
        burners = GameObject.FindGameObjectsWithTag("Burner");
        burners[0].SetActive(false);
        burners[1].SetActive(false);
    }

    protected override void Update() {
        if (CatchObjects.catchedObject != null && CatchObjects.objectsToCatch > 0) {
            base.Update();
        }
        if (decolling) {
            transform.parent.Translate(Vector3.up * cabaneSpeed * Time.deltaTime);
        }
    }

    protected override void DoActionUp() {
        SoundManager.PlaySound("craft");
        if (CatchObjects.catchedObject == CatchObjects.m_reacteur) {
            CatchObjects.m_reacteur.transform.localPosition = new Vector3(1.325914f, 0.6053242f, -0.4426398f);
            burners[0].SetActive(true);
            burners[1].SetActive(true);
        } else if (CatchObjects.catchedObject == CatchObjects.m_aile01) {
            CatchObjects.m_aile01.transform.localEulerAngles = Vector3.zero;
            CatchObjects.m_aile01.transform.localPosition = new Vector3(3.285106f, 2.28f, 0);
        } else if (CatchObjects.catchedObject == CatchObjects.m_aile02) {
            CatchObjects.m_aile02.transform.localEulerAngles = 180 * Vector3.up;
            CatchObjects.m_aile02.transform.localPosition = new Vector3(-0.6982467f, 2.28f, 0.1677259f);
        }
        CatchObjects.catchedObject.GetComponent<CatchObjects>().wasCatched = true;
        CatchObjects.catchedObject = null;
        CatchObjects.objectsToCatch--;
        myMat.shader = originalShader;
        if (CatchObjects.objectsToCatch == 0) {
            // On a tout mis, on décolle
            FadeManager.DoFadeOut();
            SoundManager.PlaySound("rocket");
            Invoke("Decolling", 7f);
        }
    }

    void Decolling() {
        Camera.main.enabled = false;
        cameraLookAtMe.enabled = true;
        decolling = true;
        Destroy(GameObject.Find("CurrentDay"));
        FadeManager.DoFadeIn();
        Invoke("RefadeDerriere", 10f);
    }

    void RefadeDerriere() {
        FadeManager.DoFadeOut();
        Invoke("Cinematic", FadeManager.fadeDurationStatic + 2);
    }

    void Cinematic() {
        Application.LoadLevel("Tests");
    }
}