import random
import torch


def select_action(q_net, state, epsilon, no_note_epsilon, num_actions):
    """Returns action according to epsilon-greedy policy
    w.r.t. the given q_net. Additionally possible to set
    a fixed proportion of no note events via `no_note_epsilon`.
    `epsilon` + `no_note_epsilon` must be less than 1.
    """
    
    action_type = None
    
    sample = random.random()
    
    if sample > epsilon + no_note_epsilon:
        with torch.no_grad():
            action = q_net(state).argmax().view(1,1)
        action_type = "greedy"
    elif no_note_epsilon and sample < epsilon + no_note_epsilon and sample > epsilon:
        # no note played == highest action index
        action = torch.LongTensor([[num_actions-1]])
        action_type = "fixed"
    else:
        action = torch.LongTensor([[random.randrange(0,num_actions)]])
        action_type = "random"
        
    return action, action_type