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

public class playerInput : MonoBehaviour
{

    /* playerInput CLASS
     * 
     * Gets all user input from guitar controllers
     * and forwards them to projectile generators
     * 
     * 
     * Input key:
     * HORIZONTAL -- accelerometer. -1 <= x <= 1
     * VERTICAL   -- whammy stick. -1 <= x <= 1
     * 
     * FRET X     -- 5 frets (0-4). TRUE:FALSE
     * STRUM      -- strum bar. -1 OR 0 OR 1
     * */

    private bool p1Strumming = false;
    private bool p2Strumming = false;


    //Return which frets are currently active.
    string getP1Frets(bool attacking)
    {
        string frets = "";

        //Each fret is given as 0 (off) / 1 (on)
        if (Input.GetButton("P1Fret0"))
        {
            frets += "1";
            if (attacking) gameControl.P1Stam--;
        }
        else frets += "0";

        if (Input.GetButton("P1Fret1"))
        {
            frets += "1";
            if (attacking) gameControl.P1Stam--;
        }
        else frets += "0";

        if (Input.GetButton("P1Fret2"))
        {
            frets += "1";
            if (attacking) gameControl.P1Stam--;
        }
        else frets += "0";

        if (Input.GetButton("P1Fret3"))
        {
            frets += "1";
            if (attacking) gameControl.P1Stam--;
        }
        else frets += "0";

        if (Input.GetButton("P1Fret4"))
        {
            frets += "1";
            if (attacking) gameControl.P1Stam--;
        }
        else frets += "0";

        if(gameControl.P1Stam < 0)
        {
            gameControl.P1Stam = 0;
            return "00000";
        }

        return frets;
    }

    string getP2Frets(bool attacking)
    {
        string frets = "";

        //Each fret is given as 0 (off) / 1 (on)
        if (Input.GetButton("P2Fret0"))
        {
            frets += "1";
            if(attacking) gameControl.P2Stam--;
        }
        else frets += "0";

        if (Input.GetButton("P2Fret1"))
        {
            frets += "1";
            if (attacking) gameControl.P2Stam--; 
        }
        else frets += "0";

        if (Input.GetButton("P2Fret2"))
        {
            frets += "1";
            if (attacking) gameControl.P2Stam--;
        }
        else frets += "0";

        if (Input.GetButton("P2Fret3"))
        {
            frets += "1";
            if (attacking) gameControl.P2Stam--;
        }
        else frets += "0";

        if (Input.GetButton("P2Fret4"))
        {
            frets += "1";
            if (attacking) gameControl.P2Stam--;
        }
        else frets += "0";

        if (gameControl.P2Stam < 0)
        {
            gameControl.P2Stam = 0;
            return "00000";
        }

        return frets;
    }

    // Use this for initialization
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {

        //Player 1 and Player 2 have almost
        //identical controls.
        if (Input.GetButtonDown("P1Fret0"))
        {
            Debug.Log("P1Fret0");
        }
        //PLAYER ONE

        //The five frets on the controller,
        //turned into a series of 0's (off)
        //or 1's (on) and used as a reference
        //for the combos.
        string p1FretCode;

        //'Aim' axis (if the wiggle axis isn't
        //functional on the controller used, switch
        //to whammy axis)
        float p1AimAxis = Input.GetAxis("P1Wiggle");
        if(p1AimAxis == 0)
        {
            p1AimAxis = Input.GetAxis("P1Whammy");
        }

        //Identify the box being 'aimed' at
        GameObject p1TargetSpeaker;

        if (p1AimAxis < 0)
        {
            p1TargetSpeaker = gameControl.speakers[0];
        }
        else if (p1AimAxis < 0.6)
        {
            p1TargetSpeaker = gameControl.speakers[1];
        }
        else
        {
            p1TargetSpeaker = gameControl.speakers[2];
        }

        //Remove highlight from all the player's speakers
        //and reapply highlight to the correct one
        for(int i = 0; i < 3; i++)
        {
            gameControl.speakers[i].GetComponent<MeshRenderer>().material.color = Color.white;
        }

        p1TargetSpeaker.GetComponent<MeshRenderer>().material.color = new Color32(50, 255, 50, 255);

        //Find if the player is strumming.
        if (Input.GetAxis("P1Strum") < -0.3f)
        {
            //BLOCK strum: get frets
            //and determine where is blocked
            if (p1Strumming == false)
            {
                p1Strumming = true;
                p1FretCode = getP1Frets(false);

                //Place block field in front of correct box
                p1TargetSpeaker.GetComponent<speakerShooting>().fireShield(p1FretCode);
            }
        }
        else if (Input.GetAxis("P1Strum") > 0.3f)
        {
            //ATTACK strum: get frets
            //and determine what combo to use
            if (p1Strumming == false)
            {
                p1Strumming = true;
                p1FretCode = getP1Frets(true);

                //Fire projectiles from the correct box
                p1TargetSpeaker.GetComponent<speakerShooting>().fireBullet(p1FretCode);
            }
        }
        else
        {
            //No strum: reset strumming
            p1Strumming = false;
        }




        ////PLAYER TWO

        //The five frets on the controller,
        //turned into a series of 0's (off)
        //or 1's (on) and used as a reference
        //for the combos.
        string p2FretCode;

        //'Aim' axis (if the wiggle axis isn't
        //functional on the controller used, switch
        //to whammy axis)
        float p2AimAxis = Input.GetAxis("P2Wiggle");
        if (p2AimAxis == 0)
        {
            p2AimAxis = Input.GetAxis("P2Whammy");
        }

        //Identify the box being 'aimed' at
        GameObject p2TargetSpeaker;

        if (p2AimAxis < 0)
        {
            p2TargetSpeaker = gameControl.speakers[3];
        }
        else if (p2AimAxis < 0.6)
        {
            p2TargetSpeaker = gameControl.speakers[4];
        }
        else
        {
            p2TargetSpeaker = gameControl.speakers[5];
        }

        //Remove highlight from all the player's speakers
        //and reapply highlight to the correct one
        for (int i = 3; i < 6; i++)
        {
            gameControl.speakers[i].GetComponent<MeshRenderer>().material.color = Color.white;
        }

        p2TargetSpeaker.GetComponent<MeshRenderer>().material.color = new Color32(50, 255, 50, 255);
        //Find if the player is strumming.
        if (Input.GetAxis("P2Strum") < -0.3f)
        {
            //BLOCK strum: get frets
            //and determine where is blocked
            if (p2Strumming == false)
            {
                p2Strumming = true;
                p2FretCode = getP2Frets(false);

                //Place block field in front of correct box
                p2TargetSpeaker.GetComponent<speakerShooting>().fireShield(p2FretCode);
            }
        }
        else if (Input.GetAxis("P2Strum") > 0.3f)
        {
            //ATTACK strum: get frets
            //and determine what combo to use
            if (p2Strumming == false)
            {
                p2Strumming = true;
                p2FretCode = getP2Frets(true);

                //Fire projectiles from the correct box
                p2TargetSpeaker.GetComponent<speakerShooting>().fireBullet(p2FretCode);
            }
        }
        else
        {
            //No strum: reset strumming
            p2Strumming = false;
        }

    }
}
