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

public class speakerShooting : MonoBehaviour
{
    //Method called on when object leaves screen

    //Creates an instance of the prefab so it is spawned in when payer shoots multiple times
    public Rigidbody Bullet_prefab;

    //Prefab shield which will be created when blocking is activated.
    public GameObject pfbShield;

    //Speed bullet travels at
    public float Bullet_speed = 1000f;
    //Cooldown
    float bulletCD = 1f;
    float shieldCD = 0.3f;

    //The time last shot is fired
    float lastBullet = 0.0f;
    float lastShield = 0.0f;

    //Substitute 'damaged' meshes
    public Mesh damagedMesh;
    public Mesh brokenMesh;


    //Audio source for chords
    public AudioSource Chords;
    //Audio clips for chords
    public AudioClip As2; //
    public AudioClip A2; //
    public AudioClip B2; //
    public AudioClip Cs2; //
    public AudioClip Cs3;//
    public AudioClip C2; //
    public AudioClip C3;//
    public AudioClip Ds2;//
    public AudioClip Ds3;//
    public AudioClip D2; //
    public AudioClip D3;//
    public AudioClip E2; //
    public AudioClip E3;//
    public AudioClip Fs2;//
    public AudioClip F2; //
    public AudioClip F3;//
    public AudioClip Gs2;//
    public AudioClip G2; //


    // Update is called once per frame
    void Update()
    {
        //Modify the renderer to display damage.
        if(gameObject.tag == "leftSpeaker")
        {
            if(gameControl.P1HP < 50)
            {
                gameObject.GetComponent<MeshFilter>().mesh = damagedMesh;
            }
        } else
        {
            if (gameControl.P2HP < 50)
            {
                gameObject.GetComponent<MeshFilter>().mesh = damagedMesh;
            }
        }
    }



    public void fireBullet(string frets)
    {
        // Checks shooting cooldown

        if (Time.time - lastBullet < bulletCD)
        {
            return;
        }

        //Check we're not dealing with a blank fret
        if (frets == "00000")
        {
            return;
        }

        //Switch for audio clips
        switch (frets)
        {
            case "10000":
                Chords.PlayOneShot(A2);
                break;
            case "01000":
            case "11110":
                Chords.PlayOneShot(B2);
                break;
            case "00100":
            case "11011":
                Chords.PlayOneShot(C2);
                break;
            case "00010":
            case "11101":
                Chords.PlayOneShot(D2);
                break;
            case "00001":
            case "01111":
                Chords.PlayOneShot(E2);
                break;
            case "11000":
            case "10111":
                Chords.PlayOneShot(As2);
                break;
            case "10100":
            case "10011":
                Chords.PlayOneShot(Cs2);
                break;
            case "10010":
            case "01011":
                Chords.PlayOneShot(Ds2);
                break;
            case "10001":
            case "01110":
                Chords.PlayOneShot(Fs2);
                break;
            case "01100":
            case "10101":
                Chords.PlayOneShot(Cs3);
                break;
            case "01010":
            case "10110":
                Chords.PlayOneShot(Ds3);
                break;
            case "01001":
            case "11010":
                Chords.PlayOneShot(C3);
                break;
            case "00110":
            case "01101":
                Chords.PlayOneShot(F3);
                break;
            case "00101":
            case "00111":
                Chords.PlayOneShot(D3);
                break;
            case "00011":
            case "11111":
                Chords.PlayOneShot(E3);
                break;
            case "11100":
            case "11001":
                Chords.PlayOneShot(Gs2);
                break;


        }

        //Creates instance of Bullet Prefab
        Rigidbody Bulletprefab = Instantiate(Bullet_prefab, new Vector2(transform.position.x, transform.position.y),
        Quaternion.identity) as Rigidbody;

        //Pass the frets to the projectile so that it
        //can be blocked
        Bulletprefab.gameObject.GetComponent<projectileColliding>().fretID = frets;


        SpriteRenderer[] renderers = Bulletprefab.gameObject.GetComponentsInChildren<SpriteRenderer>();

        //Fire right if the speaker is on the left.
        //Fire left if the speaker is on the right.
        if (gameObject.tag == "leftSpeaker")
        {
            Bulletprefab.tag = "leftProjectile";
        }
        else
        {
            for (int i = 0; i < renderers.Length; i++)
            {
                renderers[i].flipX = true;
            }
            Bulletprefab.tag = "rightProjectile";
        }


        if (frets.Substring(0, 1) == "1")
        {
            renderers[0].color = new Color(0, 255, 0);
        }
        else
        {
            renderers[0].color = Color.black;
        }
        if (frets.Substring(1, 1) == "1")
        {
            renderers[1].color = new Color(255, 0, 0);
        }
        else
        {
            renderers[1].color = Color.black;
        }
        if (frets.Substring(2, 1) == "1")
        {
            renderers[2].color = new Color(255, 255, 0);
        }
        else
        {
            renderers[2].color = Color.black;
        }
        if (frets.Substring(3, 1) == "1")
        {
            renderers[3].color = new Color(0, 0, 255);
        }
        else
        {
            renderers[3].color = Color.black;
        }
        if (frets.Substring(4, 1) == "1")
        {
            renderers[4].color = new Color32(255, 180, 0, 255);
        }
        else
        {
            renderers[4].color = Color.black;
        }

        // Save the time to the last shot so it can be used in,
        // the cool down algorithim
        lastBullet = Time.time;

    }

    //Code above taken from tutorial on shooting and advanced shooting tutuorials on moodle


    public void fireShield(string frets)
    {
        // Checks shooting cooldown

        if (Time.time - lastShield < shieldCD)
        {
            return;
        }

        //Check we're not dealing with a blank fret
        if (frets == "00000")
        {
            return;
        }

        //Create a new shield object
        GameObject newShield = Instantiate(pfbShield, transform.position, Quaternion.identity);

        //Pass the frets to the shield so it knows what it can block
        newShield.GetComponent<projectileColliding>().fretID = frets;

        SpriteRenderer[] renderers = newShield.gameObject.GetComponentsInChildren<SpriteRenderer>();

        //Place in front of the speaker
        if (gameObject.tag == "leftSpeaker")
        {
            newShield.transform.position = newShield.transform.position + Vector3.right * 0.3f;
            newShield.tag = "leftShield";
        }
        else
        {
            for (int i = 0; i < renderers.Length; i++)
            {
                renderers[i].flipX = true;
            }
            newShield.transform.position = newShield.transform.position + Vector3.left * 0.3f;
            newShield.tag = "rightShield";
        }


        if (frets.Substring(0, 1) == "1")
        {
            renderers[0].color = new Color(0, 255, 0);
        }
        else
        {
            renderers[0].color = Color.black;
        }
        if (frets.Substring(1, 1) == "1")
        {
            renderers[1].color = new Color(255, 0, 0);
        }
        else
        {
            renderers[1].color = Color.black;
        }
        if (frets.Substring(2, 1) == "1")
        {
            renderers[2].color = new Color(255, 255, 0);
        }
        else
        {
            renderers[2].color = Color.black;
        }
        if (frets.Substring(3, 1) == "1")
        {
            renderers[3].color = new Color(0, 0, 255);
        }
        else
        {
            renderers[3].color = Color.black;
        }
        if (frets.Substring(4, 1) == "1")
        {
            renderers[4].color = new Color32(255, 180, 0, 255);
        }
        else
        {
            renderers[4].color = Color.black;
        }

        // Save the time to the last shot so it can be used in,
        // the cool down algorithim
        lastShield = Time.time;

    }
}