using UnityEngine;

public class BoutonPupitreMoulin : Button {

    [SerializeField] private int num = 0;

    private static GameObject tuyau1 = null;
    private static GameObject tuyau2 = null;
    private static GameObject tuyau3 = null;

    private int tuyau1Decal = 0;
    private int tuyau2Decal = 0;
    private int tuyau3Decal = 0;

    private static float pasDecal = 0.4f;
    public static bool win = false;
    private static Transform etincelles;

    protected override void Start() {

        base.Start();

        etincelles = transform.parent.GetComponentInChildren<ParticleSystem>().transform;

        tuyau1 = GameObject.Find("TuyauxMoulin1");
        tuyau2 = GameObject.Find("TuyauxMoulin2");
        tuyau3 = GameObject.Find("TuyauxMoulin3");

        switch (num) {
            case 1: tuyau1Decal = -2;
                    tuyau2Decal = 1;
                break;
            case 2: tuyau1Decal = 1;
                    tuyau2Decal = -1;
                    tuyau3Decal = 1;
                break;
            case 3: tuyau2Decal = 2;
                    tuyau3Decal = -1;
                break;
            default:
                    Debug.LogError("Erreur : num non compris entre 1 et 3");
                break;
        }
    }
    protected override void DoActionDown() {

        SoundManager.PlaySound("button_down");
        transform.Translate(0.03f * Vector3.back);
    }
    protected override void DoActionUp() {

        SoundManager.PlaySound("button_up");
        transform.Translate(0.03f * Vector3.forward);
        if (!win) {

            tuyau1.transform.localPosition += pasDecal * (tuyau1Decal > 0 ? Vector3.left : tuyau1Decal < 0 ? Vector3.right : Vector3.zero);
            if (tuyau1.transform.localPosition.x == -0.8f) { // 1 cran trop à droite, on se met à gauche
                tuyau1.transform.localPosition = new Vector3(0.4f, tuyau1.transform.localPosition.y, tuyau1.transform.localPosition.z);
            } else if (tuyau1.transform.localPosition.x == 0.8f) { // 1 cran trop à gauche, on se met à droite
                tuyau1.transform.localPosition = new Vector3(-0.4f, tuyau1.transform.localPosition.y, tuyau1.transform.localPosition.z);
            } else if (tuyau1.transform.localPosition.x == 1.2f) { // 2 crans trop à gauche, on se met au milieu
                tuyau1.transform.localPosition = new Vector3(0, tuyau1.transform.localPosition.y, tuyau1.transform.localPosition.z);
            }

            tuyau2.transform.localPosition += pasDecal * (tuyau2Decal > 0 ? Vector3.left : tuyau2Decal < 0 ? Vector3.right : Vector3.zero);
            if (tuyau2.transform.localPosition.x == -0.8f) { // Un cran trop à droite, on se met à gauche
                tuyau2.transform.localPosition = new Vector3(0.4f, tuyau2.transform.localPosition.y, tuyau2.transform.localPosition.z);
            } else if (tuyau2.transform.localPosition.x == 0.8f) { // Un cran trop à gauche, on se met à droite
                tuyau2.transform.localPosition = new Vector3(-0.4f, tuyau2.transform.localPosition.y, tuyau2.transform.localPosition.z);
            } else if (tuyau2.transform.localPosition.x == -1.2f) { // Deux crans trop à droite, on se met au milieu
                tuyau2.transform.localPosition = new Vector3(0, tuyau2.transform.localPosition.y, tuyau2.transform.localPosition.z);
            }

            tuyau3.transform.localPosition += pasDecal * (tuyau3Decal > 0 ? Vector3.left : tuyau3Decal < 0 ? Vector3.right : Vector3.zero);
            if (tuyau3.transform.localPosition.x == -0.8f) { // Un cran trop à droite, on se met à gauche
                tuyau3.transform.localPosition = new Vector3(0.4f, tuyau3.transform.localPosition.y, tuyau3.transform.localPosition.z);
            } else if (tuyau3.transform.localPosition.x == 0.8f) { // Un cran trop à gauche, on se met à droite
                tuyau3.transform.localPosition = new Vector3(-0.4f, tuyau3.transform.localPosition.y, tuyau3.transform.localPosition.z);
            }

            if (tuyau1.transform.localPosition.x == 0 && tuyau2.transform.localPosition.x == 0 && tuyau3.transform.localPosition.x == 0) {
                 win = true;
                 SoundManager.PlaySound("ElectriciteEnMarche");
                 Destroy(etincelles.gameObject);
            }
            if (InteruptMoulin.isOn) {
                UpdateEtincelle();
            }
        }
    }

    public static void UpdateEtincelle() {
        if (tuyau1.transform.localPosition.x != 0) {
            etincelles.localPosition = new Vector3(0, 0.58f, 0.2f);
        } else if (tuyau2.transform.localPosition.x != 0) {
            etincelles.localPosition = new Vector3(0, 0.235f, 0.2f);
        } else if (tuyau3.transform.localPosition.x != 0) {
            etincelles.localPosition = new Vector3(0, -0.156f, 0.2f);
        }
    }
}