# Path: humanrl/utils.py
# ACTION_MEANING = {
#     None: "NONE",
#     0: "NOOP",
#     1: "FIRE",
#     2: "UP",
#     3: "RIGHT",
#     4: "LEFT",
#     5: "DOWN",
#     6: "UPRIGHT",
#     7: "UPLEFT",
#     8: "DOWNRIGHT",
#     9: "DOWNLEFT",
#     10: "UPFIRE",
#     11: "RIGHTFIRE",
#     12: "LEFTFIRE",
#     13: "DOWNFIRE",
#     14: "UPRIGHTFIRE",
#     15: "UPLEFTFIRE",
#     16: "DOWNRIGHTFIRE",
#     17: "DOWNLEFTFIRE",
# }
# 
# ACTION_MEANING_TO_ACTION = {v: k for k, v in ACTION_MEANING.items()}
# 
# SAFE_ACTION_MAPPINGS = {"SpaceInvaders": {0: 0, 1: 0, 2: 2, 3: 3, 4: 2, 5: 3}}
# 
# def identity(*args):
#     if len(args) == 1:
#         return args[0]
#     return args
# 
# def shuffle(lst):
#     random.shuffle(lst)
#     return lst


import argparse
import datetime
import gzip
import itertools
import logging
import os
import os.path
import pickle
import random
import time
import numpy as np
import cv2
import humanrl.frame as frame_module
from multiprocessing.connection import Listener
from subprocess import call
from PIL import Image
from humanrl.utils import (ACTION_MEANING, ACTION_MEANING_TO_ACTION,
                           SAFE_ACTION_MAPPINGS, identity, shuffle)
    elif msg['msg'] == 'close':
        viewer_state.conn.close()
        viewer_state.close = True
    elif msg['msg'] == 'done':
        viewer_state.frame_index = 0
        proceed = input('Proceed to next episode?')

        if proceed != 'y':
            viewer_state.EXIT = True

        while True:
            k = cv2.waitKey(viewer_state.delay) & 0xFF
            if k == 255: break
        viewer_state.skip_episode = True
    else:
        print('Unknown message received: {}'.format(msg))
        viewer_state.skip_frame = True

    return viewer_state, episode


def get_safe_action(action, args):
    if args.blocking_mode == "action_pruning":
        return None
    if args.safe_action_mapping:
        if args.safe_action_mapping not in SAFE_ACTION_MAPPINGS:
            raise ValueError("{} {}".format(args.safe_action_mapping, SAFE_ACTION_MAPPINGS.keys()))
        if action not in SAFE_ACTION_MAPPINGS[args.safe_action_mapping]:
            raise ValueError(
                "{} {}".format(action, SAFE_ACTION_MAPPINGS[args.safe_action_mapping].keys()))

# Path: humanrl/utils.py
# ACTION_MEANING = {
#     None: "NONE",
#     0: "NOOP",
#     1: "FIRE",
#     2: "UP",
#     3: "RIGHT",
#     4: "LEFT",
#     5: "DOWN",
#     6: "UPRIGHT",
#     7: "UPLEFT",
#     8: "DOWNRIGHT",
#     9: "DOWNLEFT",
#     10: "UPFIRE",
#     11: "RIGHTFIRE",
#     12: "LEFTFIRE",
#     13: "DOWNFIRE",
#     14: "UPRIGHTFIRE",
#     15: "UPLEFTFIRE",
#     16: "DOWNRIGHTFIRE",
#     17: "DOWNLEFTFIRE",
# }
# 
# ACTION_MEANING_TO_ACTION = {v: k for k, v in ACTION_MEANING.items()}
# 
# SAFE_ACTION_MAPPINGS = {"SpaceInvaders": {0: 0, 1: 0, 2: 2, 3: 3, 4: 2, 5: 3}}
# 
# def identity(*args):
#     if len(args) == 1:
#         return args[0]
#     return args
# 
# def shuffle(lst):
#     random.shuffle(lst)
#     return lst


# Path: scripts/human_feedback.py
import argparse
import datetime
import gzip
import itertools
import logging
import os
import os.path
import pickle
import random
import time
import numpy as np
import cv2
import humanrl.frame as frame_module
from multiprocessing.connection import Listener
from subprocess import call
from PIL import Image
from humanrl.utils import (ACTION_MEANING, ACTION_MEANING_TO_ACTION,
                           SAFE_ACTION_MAPPINGS, identity, shuffle)

    elif msg['msg'] == 'close':
        viewer_state.conn.close()
        viewer_state.close = True
    elif msg['msg'] == 'done':
        viewer_state.frame_index = 0
        proceed = input('Proceed to next episode?')

        if proceed != 'y':
            viewer_state.EXIT = True

        while True:
            k = cv2.waitKey(viewer_state.delay) & 0xFF
            if k == 255: break
        viewer_state.skip_episode = True
    else:
        print('Unknown message received: {}'.format(msg))
        viewer_state.skip_frame = True

    return viewer_state, episode


def get_safe_action(action, args):
    if args.blocking_mode == "action_pruning":
        return None
    if args.safe_action_mapping:
        if args.safe_action_mapping not in SAFE_ACTION_MAPPINGS:
            raise ValueError("{} {}".format(args.safe_action_mapping, SAFE_ACTION_MAPPINGS.keys()))
        if action not in SAFE_ACTION_MAPPINGS[args.safe_action_mapping]:
            raise ValueError(
                "{} {}".format(action, SAFE_ACTION_MAPPINGS[args.safe_action_mapping].keys()))

        return SAFE_ACTION_MAPPINGS[args.safe_action_mapping][action]