import numpy as np
from gym import utils
from gym.envs.mujoco import mujoco_env

class HalfCheetahVelEnv(mujoco_env.MujocoEnv, utils.EzPickle):
    def __init__(self, goal_vel=3.0, backward=False, task_one_hot_id=None):
        self._goal_vel = goal_vel
        self.backward = backward
        self.task_one_hot_id = task_one_hot_id
        mujoco_env.MujocoEnv.__init__(self, 'half_cheetah.xml', 5)
        utils.EzPickle.__init__(self)

    def step(self, action):
        if not hasattr(self, "init_zpos"):
            self.init_zpos = self.sim.data.get_body_xpos('torso')[2]
        # self.render()
        xposbefore = self.sim.data.qpos[0]
        self.do_simulation(action, self.frame_skip)
        xposafter = self.sim.data.qpos[0]
        ob = self._get_obs()
        reward_ctrl = - 0.1 * np.square(action).sum()
        if not self.backward:
            reward_run = (xposafter - xposbefore)/self.dt
        else:
            reward_run = -(xposafter - xposbefore)/self.dt
        reward_run = min(reward_run, self._goal_vel)
        reward_jump = 15.0*(self.sim.data.get_body_xpos('torso')[2] - self.init_zpos) #zpos
        reward = reward_ctrl + reward_run
        done = False
        return ob, reward, done, dict(reward_run=reward_run, reward_ctrl=reward_ctrl, reward_jump=reward_jump)

    # def _get_obs(self):
    #     return np.concatenate([
    #         self.sim.data.qpos.flat[1:],
    #         self.sim.data.qvel.flat,
    #     ])

    def _get_obs(self):
        if self.task_one_hot_id is not None:
            return np.concatenate([
                self.sim.data.qpos.flat[1:],
                # self.sim.data.qpos.flat,
                [self.sim.data.get_body_xpos('torso')[2]],
                self.sim.data.qvel.flat,
                self.task_one_hot_id
            ])
        return np.concatenate([
            self.sim.data.qpos.flat[1:],
            # self.sim.data.qpos.flat,
            [self.sim.data.get_body_xpos('torso')[2]],
            self.sim.data.qvel.flat,
        ])

    def reset_model(self):
        qpos = self.init_qpos + self.np_random.uniform(low=-.1, high=.1, size=self.model.nq)
        qvel = self.init_qvel + self.np_random.randn(self.model.nv) * .1
        self.set_state(qpos, qvel)
        self.init_zpos = self.sim.data.get_body_xpos('torso')[2]
        return self._get_obs()

    def viewer_setup(self):
        self.viewer.cam.distance = self.model.stat.extent * 0.5
