#!/usr/bin/env python3

from collections import defaultdict

import torch
import copy

from habitat.config import Config as CN


class RolloutStorage:
    r"""Class for storing rollout information for RL trainers.

    """

    def __init__(
        self,
        num_steps,
        num_envs,
        observation_space,
        action_space,
        recurrent_hidden_state_size,
        device,
        num_recurrent_layers=1,

    ):
        self.observations = {}
        self.device = device

        for sensor in observation_space.spaces:
            self.observations[sensor] = torch.zeros(
                num_steps + 1,
                num_envs,
                *observation_space.spaces[sensor].shape,
                device = self.device,
            )

        self.recurrent_hidden_states = torch.zeros(
            num_steps + 1,
            num_recurrent_layers,
            num_envs,
            recurrent_hidden_state_size,
            device = self.device,
        )

        self.rewards = torch.zeros(num_steps, num_envs, 1,device = self.device)
        self.value_preds = torch.zeros(num_steps + 1, num_envs, 1,device = self.device)
        self.returns = torch.zeros(num_steps + 1, num_envs, 1,device = self.device)

        self.action_log_probs = torch.zeros(num_steps, num_envs, 1,device = self.device)
        if action_space.__class__.__name__ == "ActionSpace":
            action_shape = 1
        else:
            action_shape = action_space.shape[0]

        self.actions = torch.zeros(num_steps, num_envs, action_shape,device = self.device)
        self.prev_actions = torch.zeros(num_steps + 1, num_envs, action_shape,device = self.device)
        if action_space.__class__.__name__ == "ActionSpace":
            self.actions = self.actions.long()
            self.prev_actions = self.prev_actions.long()

        self.masks = torch.ones(num_steps + 1, num_envs, 1,device = self.device)

        self.num_steps = num_steps
        self.step = 0

    def to(self, device):
        for sensor in self.observations:
            self.observations[sensor] = self.observations[sensor].to(device)

        self.recurrent_hidden_states = self.recurrent_hidden_states.to(device)
        self.rewards = self.rewards.to(device)
        self.value_preds = self.value_preds.to(device)
        self.returns = self.returns.to(device)
        self.action_log_probs = self.action_log_probs.to(device)
        self.actions = self.actions.to(device)
        self.prev_actions = self.prev_actions.to(device)
        self.masks = self.masks.to(device)

    def insert(
        self,
        observations,
        recurrent_hidden_states,
        actions,
        action_log_probs,
        value_preds,
        rewards,
        masks,
    ):
        for sensor in observations:
            self.observations[sensor][self.step + 1].copy_(
                observations[sensor]
            )
        self.recurrent_hidden_states[self.step + 1].copy_(
            recurrent_hidden_states
        )
        self.actions[self.step].copy_(actions)
        self.prev_actions[self.step + 1].copy_(actions)
        self.action_log_probs[self.step].copy_(action_log_probs)
        self.value_preds[self.step].copy_(value_preds)
        self.rewards[self.step].copy_(rewards)
        self.masks[self.step + 1].copy_(masks)

        self.step = (self.step + 1) % self.num_steps

    def after_update(self):
        for sensor in self.observations:
            self.observations[sensor][0].copy_(self.observations[sensor][-1])

        self.recurrent_hidden_states[0].copy_(self.recurrent_hidden_states[-1])
        self.masks[0].copy_(self.masks[-1])
        self.prev_actions[0].copy_(self.prev_actions[-1])

    def compute_returns(self, next_value, use_gae, gamma, tau):
        if use_gae:
            self.value_preds[-1] = next_value
            gae = 0
            for step in reversed(range(self.rewards.size(0))):
                delta = (
                    self.rewards[step]
                    + gamma * self.value_preds[step + 1] * self.masks[step + 1]
                    - self.value_preds[step]
                )
                gae = delta + gamma * tau * self.masks[step + 1] * gae
                self.returns[step] = gae + self.value_preds[step]
        else:
            self.returns[-1] = next_value
            for step in reversed(range(self.rewards.size(0))):
                self.returns[step] = (
                    self.returns[step + 1] * gamma * self.masks[step + 1]
                    + self.rewards[step]
                )

    def recurrent_generator(self, advantages, num_mini_batch):
        #rewards: (num_steps, num_envs, 1)

        num_processes = self.rewards.size(1)
        assert num_processes >= num_mini_batch, (
            "Trainer requires the number of processes ({}) "
            "to be greater than or equal to the number of "
            "trainer mini batches ({}).".format(num_processes, num_mini_batch)
        )
        num_envs_per_batch = num_processes // num_mini_batch
        perm = torch.randperm(num_processes)
        for start_ind in range(0, num_processes, num_envs_per_batch):
            observations_batch = defaultdict(list)

            recurrent_hidden_states_batch = []
            actions_batch = []
            prev_actions_batch = []
            value_preds_batch = []
            return_batch = []
            masks_batch = []
            old_action_log_probs_batch = []
            adv_targ = []

            for offset in range(num_envs_per_batch):
                ind = perm[start_ind + offset]

                for sensor in self.observations:
                    observations_batch[sensor].append(
                        self.observations[sensor][:-1, ind]
                    )

                recurrent_hidden_states_batch.append(
                    self.recurrent_hidden_states[0, :, ind]
                )

                actions_batch.append(self.actions[:, ind])
                prev_actions_batch.append(self.prev_actions[:-1, ind])
                value_preds_batch.append(self.value_preds[:-1, ind])
                return_batch.append(self.returns[:-1, ind])
                masks_batch.append(self.masks[:-1, ind])
                old_action_log_probs_batch.append(
                    self.action_log_probs[:, ind]
                )

                adv_targ.append(advantages[:, ind])

            T, N = self.num_steps, num_envs_per_batch

            # These are all tensors of size (T, N, -1)
            for sensor in observations_batch:
                observations_batch[sensor] = torch.stack(
                    observations_batch[sensor], 1
                )

            actions_batch = torch.stack(actions_batch, 1)
            prev_actions_batch = torch.stack(prev_actions_batch, 1)
            value_preds_batch = torch.stack(value_preds_batch, 1)
            return_batch = torch.stack(return_batch, 1)
            masks_batch = torch.stack(masks_batch, 1)
            old_action_log_probs_batch = torch.stack(
                old_action_log_probs_batch, 1
            )
            adv_targ = torch.stack(adv_targ, 1)

            # States is just a (num_recurrent_layers, N, -1) tensor
            recurrent_hidden_states_batch = torch.stack(
                recurrent_hidden_states_batch, 1
            )

            # Flatten the (T, N, ...) tensors to (T * N, ...)
            for sensor in observations_batch:
                observations_batch[sensor] = self._flatten_helper(
                    T, N, observations_batch[sensor]
                )

            actions_batch = self._flatten_helper(T, N, actions_batch)
            prev_actions_batch = self._flatten_helper(T, N, prev_actions_batch)
            value_preds_batch = self._flatten_helper(T, N, value_preds_batch)
            return_batch = self._flatten_helper(T, N, return_batch)
            masks_batch = self._flatten_helper(T, N, masks_batch)
            old_action_log_probs_batch = self._flatten_helper(
                T, N, old_action_log_probs_batch
            )
            adv_targ = self._flatten_helper(T, N, adv_targ)

            yield (
                observations_batch,
                recurrent_hidden_states_batch,
                actions_batch,
                prev_actions_batch,
                value_preds_batch,
                return_batch,
                masks_batch,
                old_action_log_probs_batch,
                adv_targ,
            )

    @staticmethod
    def _flatten_helper(t: int, n: int, tensor: torch.Tensor) -> torch.Tensor:
        r"""Given a tensor of size (t, n, ..), flatten it to size (t*n, ...).

        Args:
            t: first dimension of tensor.
            n: second dimension of tensor.
            tensor: target tensor to be flattened.

        Returns:
            flattened tensor of size (t*n, ...)
        """
        return tensor.view(t * n, *tensor.size()[2:])

class RolloutStorageTwoAgentHybrid:
    r"""Class for storing rollout information for Two RL trainers.

    """

    def __init__(
        self,
        attack:RolloutStorageHybrid,
        agent:RolloutStorage,
    ):
        self.attack = attack
        self.agent = agent
 
    def to(self, device):
        self.attack = self.attack.to(device)
        self.agent  = self.agent.to(device)


class RolloutStorageTwoAgent:
    r"""Class for storing rollout information for Two RL trainers.

    """

    def __init__(
        self,
        attack:RolloutStorageHybrid,
        agent:RolloutStorage,
    ):
        self.attack = attack
        self.agent = agent
 
    def to(self, device):
        self.attack = self.attack.to(device)
        self.agent  = self.agent.to(device)
        
class RolloutStorageMA:
    r"""Class for storing rollout information for Two RL trainers.

    """

    def __init__(
        self,
        attack:RolloutStorageHybrid,
        agent:RolloutStorage,
    ):
        self.attack = attack
        self.agent = agent
 
    def to(self, device):
        self.attack = self.attack.to(device)
        self.agent  = self.agent.to(device)

    def recurrent_generator(self, advantages, num_mini_batch):
        data_generator_agent = self.agent.recurrent_generator(
                advantages["agent"], num_mini_batch
            )
        data_generator_attack = self.attack.recurrent_generator(
                advantages["attack"], num_mini_batch
            )
        for sample_agent,sample_attack in zip(data_generator_agent,data_generator_attack):
            (
                obs_batch_agent,
                recurrent_hidden_states_batch_agent,   
                actions_batch_agent,                       
                prev_actions_batch_agent,             
                value_preds_batch_agent,               
                return_batch_agent,                    
                masks_batch_agent,                    
                old_action_log_probs_batch_agent,     
                adv_targ_agent,                       
            ) = sample_agent

            (
                obs_batch_attack,
                recurrent_hidden_states_batch_attack,   
                actions_batch_attack,                   
                prev_actions_batch_attack,              
                value_preds_batch_attack,               
                return_batch_attack,                    
                masks_batch_attack,                     
                old_action_log_probs_batch_attack,      
                adv_targ_attack,                        
            ) = sample_attack
            

            yield (
                obs_batch_agent,                           obs_batch_attack,
                recurrent_hidden_states_batch_agent,       recurrent_hidden_states_batch_attack,
                actions_batch_agent,                       actions_batch_attack,
                prev_actions_batch_agent,                  prev_actions_batch_attack,
                value_preds_batch_agent,                   value_preds_batch_attack,
                return_batch_agent,                        return_batch_attack,
                masks_batch_agent,                         masks_batch_attack,
                old_action_log_probs_batch_agent,          old_action_log_probs_batch_attack,
                adv_targ_agent,                            adv_targ_attack,
            )
        


        
def get(dg1,dg2):
    for  i,j in zip(dg1,dg2):
        a,b,c = i
        _,d,e = j
        yield (
            a,
            b,
            c,
            d,
            e,
        )

if __name__ == "__main__":
    import itertools
    dg1 = [(1,2,3),(2,4,5),(3,6,7)]
    dg2 = [(1,12,13),(2,14,15)]
    i = 0
    for sample in get(dg1,dg2):
        print(sample) 
        print(i)
        i+=1

