import copy
from components.episode_buffer import EpisodeBatch
# from modules.critics.maddpg import MADDPGCritic
from modules.critics.maddpg import DDPGCritic
import torch as th
from torch.optim import RMSprop, Adam


class AdvDDPGLearner:
    def __init__(self, mac, scheme, logger, args):
        self.args = args
        self.n_agents = args.n_agents
        self.n_actions = args.n_actions
        self.logger = logger

        self.mac = mac
        self.target_mac = copy.deepcopy(self.mac)
        self.agent_params = list(mac.parameters())

        # self.critic = MADDPGCritic(scheme, args)
        self.critic = DDPGCritic(scheme, args)
        self.target_critic = copy.deepcopy(self.critic)
        self.critic_params = list(self.critic.parameters())

        if getattr(self.args, "optimizer", "rmsprop") == "rmsprop":
            self.agent_optimiser = RMSprop(params=self.agent_params, lr=args.lr, alpha=args.optim_alpha, eps=args.optim_eps)
        elif getattr(self.args, "optimizer", "rmsprop") == "adam":
            self.agent_optimiser = Adam(params=self.agent_params, lr=args.lr, eps=getattr(args, "optimizer_epsilon", 10E-8))
        else:
            raise Exception("unknown optimizer {}".format(getattr(self.args, "optimizer", "rmsprop")))

        if getattr(self.args, "optimizer", "rmsprop") == "rmsprop":
            self.critic_optimiser = RMSprop(params=self.critic_params, lr=args.critic_lr, alpha=args.optim_alpha, eps=args.optim_eps)
        elif getattr(self.args, "optimizer", "rmsprop") == "adam":
            self.critic_optimiser = Adam(params=self.critic_params, lr=args.critic_lr, eps=getattr(args, "optimizer_epsilon", 10E-8))
        else:
            raise Exception("unknown optimizer {}".format(getattr(self.args, "optimizer", "rmsprop")))

        self.log_stats_t = -self.args.learner_log_interval - 1

        if hasattr(args,'attack_agent'):
            if args.attack_agent is not None:
                self.attack_agent = args.attack_agent
            else:
                raise ValueError('Need to specify attack_agent')
        else:
            raise ValueError('Need to specify attack_agent')

    def train(self, batch: EpisodeBatch, t_env: int, episode_num: int):
        # Get the relevant quantities
        # rewards = batch["reward"][:, :-1]
        # actions = batch["actions"][:, :-1]
        # terminated = batch["terminated"][:, :-1].float()
        # mask = 1 - terminated

        rewards = batch["reward"][:, :-1]
        actions = batch["actions"][:, :-1, self.attack_agent:self.attack_agent+1]
        terminated = batch["terminated"][:, :-1].float()
        mask = 1 - terminated


        # Train the critic
        inputs = self._build_inputs(batch, t=0)

        # print('aaa',inputs.shape, actions.shape, batch["actions"].shape)
        q_taken, _ = self.critic(inputs, actions.detach())
        q_taken = q_taken.view(batch.batch_size, -1, 1)

        # Use the target actor and target critic network to compute the target q
        target_actions = []
        for t in range(1, batch.max_seq_length):
            agent_target_outs = self.target_mac.select_actions(batch, t_ep=t, t_env=None, test_mode=True)
            target_actions.append(agent_target_outs[:,self.attack_agent:self.attack_agent+1])
        target_actions = th.stack(target_actions, dim=1)  # Concat over time

        target_inputs = self._build_inputs(batch, t=1)

        # print('111',target_inputs.shape, target_actions.shape)
        target_vals, _ = self.target_critic(target_inputs, target_actions.detach())
        target_vals = target_vals.view(batch.batch_size, -1, 1)
        targets = rewards.view(-1, 1) + self.args.gamma * (1 - terminated.view(-1, 1)) * target_vals.view(-1, 1)

        td_error = (q_taken.view(-1, 1) - targets.detach())
        masked_td_error = td_error
        loss = (masked_td_error ** 2).mean()

        self.critic_optimiser.zero_grad()
        loss.backward()
        critic_grad_norm = th.nn.utils.clip_grad_norm_(self.critic_params, self.args.grad_norm_clip)
        self.critic_optimiser.step()

        # Train the actor
        pi = self.mac.forward(batch, t=0, select_actions=True)["actions"]
        # extract actions and take only attacked agent's
        pi = pi.view(batch.batch_size,self.n_agents, -1)
        pi = pi[:,self.attack_agent:self.attack_agent+1,:].view(batch.batch_size,-1)
        q, _ = self.critic(self._build_inputs(batch, t=0), pi)
        q = q.view(batch.batch_size, -1, 1)

        # Compute the actor loss
        pg_loss = -(-q.mean() + (pi**2).mean() * 1e-3)

        # Optimise agents
        self.agent_optimiser.zero_grad()
        pg_loss.backward()
        agent_grad_norm = th.nn.utils.clip_grad_norm_(self.agent_params, self.args.grad_norm_clip)
        self.agent_optimiser.step()

        if getattr(self.args, "target_update_mode", "hard") == "hard":
            self._update_targets()
        elif getattr(self.args, "target_update_mode", "hard") in ["soft", "exponential_moving_average"]:
            self._update_targets_soft(tau=getattr(self.args, "target_update_tau", 0.001))
        else:
            raise Exception(
                "unknown target update mode: {}!".format(getattr(self.args, "target_update_mode", "hard")))

        if t_env - self.log_stats_t >= self.args.learner_log_interval:
            self.logger.log_stat("critic_loss", loss.item(), t_env)
            self.logger.log_stat("critic_grad_norm", critic_grad_norm, t_env)
            mask_elems = mask.sum().item()
            self.logger.log_stat("td_error_abs", masked_td_error.abs().sum().item() / mask_elems, t_env)
            self.logger.log_stat("q_taken_mean", (q_taken * mask).sum().item() / mask_elems, t_env)
            self.logger.log_stat("target_mean", targets.sum().item() / mask_elems, t_env)
            self.logger.log_stat("pg_loss", pg_loss.item(), t_env)
            self.logger.log_stat("agent_grad_norm", agent_grad_norm, t_env)
            self.log_stats_t = t_env

    def _update_targets_soft(self, tau):
        for target_param, param in zip(self.target_mac.parameters(), self.mac.parameters()):
            target_param.data.copy_(target_param.data * (1.0 - tau) + param.data * tau)

        for target_param, param in zip(self.target_critic.parameters(), self.critic.parameters()):
            target_param.data.copy_(target_param.data * (1.0 - tau) + param.data * tau)

        if self.args.verbose:
            self.logger.console_logger.info("Updated all target networks (soft update tau={})".format(tau))

    def _build_inputs(self, batch, t):
        bs = batch.batch_size
        inputs = []
        inputs.append(batch["state"][:, t])

        if self.args.obs_last_action:
            if t == 0:
                inputs.append(th.zeros_like(batch["actions"][:, t]))
            else:
                inputs.append(batch["actions"][:, t - 1])
        if self.args.obs_agent_id:
            inputs.append(th.eye(self.n_agents, device=batch.device).unsqueeze(0).expand(bs, -1, -1))

        inputs = th.cat([x.reshape(bs, -1) for x in inputs], dim=1)
        return inputs

    def _update_targets(self):
        self.target_mac.load_state(self.mac)
        self.target_critic.load_state_dict(self.critic.state_dict())
        self.logger.console_logger.info("Updated all target networks")

    def cuda(self, device="cuda:0"):
        self.mac.cuda(device=device)
        self.target_mac.cuda(device=device)
        self.critic.cuda(device=device)
        self.target_critic.cuda(device=device)

    def save_models(self, path):
        self.mac.save_models(path)
        th.save(self.agent_optimiser.state_dict(), "{}/opt.th".format(path))

    def load_models(self, path):
        self.mac.load_models(path)
        # Not quite right but I don't want to save target networks
        self.target_mac.load_models(path)
        self.agent_optimiser.load_state_dict(
            th.load("{}/opt.th".format(path), map_location=lambda storage, loc: storage))

    def load_trained_models(self, path):
        self.mac.load_trained_models(path)
        # Not quite right but I don't want to save target networks
        self.target_mac.load_trained_models(path)