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

public class VRTK_RadialMenuController_Grip : VRTK_RadialMenuController {

    [SerializeField]
    private DeformationManager _deformationManager;

    private void Start()
    {
        menu.buttons = new List<VRTK_RadialMenu.RadialMenuButton>();
        for (int i = 0; i < _deformationManager.Deformations.Length; i++)
        {
            BaseDeformation currentDeformation = _deformationManager.Deformations[i];

            var currentButton = new VRTK_RadialMenu.RadialMenuButton
            {
                ButtonIcon = currentDeformation.DeformationIcon
            };
            currentButton.OnClick.AddListener(() => _deformationManager.SetDeformationByName(currentDeformation.GetType()));
            menu.buttons.Add(currentButton);
        }
        menu.RegenerateButtons();
    }

    protected override void OnEnable()
    {
        if (events == null)
        {
            VRTK_Logger.Error(VRTK_Logger.GetCommonMessage(VRTK_Logger.CommonMessageKeys.REQUIRED_COMPONENT_MISSING_NOT_INJECTED, "RadialMenuController", "VRTK_ControllerEvents", "events", "the parent"));
            return;
        }
        else
        {
            events.GripPressed += new ControllerInteractionEventHandler(OpenMenu);
            events.GripReleased += new ControllerInteractionEventHandler(CloseMenu);

            menu.FireHapticPulse += new HapticPulseEventHandler(AttemptHapticPulse);
        }
    }

    protected override void OnDisable()
    {
        events.GripPressed -= new ControllerInteractionEventHandler(OpenMenu);
        events.GripReleased -= new ControllerInteractionEventHandler(CloseMenu);

        menu.FireHapticPulse -= new HapticPulseEventHandler(AttemptHapticPulse);
    }

    private void OpenMenu(object sender, ControllerInteractionEventArgs e)
    {
        DoTouchpadTouched(sender, e);
        events.TouchpadPressed += new ControllerInteractionEventHandler(DoTouchpadClicked);
        events.TouchpadReleased += new ControllerInteractionEventHandler(DoTouchpadUnclicked);
        events.TouchpadAxisChanged += new ControllerInteractionEventHandler(DoTouchpadAxisChanged);
    }

    private void CloseMenu(object sender, ControllerInteractionEventArgs e)
    {
        DoTouchpadUntouched(sender, e);
        events.TouchpadPressed -= new ControllerInteractionEventHandler(DoTouchpadClicked);
        events.TouchpadReleased -= new ControllerInteractionEventHandler(DoTouchpadUnclicked);
        events.TouchpadAxisChanged -= new ControllerInteractionEventHandler(DoTouchpadAxisChanged);
    }
    protected override void DoChangeAngle(TouchAngleDeflection givenTouchAngleDeflection, object sender = null)
    {
        
        currentTad = givenTouchAngleDeflection;

        menu.HoverButton(currentTad);
        menu.ClickButton(givenTouchAngleDeflection);
        menu.UnClickButton(givenTouchAngleDeflection);
    }
}
