import math


def discretize_state(state):
    """Discretizes continuous state space to discrete state space with 14 physical values

    Args:
        state: current continuous state space

    Returns:
        discretized state space
    """

    # discretize observation space
    # https://github.com/openai/gym/wiki/BipedalWalker-v2
    obs_state_bounds = [
        (0, math.pi),  # hull_angle
        (-2, 2),  # hull_angular_velocity
        (-1, 1),  # vel_x
        (-1, 1),  # vel_y
        (0, math.pi),  # hip_joint_1_angle
        (-2, 2),  # hip_joint_1_speed
        (0, math.pi),  # knee_joint_1_angle
        (-2, 2),  # knee_joint_1_speed
        (0, 1),  # leg_1_ground_contact_flag
        (0, math.pi),  # hip_joint_2_angle
        (-2, 2),  # hip_joint_2_speed
        (0, math.pi),  # knee_joint_2_angle
        (-2, 2),  # knee_joint_2_speed
        (0, 1),  # leg_2_ground_contact_flag
    ]

    # create an empty obs_discrete_state array to store converted discrete state array
    obs_discrete_state = []

    for i in range(len(state)):
        converted_i = int(
            (state[i] - obs_state_bounds[i][0])
            / (obs_state_bounds[i][1] - obs_state_bounds[i][0])
            * 19  # 19 is arbitrary integer
        )
        obs_discrete_state.append(converted_i)

    ds = tuple(
        obs_discrete_state
    )  # convert collected discrete state array into tuple to maintain same shape
    return ds