import torch


def euclidean_reward(target_mel, pred_mel, reward_min=None, reward_max=None, verbose=0):
    """Negative Euclidean norm of the difference between the two 
    flattened inputs. If reward_min or reward_max values are given, it caps
    the reward at appropriate levels.
    """

    target_mel = torch.flatten(target_mel)
    pred_mel = torch.flatten(pred_mel)
    diff = target_mel-pred_mel
    reward = -torch.norm(diff, p=2).view(1,1)

    if reward_min:
        reward = torch.max(torch.Tensor([reward, reward_min])).view(1,1)
    if reward_max:
        reward = torch.min(torch.Tensor([reward, reward_max])).view(1,1)
        
    if verbose > 1:
        print("REWARDS: Euclidean reward: {:.6f}".format(reward.item()))

    return reward