import copy
from components.episode_buffer import EpisodeBatch
from modules.mixers.vdn import VDNMixer
from modules.mixers.qmix import QMixer
from modules.mixers.qmix_central_no_hyper import QMixerCentralFF
from modules.mixers.qmix_central_attention import QMixerCentralAtten
import torch as th
import torch.nn.functional as F
from torch.optim import RMSprop, Adam
from collections import deque
from controllers import REGISTRY as mac_REGISTRY
from utils.rl_utils import build_td_lambda_targets, build_td_lambda_targets_with_weights
import pdb

class MAXQLearner:
    def __init__(self, mac, scheme, logger, args, buffer):
        self.args = args
        self.mac = mac
        self.logger = logger

        self.mac_params = list(mac.parameters())
        self.params = list(self.mac.parameters())

        self.last_target_update_episode = 0

        self.mixer = None
        assert args.mixer is not None
        if args.mixer is not None:
            if args.mixer == "vdn":
                self.mixer = VDNMixer()
            elif args.mixer == "qmix":
                self.mixer = QMixer(args)
            else:
                raise ValueError("Mixer {} not recognised.".format(args.mixer))
            self.mixer_params = list(self.mixer.parameters())
            self.params += list(self.mixer.parameters())
            self.target_mixer = copy.deepcopy(self.mixer)

        # a little wasteful to deepcopy (e.g. duplicates action selector), but should work for any MAC
        self.target_mac = copy.deepcopy(mac)

        # Central Q
        # TODO: Clean this mess up!
        self.central_mac = None
        if self.args.central_mixer in ["ff", "atten"]:
            if self.args.central_loss == 0:
                self.central_mixer = self.mixer
                self.central_mac = self.mac
                self.target_central_mac = self.target_mac
            else:
                if self.args.central_mixer == "ff":
                    self.central_mixer = QMixerCentralFF(args) # Feedforward network that takes state and agent utils as input
                elif self.args.central_mixer == "atten":
                    self.central_mixer = QMixerCentralAtten(args)
                else:
                    raise Exception("Error with central_mixer")

                assert args.central_mac == "basic_central_mac"
                self.central_mac = mac_REGISTRY[args.central_mac](scheme, args) # Groups aren't used in the CentralBasicController. Little hacky
                self.target_central_mac = copy.deepcopy(self.central_mac)
                self.params += list(self.central_mac.parameters())
        else:
            raise Exception("Error with qCentral")
        self.params += list(self.central_mixer.parameters())
        self.target_central_mixer = copy.deepcopy(self.central_mixer)

        if self.args.optim == 'RMSProp':
            self.optimiser = RMSprop(params=self.params, lr=args.lr, alpha=args.optim_alpha, eps=args.optim_eps)
        elif self.args.optim == 'Adam':
            self.optimiser = Adam(params=self.params, lr=args.lr)


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

        self.grad_norm = 1
        self.mixer_norm = 1
        self.mixer_norms = deque([1], maxlen=100)

        # LFIW parameters
        self.lfiw_optimiser = Adam(params=mac.lfiw_parameters(), lr=args.lfiw_lr)
        self.prob_temperature = self.args.prob_temperature
        # lambda parameters
        self.buffer = buffer
        self.max_discounted_return = 0
        self.max_weight = 0
        self.min_weight = 0

    def train(self, batch: EpisodeBatch, fast_batch: EpisodeBatch, t_env: int, episode_num: int, ids: list):
        # Get the relevant quantities
        rewards = batch["reward"][:, :-1]
        actions = batch["actions"][:, :-1]
        terminated = batch["terminated"][:, :-1].float()
        mask = batch["filled"][:, :-1].float()
        mask[:, 1:] = mask[:, 1:] * (1 - terminated[:, :-1])
        avail_actions = batch["avail_actions"]
        weights = batch["td_lambda"]

        # Calculate estimated Q-Values
        mac_out = []
        self.mac.init_hidden(batch.batch_size)
        for t in range(batch.max_seq_length):
            agent_outs, _ = self.mac.forward(batch, t=t)
            mac_out.append(agent_outs)
        mac_out = th.stack(mac_out, dim=1)  # Concat over time

        # Pick the Q-Values for the actions taken by each agent
        chosen_action_qvals_agents = th.gather(mac_out[:, :-1], dim=3, index=actions).squeeze(3)  # Remove the last dim
        chosen_action_qvals = chosen_action_qvals_agents

        # Calculate the Q-Values necessary for the target
        target_mac_out = []
        target_policies = []
        self.target_mac.init_hidden(batch.batch_size)
        for t in range(batch.max_seq_length):
            target_agent_outs, target_policy = self.target_mac.forward(batch, t=t)
            target_mac_out.append(target_agent_outs)
            target_policies.append(target_policy)

        # We don't need the first timesteps Q-Value estimate for calculating targets
        target_mac_out = th.stack(target_mac_out[:], dim=1)  # Concat across time
        target_policies = th.stack(target_policies, dim=1)
        # Mask out unavailable actions
        target_mac_out[avail_actions[:, :] == 0] = -9999999  # From OG deepmarl

        # Max over target Q-Values
        if self.args.double_q:
            # Get actions that maximise live Q (for double q-learning)
            mac_out_detach = mac_out.clone().detach()
            mac_out_detach[avail_actions == 0] = -9999999
            cur_max_action_targets, cur_max_actions = mac_out_detach[:, :].max(dim=3, keepdim=True)
            target_max_agent_qvals = th.gather(target_mac_out[:,:], 3, cur_max_actions[:,:]).squeeze(3)
            target_policies = th.gather(target_policies, 3, cur_max_actions).squeeze(3)
        else:
            raise Exception("Use double q")

        # Central MAC stuff
        central_mac_out = []
        self.central_mac.init_hidden(batch.batch_size)
        for t in range(batch.max_seq_length):
            agent_outs = self.central_mac.forward(batch, t=t)
            central_mac_out.append(agent_outs)
        central_mac_out = th.stack(central_mac_out, dim=1)  # Concat over time
        central_chosen_action_qvals_agents = th.gather(central_mac_out[:, :-1], dim=3, index=actions.unsqueeze(4).repeat(1,1,1,1,self.args.central_action_embed)).squeeze(3)  # Remove the last dim

        central_target_mac_out = []
        self.target_central_mac.init_hidden(batch.batch_size)
        for t in range(batch.max_seq_length):
            target_agent_outs = self.target_central_mac.forward(batch, t=t)
            central_target_mac_out.append(target_agent_outs)
        central_target_mac_out = th.stack(central_target_mac_out[:], dim=1)  # Concat across time
        # Mask out unavailable actions
        central_target_mac_out[avail_actions[:, :] == 0] = -9999999  # From OG deepmarl
        # Use the Qmix max actions
        central_target_max_agent_qvals = th.gather(central_target_mac_out[:,:], 3, cur_max_actions[:,:].unsqueeze(4).repeat(1,1,1,1,self.args.central_action_embed)).squeeze(3)
        # ---

        # Mix
        chosen_action_qvals = self.mixer(chosen_action_qvals, batch["state"][:, :-1])
        target_max_qvals = self.target_central_mixer(central_target_max_agent_qvals[:,:], batch["state"][:,:])

        
        # From here!
        if self.args.dyn_td_lambda:
            targets = rewards + self.args.gamma * (1 - terminated) * target_max_qvals[:, 1:].detach()
            delta_td = (chosen_action_qvals - targets).detach()
            
            delta = 1 - th.exp(-th.abs(delta_td))

            delta_expand = th.zeros_like(target_max_qvals)
            delta_expand[:, :delta.size(1),] = delta

            delta_expand = delta_expand / th.mean(delta_expand)
            '''
            delta = self.cal_delta(batch, target_max_qvals)
            delta_expand = delta.T.expand(target_max_qvals.size(1), target_max_qvals.size(0)).T
            '''


            #new_weights = self.update_lfiw(fast_batch, batch) * delta.unsqueeze(dim=-1).detach() * policy_weights.unsqueeze(dim=-1).detach()
            lambda_weights, MSE_weights = self.update_lfiw(fast_batch, batch)

            new_weights = th.clamp(lambda_weights * delta_expand.detach(), min=0.0, max=1.0)
            #new_weights = lambda_weights * delta.unsqueeze(dim=-1).detach()
            


            td_lambda = th.where(weights==0, new_weights, weights)
            

            for t in range(weights.size(1)):
                self.buffer.update({"td_lambda": td_lambda[:, t]}, bs=ids, ts=t, mark_filled=False)

        else:
            td_lambda = weights.clone()

        # Calculate 1-step Q-Learning targets
        #targets = rewards + self.args.gamma * (1 - terminated) * target_max_qvals
        targets = build_td_lambda_targets_with_weights(rewards, terminated, mask, target_max_qvals, self.args.n_agents, self.args.gamma, td_lambda)
        

        # Td-error
        td_error = (chosen_action_qvals - (targets[:, :-1].detach()))

        mask = mask.expand_as(td_error)

        # 0-out the targets that came from padded data
        masked_td_error = td_error * mask

        # Training central Q
        central_chosen_action_qvals = self.central_mixer(central_chosen_action_qvals_agents, batch["state"][:, :-1])
        central_td_error = (central_chosen_action_qvals - targets[:, :-1].detach())
        central_mask = mask.expand_as(central_td_error)
        central_masked_td_error = central_td_error * central_mask
        central_loss = (central_masked_td_error ** 2).sum() / mask.sum()


        # QMIX loss with weighting
        ws = th.ones_like(td_error) * self.args.w
        if self.args.hysteretic_qmix: # OW-QMIX
            ws = th.where(td_error < 0, th.ones_like(td_error)*1, ws) # Target is greater than current max
            w_to_use = ws.mean().item() # For logging
        else: # CW-QMIX
            is_max_action = (actions == cur_max_actions[:, :-1]).min(dim=2)[0]
            max_action_qtot = self.target_central_mixer(central_target_max_agent_qvals[:, :-1], batch["state"][:, :-1])
            qtot_larger = targets > max_action_qtot
            ws = th.where(is_max_action | qtot_larger, th.ones_like(td_error)*1, ws) # Target is greater than current max
            w_to_use = ws.mean().item() # Average of ws for logging
        '''
        if self.args.use_omega:
        # QMIX loss with weighting

            ws = th.ones_like(td_error) * MSE_weights[:,:-1]
            if self.args.hysteretic_qmix: # OW-QMIX
                ws = th.where(td_error < 0, th.ones_like(td_error)*1, ws) # Target is greater than current max
                w_to_use = ws.mean().item() # For logging
            else: # CW-QMIX
                is_max_action = (actions == cur_max_actions[:, :-1]).min(dim=2)[0]
                max_action_qtot = self.target_central_mixer(central_target_max_agent_qvals[:, :-1], batch["state"][:, :-1])
                qtot_larger = targets > max_action_qtot
                ws = th.where(is_max_action | qtot_larger, th.ones_like(td_error)*1, ws) # Target is greater than current max
                w_to_use = ws.mean().item() # Average of ws for logging
        '''


        if self.args.dyn_td_lambda:
            if self.args.use_weight:
                qmix_loss = (MSE_weights[:, :-1] * (masked_td_error ** 2)).sum() / mask.sum()
            else:
                qmix_loss = (ws.detach()*(masked_td_error ** 2)).sum() / mask.sum()
        else:
            qmix_loss = (ws.detach()*(masked_td_error ** 2)).sum() / mask.sum()

        # The weightings for the different losses aren't used (they are always set to 1)
        loss = self.args.qmix_loss * qmix_loss + self.args.central_loss * central_loss

        # Optimise
        self.optimiser.zero_grad()
        loss.backward()

        # Logging
        agent_norm = 0
        for p in self.mac_params:
            param_norm = p.grad.data.norm(2)
            agent_norm += param_norm.item() ** 2
        agent_norm = agent_norm ** (1. / 2)

        mixer_norm = 0
        for p in self.mixer_params:
            param_norm = p.grad.data.norm(2)
            mixer_norm += param_norm.item() ** 2
        mixer_norm = mixer_norm ** (1. / 2)
        self.mixer_norm = mixer_norm
        self.mixer_norms.append(mixer_norm)

        grad_norm = th.nn.utils.clip_grad_norm_(self.params, self.args.grad_norm_clip)
        self.grad_norm = grad_norm

        self.optimiser.step()

        if (episode_num - self.last_target_update_episode) / self.args.target_update_interval >= 1.0:
            self.buffer.clear_lambda()
            self._update_targets()
            self.last_target_update_episode = episode_num

        if t_env - self.log_stats_t >= self.args.learner_log_interval:
            self.logger.log_stat("loss", loss.item(), t_env)
            self.logger.log_stat("qmix_loss", qmix_loss.item(), t_env)
            #self.logger.log_stat("grad_norm", grad_norm, t_env)
            #self.logger.log_stat("mixer_norm", mixer_norm, t_env)
            #self.logger.log_stat("agent_norm", agent_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", (chosen_action_qvals * mask).sum().item()/(mask_elems * self.args.n_agents), t_env)
            self.logger.log_stat("target_mean", (targets[:, :-1] * mask).sum().item()/(mask_elems * self.args.n_agents), t_env)
            self.logger.log_stat("central_loss", central_loss.item(), t_env)
            if not self.args.dyn_td_lambda:
                self.logger.log_stat("w_to_use", w_to_use, t_env)
            self.log_stats_t = t_env

    def update_lfiw(self, fast_batch, slow_batch):
        slow_outs = []
        fast_outs = []
        self.mac.LFIW_init_hidden(slow_batch.batch_size)
        for t in range(fast_batch.max_seq_length):
            slow_out = self.mac.LFIW_forward(slow_batch, t).reshape(-1, 1)       # [bs * T, 1]

            slow_outs.append(slow_out)
        
        slow_outs = th.stack(slow_outs, dim=1)


        self.mac.LFIW_init_hidden(fast_batch.batch_size)
        for t in range(fast_batch.max_seq_length):
            fast_out = self.mac.LFIW_forward(fast_batch, t).reshape(-1, 1)

            fast_outs.append(fast_out)

        fast_outs = th.stack(fast_outs, dim=1)

        zeros = th.zeros_like(slow_outs).to(self.args.device)
        ones = th.ones_like(fast_outs).to(self.args.device)

        loss = F.binary_cross_entropy(F.sigmoid(slow_outs), zeros) + F.binary_cross_entropy(F.sigmoid(fast_outs), ones)
        

        self.lfiw_optimiser.zero_grad()
        loss.backward(retain_graph=False)
        self.lfiw_optimiser.step()

        self.mac.LFIW_init_hidden(slow_batch.batch_size)
        slow_preds = []
        for t in range(slow_batch.max_seq_length):
            slow_pred = self.mac.LFIW_forward(slow_batch, t).reshape(-1, 1)
            slow_preds.append(slow_pred)
        slow_preds = th.stack(slow_preds, dim=1)

        importance_weights = F.sigmoid(slow_preds/self.prob_temperature).detach()
        lambda_weights = importance_weights.clone()

        #lambda_weights = (lambda_weights - self.min_weight) / (self.max_weight - self.min_weight)
        #lambda_weights = th.minimum(th.maximum((lambda_weights / th.mean(lambda_weights) - 1), th.zeros_like(lambda_weights)), th.ones_like(lambda_weights))
        #lambda_weights = th.nn.functional.softmax(lambda_weights / th.mean(lambda_weights) - 1, dim=1)

        #print(th.max(lambda_weights), th.min(lambda_weights), th.median(lambda_weights))
         
        MSE_weights = F.sigmoid(slow_preds/5.0).detach()
        MSE_weights = MSE_weights / th.mean(MSE_weights)
        #print(lambda_weights)
        return lambda_weights, MSE_weights
    

    def cal_delta(self, batch, target_agent_qval):
        
        rewards = batch['reward']
        terminated = batch['terminated']
        discounted_returns = target_agent_qval.new_zeros(*target_agent_qval.shape)
        discounted_returns[:, -1] = target_agent_qval[:, -1] * (1 - th.sum(terminated, dim=1))
        
        for i in range(rewards.size(1) - 2, -1, -1):
            discounted_returns[:, i] = rewards[:, i] + self.args.gamma * discounted_returns[:, i+1]

        self.max_discounted_return = max(self.max_discounted_return, th.max(discounted_returns[:, 0]).item())
        
        delta = th.exp(-th.abs(discounted_returns[:, 0] - self.max_discounted_return))

        delta = delta / th.mean(delta)

        return delta
    

    def _update_targets(self):
        self.target_mac.load_state(self.mac)
        if self.mixer is not None:
            self.target_mixer.load_state_dict(self.mixer.state_dict())
        if self.central_mac is not None:
            self.target_central_mac.load_state(self.central_mac)
        self.target_central_mixer.load_state_dict(self.central_mixer.state_dict())
        self.logger.console_logger.info("Updated target network")

    def cuda(self):
        self.mac.cuda()
        self.target_mac.cuda()
        if self.mixer is not None:
            self.mixer.cuda()
            self.target_mixer.cuda()
        if self.central_mac is not None:
            self.central_mac.cuda()
            self.target_central_mac.cuda()
        self.central_mixer.cuda()
        self.target_central_mixer.cuda()

    # TODO: Model saving/loading is out of date!
    def save_models(self, path):
        self.mac.save_models(path)
        if self.mixer is not None:
            th.save(self.mixer.state_dict(), "{}/mixer.th".format(path))
        th.save(self.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)
        if self.mixer is not None:
            self.mixer.load_state_dict(th.load("{}/mixer.th".format(path), map_location=lambda storage, loc: storage))
        self.optimiser.load_state_dict(th.load("{}/opt.th".format(path), map_location=lambda storage, loc: storage))
