// Update is called once per frame
	void Update () 
    {
        XboxButton btn = getButtonSelection();
        if(btn != XboxButton.NONE)
        {
            bool doPlaySound = true;
            Debug.Log(String.Format("Determing which controller {0}", controller));
            switch(controller)
            {
                case XboxController.First:
                    curPlayer = Player.Red;
                    break;
                case XboxController.Second:
                    curPlayer = Player.Blue;
                    break;
            }
            Debug.Log(String.Format("Getting button action for button={0}", btn));
            switch (btn)
            {
                case XboxButton.A:
                    Debug.Log(String.Format("Got button A for controller {0}", controller));
                    doRitualActionSelection(ActionButton.A);
                    break;
                case XboxButton.Y:
                    Debug.Log(String.Format("Got button Y for controller {0}", controller));
                    doRitualActionSelection(ActionButton.Y);
                    break;
                case XboxButton.B:
                    Debug.Log(String.Format("Got button B for controller {0}", controller));
                    doRitualActionSelection(ActionButton.B);
                    break;
                case XboxButton.X:
                    Debug.Log(String.Format("Got button X for controller {0}", controller));
                    doRitualActionSelection(ActionButton.X);
                    break;
                case XboxButton.DPadLeft:
                    Debug.Log(String.Format("Got left pad for controller {0}. Getting Ritual", controller));
                    curRitual = UIManager.Instance.SelectRitualObject(Direction.Left, curPlayer);
                    Debug.Log(String.Format("controller {0} ritual {1}", controller, curRitual));
                    break;
                case XboxButton.DPadRight:
                    Debug.Log(String.Format("Got right pad for controller {0}. Getting Ritual", controller));
                    curRitual = UIManager.Instance.SelectRitualObject(Direction.Right, curPlayer);
                    Debug.Log(String.Format("controller {0} ritual {1}", controller, curRitual));
                    break;
                default:
                    Debug.Log(String.Format("Got button {0} from controller {1} however {2} it is unmapped", btn, controller, btn));
                    doPlaySound = false;
                    break;
            }
            if(doPlaySound)
            {
                playSound();
            }
        }
	}
 public void ClearPlayerSelections(Player player)
 {
     if (player == Player.Red)
     {
         redjersey.PlayObjectInteraction(RitualObjAnimation.Deselected);
         redbobbleHead.PlayObjectInteraction(RitualObjAnimation.Deselected);
         redpizzaBox.PlayObjectInteraction(RitualObjAnimation.Deselected);
         redbuddha.PlayObjectInteraction(RitualObjAnimation.Deselected);
         redfoamFinger.PlayObjectInteraction(RitualObjAnimation.Deselected);
         redsocks.PlayObjectInteraction(RitualObjAnimation.Deselected);
         /*
         foreach (var canvas in redjerseyAnimations)
         {
             canvas.gameObject.SetActive(false);
         }
         foreach (var canvas in redbobbleHeadAnimations)
         {
             canvas.gameObject.SetActive(false);
         }
         foreach (var canvas in redfoamFingerAnimations)
         {
             canvas.gameObject.SetActive(false);
         }
         foreach (var canvas in redpizzaBoxAnimations)
         {
             canvas.gameObject.SetActive(false);
         }
         foreach (var canvas in redsocksAnimations)
         {
             canvas.gameObject.SetActive(false);
         }
         foreach (var canvas in redbuddhaAnimations)
         {
             canvas.gameObject.SetActive(false);
         }
         */
         currentSelectionRed = RitualObjectId.NONE;
     }
     else if (player == Player.Blue)
     {
         bluejersey.PlayObjectInteraction(RitualObjAnimation.Deselected);
         bluebobbleHead.PlayObjectInteraction(RitualObjAnimation.Deselected);
         bluepizzaBox.PlayObjectInteraction(RitualObjAnimation.Deselected);
         bluebuddha.PlayObjectInteraction(RitualObjAnimation.Deselected);
         bluefoamFinger.PlayObjectInteraction(RitualObjAnimation.Deselected);
         bluesocks.PlayObjectInteraction(RitualObjAnimation.Deselected);
         /*
         foreach (var canvas in bluejerseyAnimations)
         {
             canvas.gameObject.SetActive(false);
         }
         foreach (var canvas in bluebobbleHeadAnimations)
         {
             canvas.gameObject.SetActive(false);
         }
         foreach (var canvas in bluefoamFingerAnimations)
         {
             canvas.gameObject.SetActive(false);
         }
         foreach (var canvas in bluepizzaBoxAnimations)
         {
             canvas.gameObject.SetActive(false);
         }
         foreach (var canvas in bluesocksAnimations)
         {
             canvas.gameObject.SetActive(false);
         }
         foreach (var canvas in bluebuddhaAnimations)
         {
             canvas.gameObject.SetActive(false);
         }
         */
         currentSelectionBlue = RitualObjectId.NONE;
     }
 }
    void DisplaySelection(RitualObjectId ritualObj, RitualObjAnimation anim, Player play)
    {
        if (play == Player.Red)
        {
            switch(ritualObj)
            {
                case RitualObjectId.BobbleHead:
                    //foreach (var ui in redbobbleHeadAnimations)
                    //{
                    //    ui.gameObject.SetActive(false);
                    //}
                    //redbobbleHeadAnimations[(int)anim].gameObject.SetActive(true);
                    redbobbleHead.PlayObjectInteraction(anim);
                    break;
                case RitualObjectId.Buddha:
                    //foreach (var ui in redbuddhaAnimations)
                    //{
                    //    ui.gameObject.SetActive(false);
                    //}
                    //redbuddhaAnimations[(int)anim].gameObject.SetActive(true);
                    redbuddha.PlayObjectInteraction(anim);
                    break;
                case RitualObjectId.FoamFinger:
                    //foreach (var ui in redfoamFingerAnimations)
                    //{
                    //    ui.gameObject.SetActive(false);
                    //}
                    //redfoamFingerAnimations[(int)anim].gameObject.SetActive(true);
                    redfoamFinger.PlayObjectInteraction(anim);
                    break;
                case RitualObjectId.Jersey:
                    //foreach (var ui in redjerseyAnimations)
                    //{
                    //    ui.gameObject.SetActive(false);
                    //}
                    //redjerseyAnimations[(int)anim].gameObject.SetActive(true);
                    redjersey.PlayObjectInteraction(anim);
                    break;
                case RitualObjectId.PizzaBox:
                    //foreach (var ui in redpizzaBoxAnimations)
                    //{
                    //    ui.gameObject.SetActive(false);
                    //}
                    //redpizzaBoxAnimations[(int)anim].gameObject.SetActive(true);
                    redpizzaBox.PlayObjectInteraction(anim);
                    break;
                case RitualObjectId.Socks:
                    //foreach (var ui in redsocksAnimations)
                    //{
                    //    ui.gameObject.SetActive(false);
                    //}
                    //redsocksAnimations[(int)anim].gameObject.SetActive(true);
                    redsocks.PlayObjectInteraction(anim);
                    break;
                default:
                    break;

            }
        }
        else if (play == Player.Blue)
        {
            switch (ritualObj)
            {
                case RitualObjectId.BobbleHead:
                    //foreach (var ui in bluebobbleHeadAnimations)
                    //{
                    //    ui.gameObject.SetActive(false);
                    //}
                    //bluebobbleHeadAnimations[(int)anim].gameObject.SetActive(true);
                    bluebobbleHead.PlayObjectInteraction(anim);
                    break;
                case RitualObjectId.Buddha:
                    //foreach (var ui in bluebuddhaAnimations)
                    //{
                    //    ui.gameObject.SetActive(false);
                    //}
                    //bluebuddhaAnimations[(int)anim].gameObject.SetActive(true);
                    bluebuddha.PlayObjectInteraction(anim);
                    break;
                case RitualObjectId.FoamFinger:
                    //foreach (var ui in bluefoamFingerAnimations)
                    //{
                    //    ui.gameObject.SetActive(false);
                    //}
                    //bluefoamFingerAnimations[(int)anim].gameObject.SetActive(true);
                    bluefoamFinger.PlayObjectInteraction(anim);
                    break;
                case RitualObjectId.Jersey:
                    //foreach (var ui in bluejerseyAnimations)
                    //{
                    //    ui.gameObject.SetActive(false);
                    //}
                    //bluejerseyAnimations[(int)anim].gameObject.SetActive(true);
                    bluejersey.PlayObjectInteraction(anim);
                    break;
                case RitualObjectId.PizzaBox:
                    //foreach (var ui in bluepizzaBoxAnimations)
                    //{
                    //    ui.gameObject.SetActive(false);
                    //}
                    //bluepizzaBoxAnimations[(int)anim].gameObject.SetActive(true);
                    bluepizzaBox.PlayObjectInteraction(anim);
                    break;
                case RitualObjectId.Socks:
                    //foreach (var ui in bluesocksAnimations)
                    //{
                    //    ui.gameObject.SetActive(false);
                    //}
                    //bluesocksAnimations[(int)anim].gameObject.SetActive(true);
                    bluesocks.PlayObjectInteraction(anim);
                    break;
                default:
                    break;

            }
        }
    }
    public RitualObjectId SelectRitualObject(Direction direction, Player play)
    {
        RitualObjectId tempSelection = RitualObjectId.NONE;
        if (play == Player.Red && acceptingInput)
        {
            if (currentSelectionRed == RitualObjectId.NONE)
            {
                tempSelection = RitualObjectId.Jersey;
            }
            else if (currentSelectionRed == RitualObjectId.Socks && direction == Direction.Right)
            {
                DisplaySelection(currentSelectionRed, RitualObjAnimation.Deselected, play);
                tempSelection = RitualObjectId.Jersey;
            }
            else if (currentSelectionRed == RitualObjectId.Jersey && direction == Direction.Left)
            {
                DisplaySelection(currentSelectionRed, RitualObjAnimation.Deselected, play);
                tempSelection = RitualObjectId.Socks;
            }
            else if (direction == Direction.Right)
            {
                DisplaySelection(currentSelectionRed, RitualObjAnimation.Deselected, play);
                tempSelection = (RitualObjectId)(((int)currentSelectionRed)+1);
            }
            else if (direction == Direction.Left)
            {
                DisplaySelection(currentSelectionRed, RitualObjAnimation.Deselected, play);
                tempSelection = (RitualObjectId)(((int)currentSelectionRed) - 1);
            }
            ClearPlayerSelections(play);
            currentSelectionRed = tempSelection;
        }
        else if (play == Player.Blue && acceptingInput)
        {
            if (currentSelectionBlue == RitualObjectId.NONE)
            {
                tempSelection = RitualObjectId.Jersey;
            }
            else if (currentSelectionBlue == RitualObjectId.Socks && direction == Direction.Right)
            {
                DisplaySelection(currentSelectionBlue, RitualObjAnimation.Deselected, play);
                tempSelection = RitualObjectId.Jersey;
            }
            else if (currentSelectionBlue == RitualObjectId.Jersey && direction == Direction.Left)
            {
                DisplaySelection(currentSelectionBlue, RitualObjAnimation.Deselected, play);
                tempSelection = RitualObjectId.Socks;
            }
            else if (direction == Direction.Right)
            {
                DisplaySelection(currentSelectionBlue, RitualObjAnimation.Deselected, play);
                tempSelection = (RitualObjectId)(((int)currentSelectionBlue) + 1);
            }
            else if (direction == Direction.Left)
            {
                DisplaySelection(currentSelectionBlue, RitualObjAnimation.Deselected, play);
                tempSelection = (RitualObjectId)(((int)currentSelectionBlue) - 1);
            }

            ClearPlayerSelections(play);
            currentSelectionBlue = tempSelection;
        }
        DisplaySelection(tempSelection, RitualObjAnimation.Selected, play);
        return tempSelection;
    }