from .._BlackJack import BlackJackCPP

import gym
import ctypes
import numpy as np
from gym import spaces


class BlackJack(gym.Env):
    def __init__(self, natural=False):
        self.env = BlackJackCPP(natural)
        self.action_space = spaces.Discrete(2)
        self.observation_space = spaces.Tuple((
            spaces.Discrete(32),
            spaces.Discrete(11),
            spaces.Discrete(2)
        ))
        self.state = None
        self.natural = natural

    def seed(self, seed=None):
        if seed is None:
            return [self.env.get_seed()]
        else:
            if not isinstance(seed, ctypes.c_uint32):
                seed = ctypes.c_uint32(seed).value
            self.env.set_seed(seed)
            return [seed]

    def step(self, action):
        assert self.action_space.contains(action)
        state, reward, done = self.env.step(action)
        self.state = np.array(state)
        return self.state, reward, done, {}

    def render(self, mode='human'):
        return None

    def reset(self):
        self.state = np.array(self.env.reset())
        return self.state

[((12, 28, 12, 46), 'gym.spaces.Discrete', 'spaces.Discrete', ({(12, 44, 12, 45): '2'}, {}), '(2)', False, 'from gym import spaces\n'), ((33, 21, 33, 36), 'numpy.array', 'np.array', ({(33, 30, 33, 35): 'state'}, {}), '(state)', True, 'import numpy as np\n'), ((14, 12, 14, 31), 'gym.spaces.Discrete', 'spaces.Discrete', ({(14, 28, 14, 30): '32'}, {}), '(32)', False, 'from gym import spaces\n'), ((15, 12, 15, 31), 'gym.spaces.Discrete', 'spaces.Discrete', ({(15, 28, 15, 30): '11'}, {}), '(11)', False, 'from gym import spaces\n'), ((16, 12, 16, 30), 'gym.spaces.Discrete', 'spaces.Discrete', ({(16, 28, 16, 29): '2'}, {}), '(2)', False, 'from gym import spaces\n'), ((26, 23, 26, 44), 'ctypes.c_uint32', 'ctypes.c_uint32', ({(26, 39, 26, 43): 'seed'}, {}), '(seed)', False, 'import ctypes\n')]