# coding=utf-8
# Copyright 2020 The TF-Agents Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Tests for the parallel_py_environment."""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import collections
import functools
import time

import numpy as np
import tensorflow as tf  # pylint: disable=g-explicit-tensorflow-version-import

from tf_agents.environments import parallel_py_environment
from tf_agents.environments import py_environment
from tf_agents.environments import random_py_environment
from tf_agents.specs import array_spec
from tf_agents.system import system_multiprocessing as multiprocessing
from tf_agents.trajectories import time_step as ts


class SlowStartingEnvironment(random_py_environment.RandomPyEnvironment):

  def __init__(self, *args, **kwargs):
    time_sleep = kwargs.pop('time_sleep', 1.0)
    time.sleep(time_sleep)
    super(SlowStartingEnvironment, self).__init__(*args, **kwargs)


class ParallelPyEnvironmentTest(tf.test.TestCase):

  def _set_default_specs(self):
    self.observation_spec = array_spec.ArraySpec((3, 3), np.float32)
    self.time_step_spec = ts.time_step_spec(self.observation_spec)
    self.action_spec = array_spec.BoundedArraySpec([7],
                                                   dtype=np.float32,
                                                   minimum=-1.0,
                                                   maximum=1.0)

  def _make_parallel_py_environment(self,
                                    constructor=None,
                                    num_envs=2,
                                    start_serially=True,
                                    blocking=True):
    self._set_default_specs()
    constructor = constructor or functools.partial(
        random_py_environment.RandomPyEnvironment, self.observation_spec,
        self.action_spec)
    return parallel_py_environment.ParallelPyEnvironment(
        env_constructors=[constructor] * num_envs, blocking=blocking,
        start_serially=start_serially)

  def test_close_no_hang_after_init(self):
    env = self._make_parallel_py_environment()
    env.close()

  def test_get_specs(self):
    env = self._make_parallel_py_environment()
    self.assertEqual(self.observation_spec, env.observation_spec())
    self.assertEqual(self.time_step_spec, env.time_step_spec())
    self.assertEqual(self.action_spec, env.action_spec())

    env.close()

  def test_step(self):
    num_envs = 2
    env = self._make_parallel_py_environment(num_envs=num_envs)
    action_spec = env.action_spec()
    observation_spec = env.observation_spec()
    rng = np.random.RandomState()
    action = np.array([
        array_spec.sample_bounded_spec(action_spec, rng)
        for _ in range(num_envs)
    ])
    env.reset()

    # Take one step and assert observation is batched the right way.
    time_step = env.step(action)
    self.assertEqual(num_envs, time_step.observation.shape[0])
    self.assertAllEqual(observation_spec.shape, time_step.observation.shape[1:])
    self.assertEqual(num_envs, action.shape[0])
    self.assertAllEqual(action_spec.shape, action.shape[1:])