using System;
using Intrepid.Integration.Input;
using UnityEngine;

namespace Intrepid.Gameplay.Entities.Player.PDA.Inputs
{
    public class PDAInputActions : PDAInputs
    {
        public DPad DPad { get; private set; }
        public Func<bool> Cancel { get; private set; }
        public Func<bool> Enter { get; private set; }
        public Func<bool> ExitPDA { get; private set; }
        public Func<bool> ShoulderLeft { get; private set; }
        public Func<bool> ShoulderRight { get; private set; }
        public Func<float> TriggerLeft { get; private set; }
        public Func<float> TriggerRight { get; private set; }
        public Func<bool> HasTriggerInput { get; private set; }
        
        public static PDAInputActions BuildPDAInputActions( )
        {
            var inputActions = InputService.Instance.InputActions.PDA;
            return new PDAInputActions
            {
                DPad = DPadInputActions.BuildDPadInputActions(),
                Cancel = () => inputActions.Cancel.WasPressedThisFrame(),
                Enter = () => inputActions.Enter.WasPressedThisFrame(),
                ExitPDA = () => inputActions.ExitPDA.WasPressedThisFrame(),

                ShoulderLeft = () => inputActions.ShoulderLeft.IsPressed(),
                ShoulderRight = () => inputActions.ShoulderRight.IsPressed(),

                TriggerLeft = () => inputActions.TriggerLeft.ReadValue<float>(),
                TriggerRight = () => inputActions.TriggerRight.ReadValue<float>(),

                HasTriggerInput = () => inputActions.TriggerLeft.IsPressed() || inputActions.TriggerRight.IsPressed(),
            };
        }
    }

    public class DPadInputActions : DPad
    {
        public Func<bool> Up { get; private set; }
        public Func<bool> Down { get; private set; }
        public Func<bool> Left { get; private set; }
        public Func<bool> Right { get; private set; }

        public static DPadInputActions BuildDPadInputActions()
        {
            var inputActions = InputService.Instance.InputActions.PDA;
            return new DPadInputActions
            {
                Right = () => inputActions.Dpad.WasPressedThisFrame()
                              && inputActions.Dpad.ReadValue<Vector2>().x > 0.5f,
                Left = () => inputActions.Dpad.WasPressedThisFrame()
                             && inputActions.Dpad.ReadValue<Vector2>().x < -0.5f,
                Up = () => inputActions.Dpad.WasPressedThisFrame() &&
                           inputActions.Dpad.ReadValue<Vector2>().y > 0.5f,
                Down = () => inputActions.Dpad.WasPressedThisFrame()
                             && inputActions.Dpad.ReadValue<Vector2>().y < -0.5f,
            };
        }
    }
}