import diffuser.utils as utils
from ml_logger import logger, RUN
import torch
from torch.distributions.multivariate_normal import MultivariateNormal
from copy import deepcopy
import numpy as np
import queue
import os
import gym
from diffuser.utils.timer import Timer
from config.locomotion_config import Config
from diffuser.utils.arrays import to_torch, to_np, to_device
from diffuser.datasets.d4rl import suppress_output
from diffuser.models.value_func_model import ValueMLP
from diffuser.models.forward_dynamics import ForwardDynamics
from diffuser.models.bisimulation_metric_model import BisimNet
from diffuser.datasets.sequence import CustomSequenceDataset
from collections import namedtuple
from diffuser.utils.trajectory import Trajectory
from scripts.create_trajectory import save_traj
from scripts.create_trajectory import train_val
import pickle


RewardBatch = namedtuple('Batch', 'trajectories conditions returns rewards')


def save_data(dataset, bucket, part_num):
    savepath = os.path.join(bucket, logger.prefix, 'checkpoint')
    os.makedirs(savepath, exist_ok=True)
    filename = "dataset" + str(part_num) + ".dat"
    savepath = os.path.join(savepath, filename)
    with open(savepath, "wb") as f:
        pickle.dump(dataset, f)


def cycle(dl):
    while True:
        for data in dl:
            yield data


def concat_state(states, batch_size, transpose=False):
    state_expand = torch.unsqueeze(states, dim=0)
    state_tile = state_expand.tile((batch_size, 1, 1))
    state_tile_t = torch.transpose(state_tile, dim0=0, dim1=1)
    if transpose:
        concat_states = torch.cat((state_tile_t, state_tile), dim=2)
    else:
        concat_states = torch.cat((state_tile, state_tile_t), dim=2)
    representation_dim = states.shape[1]
    return torch.reshape(concat_states, (batch_size ** 2, representation_dim * 2))


def retrain_val(**deps):
    RUN._update(deps)
    Config._update(deps)

    logger.remove('*.pkl')
    logger.remove("traceback.err")
    logger.log_params(Config=vars(Config), RUN=vars(RUN))

    Config.device = 'cuda'

    if Config.predict_epsilon:
        prefix = f'predict_epsilon_{Config.n_diffusion_steps}_1000000.0'
    else:
        prefix = f'predict_x0_{Config.n_diffusion_steps}_1000000.0'

    loadpath = os.path.join(Config.bucket, logger.prefix, 'checkpoint')
    next_loadpath = os.path.join(Config.bucket, logger.prefix, 'checkpoint')

    if Config.save_checkpoints:
        loadpath = os.path.join(loadpath, f'state_{self.step}.pt')
    else:
        loadpath = os.path.join(loadpath, 'state.pt')

    if Config.save_checkpoints:
        next_loadpath = os.path.join(next_loadpath, f'state_{self.step}.pt')
    else:
        next_loadpath = os.path.join(next_loadpath, 'state_include_next.pt')

    state_dict = torch.load(loadpath, map_location=Config.device)
    # next_state_dict = torch.load(next_loadpath, map_location=Config.device)

    # Load configs
    torch.backends.cudnn.benchmark = True
    utils.set_seed(Config.seed)

    dataset_config = utils.Config(
        Config.loader,
        savepath='dataset_config.pkl',
        env=Config.dataset,
        horizon=Config.horizon,
        normalizer=Config.normalizer,
        preprocess_fns=Config.preprocess_fns,
        use_padding=Config.use_padding,
        max_path_length=Config.max_path_length,
        include_returns=Config.include_returns,
        returns_scale=Config.returns_scale,
    )

    render_config = utils.Config(
        Config.renderer,
        savepath='render_config.pkl',
        env=Config.dataset,
    )

    dataset = dataset_config()
    # renderer = render_config()

    observation_dim = dataset.observation_dim
    action_dim = dataset.action_dim

    if Config.diffusion == 'models.GaussianInvDynDiffusion' and Config.next_diffusion == 'models.NextGaussianInvDynDiffusion':
        transition_dim = observation_dim
    else:
        transition_dim = observation_dim + action_dim

    model_config = utils.Config(
        Config.model,
        savepath='model_config.pkl',
        horizon=Config.horizon,
        transition_dim=transition_dim,
        cond_dim=observation_dim,
        dim_mults=Config.dim_mults,
        dim=Config.dim,
        returns_condition=Config.returns_condition,
        device=Config.device,
    )

    next_model_config = utils.Config(
        Config.next_model,
        savepath='model_config.pkl',
        horizon=Config.horizon,
        transition_dim=transition_dim,
        cond_dim=observation_dim,
        dim_mults=Config.dim_mults,
        dim=Config.dim,
        returns_condition=Config.returns_condition,
        device=Config.device,
    )

    diffusion_config = utils.Config(
        Config.diffusion,
        savepath='diffusion_config.pkl',
        horizon=Config.horizon,
        observation_dim=observation_dim,
        action_dim=action_dim,
        n_timesteps=Config.n_diffusion_steps,
        loss_type=Config.loss_type,
        clip_denoised=Config.clip_denoised,
        predict_epsilon=Config.predict_epsilon,
        hidden_dim=Config.hidden_dim,
        ## loss weighting
        action_weight=Config.action_weight,
        loss_weights=Config.loss_weights,
        loss_discount=Config.loss_discount,
        returns_condition=Config.returns_condition,
        device=Config.device,
        condition_guidance_w=Config.condition_guidance_w,
    )

    next_diffusion_config = utils.Config(
        Config.next_diffusion,
        savepath='diffusion_config.pkl',
        horizon=Config.horizon,
        observation_dim=observation_dim,
        action_dim=action_dim,
        n_timesteps=Config.n_diffusion_steps,
        loss_type=Config.loss_type,
        clip_denoised=Config.clip_denoised,
        predict_epsilon=Config.predict_epsilon,
        hidden_dim=Config.hidden_dim,
        ## loss weighting
        action_weight=Config.action_weight,
        loss_weights=Config.loss_weights,
        loss_discount=Config.loss_discount,
        returns_condition=Config.returns_condition,
        device=Config.device,
        condition_guidance_w=Config.condition_guidance_w,
    )

    trainer_config = utils.Config(
        utils.Trainer,
        savepath='trainer_config.pkl',
        train_batch_size=Config.batch_size,
        train_lr=Config.learning_rate,
        gradient_accumulate_every=Config.gradient_accumulate_every,
        ema_decay=Config.ema_decay,
        sample_freq=Config.sample_freq,
        save_freq=Config.save_freq,
        log_freq=Config.log_freq,
        label_freq=int(Config.n_train_steps // Config.n_saves),
        save_parallel=Config.save_parallel,
        bucket=Config.bucket,
        n_reference=Config.n_reference,
        train_device=Config.device,
    )

    model = model_config()
    next_model = next_model_config()
    diffusion = diffusion_config(model)
    next_diffusion = next_diffusion_config(next_model)
    # trainer = trainer_config(diffusion, dataset, renderer)
    trainer = trainer_config(diffusion, dataset)
    # next_trainer = trainer_config(next_diffusion, dataset, renderer)
    logger.print(utils.report_parameters(model), color='green')

    trainer.step = state_dict['step']
    trainer.model.load_state_dict(state_dict['model'])
    trainer.ema_model.load_state_dict(state_dict['ema'])

    # next_trainer.step = next_state_dict['step']
    # next_trainer.model.load_state_dict(next_state_dict['model'])
    # next_trainer.ema_model.load_state_dict(next_state_dict['ema'])

    state_dim = observation_dim
    # V_model_1 = ValueMLP(hidden_dim=256, input_dim=state_dim, output_dim=1).to(Config.device)
    # V_model_2 = ValueMLP(hidden_dim=256, input_dim=state_dim, output_dim=1).to(Config.device)
    # value_loadpath = os.path.join(Config.bucket, logger.prefix, 'checkpoint')
    # value_loadpath = os.path.join(value_loadpath, 'V_model.pt')
    # value_state_dict = torch.load(value_loadpath, map_location=Config.device)
    # V_model_1.load_state_dict(value_state_dict['model1'])
    # V_model_2.load_state_dict(value_state_dict['model2'])
    #
    # forward_models = []
    # forward_loadpath = os.path.join(Config.bucket, logger.prefix, 'checkpoint')
    # forward_loadpath = os.path.join(forward_loadpath, 'forward.pt')
    # forward_state_dict = torch.load(forward_loadpath, map_location=Config.device)
    # for i in range(7):
    #     hidden_dim = 200
    #     model = ForwardDynamics(state_dim=state_dim, hidden_dim=hidden_dim).to(Config.device)
    #     model_idx = "model" + str(i)
    #     model.load_state_dict(forward_state_dict[model_idx])
    #     forward_models.append(model)

    # path_num = len(dataset.indices)
    path_num = dataset.fields.normed_observations.shape[0]
    paths = []
    first_epoch = True # After the first epoch: also false
    obs_numpy = np.array([])
    loadpath = os.path.join(Config.bucket, logger.prefix, 'checkpoint')
    loadpath = os.path.join(loadpath, "new_dataset.dat")
    if first_epoch:
        for path_ind in range(path_num):
            # path_ind, start, end = dataset.indices[i]
            observations = dataset.fields.normed_observations[path_ind, :dataset.fields.path_lengths[path_ind]]
            if path_ind == 0:
                obs_numpy = observations
            else:
                obs_numpy = np.concatenate((obs_numpy, observations), axis=0)
            actions = dataset.fields.normed_actions[path_ind, :dataset.fields.path_lengths[path_ind]]
            if "hopper" in Config.dataset:
                rewards = dataset.fields.rewards[path_ind, :dataset.fields.path_lengths[path_ind]] / 4
            elif "halfcheetah" in Config.dataset:
                rewards = dataset.fields.rewards[path_ind, :dataset.fields.path_lengths[path_ind]] / 15
            elif "walker2d" in Config.dataset:
                rewards = dataset.fields.rewards[path_ind, :dataset.fields.path_lengths[path_ind]] / 9
            elif "kitchen" in Config.dataset:
                rewards = dataset.fields.rewards[path_ind, :dataset.fields.path_lengths[path_ind]] / 3
            elif "maze" in Config.dataset:
                rewards = dataset.fields.rewards[path_ind, :dataset.fields.path_lengths[path_ind]]
            else:
                return "ERROR"
            next_item = np.concatenate(
                (dataset.fields.normed_observations[path_ind, 1:dataset.fields.path_lengths[path_ind]],
                 np.array([dataset.fields.normed_observations[path_ind, dataset.fields.path_lengths[path_ind] - 1]])),
                axis=0)
            paths.append([observations, actions, rewards, next_item])
    else:
        with open(loadpath, "rb") as f:
            paths = pickle.load(f)
        path_num = len(paths)
        for path_ind in range(path_num):
            if path_ind == 0:
                obs_numpy = paths[path_ind][0]
            else:
                obs_numpy = np.concatenate((obs_numpy, paths[path_ind][0]), axis=0)

    loadpath = os.path.join(Config.bucket, logger.prefix, 'checkpoint')
    os.makedirs(loadpath, exist_ok=True)
    part_num = 1
    part_length = path_num // 6
    # loadpath = os.path.join(loadpath, "part1.dat")
    # with open(loadpath, "rb") as f:
    #     paths_part = pickle.load(f)

    # logger.print(paths[-1][0].shape, paths[-1][1].shape, paths[-1][2].shape, paths[-1][3].shape)

    num_layer = 1
    # bisim_model = BisimNet(state_dim=state_dim, num_layers=num_layer).to(Config.device)
    # bisim_loadpath = os.path.join(Config.bucket, logger.prefix, 'checkpoint')
    # bisim_loadpath = os.path.join(bisim_loadpath, 'bisim.pt')
    # bisim_state_dict = torch.load(bisim_loadpath, map_location=Config.device)
    # bisim_model.load_state_dict(bisim_state_dict['online'])

    discount = 0.99
    returns_scale = 400
    max_path_length = 1000
    discounts = discount ** np.arange(max_path_length)[:, None]
    epoch_num = 1
    train_diffuser_epoch = 10
    horizon = 10
    threshold = 0.1
    returns = to_device(Config.test_ret * torch.ones(1, 1), Config.device)
    # dataloader = cycle(torch.utils.data.DataLoader(
    #     dataset, batch_size=batch_size, num_workers=0, shuffle=True, pin_memory=True
    # ))
    timer = Timer()

    new_paths = []
    stitching_num = 0
    count = 0
    obs_numpy_new = np.array([])
    # loadpath = os.path.join(Config.bucket, logger.prefix, 'checkpoint')
    # loadpath = os.path.join(loadpath, "new_dataset.dat")
    # with open(loadpath, "rb") as f:
    #     paths = pickle.load(f)
    new_dataset = CustomSequenceDataset(paths, dataset.indices)
    new_dataloader = cycle(torch.utils.data.DataLoader(
        new_dataset, batch_size=Config.batch_size, num_workers=0, shuffle=True, pin_memory=True
    ))

    # for step in range(int(Config.n_train_steps // 10)):
    #     for k in range(int(Config.gradient_accumulate_every)):
    #         batch = next(new_dataloader)
    #         batch = [to_torch(item, device=Config.device) for item in batch]
    #         loss, infos = trainer.model.loss(*batch)
    #         loss = loss / Config.gradient_accumulate_every
    #         loss.backward()
    #
    #     trainer.optimizer.step()
    #     trainer.optimizer.zero_grad()
    #
    #     if trainer.step % trainer.update_ema_every == 0:
    #         trainer.step_ema()
    #
    #     if trainer.step % trainer.save_freq == 0:
    #         trainer.save()
    #
    #     if trainer.step % trainer.log_freq == 0:
    #         infos_str = ' | '.join([f'{key}: {val:8.4f}' for key, val in infos.items()])
    #         logger.print(f'{trainer.step}: {loss:8.4f} | {infos_str} | t: {timer():8.4f}')
    #         metrics = {k:v.detach().item() for k, v in infos.items()}
    #         metrics['steps'] = trainer.step
    #         metrics['loss'] = loss.detach().item()
    #         logger.log_metrics_summary(metrics, default_stats='mean')
    #
    #     if trainer.step == 0 and trainer.sample_freq:
    #         trainer.render_reference(trainer.n_reference)
    #
    #     trainer.step += 1
    #
    # trainer.step = 0

    # TODO: create trajectories and retrain value function

    num_eval = 300 # original 300
    device = Config.device

    env_list = [gym.make(Config.dataset) for _ in range(num_eval)]
    dones = [0 for _ in range(num_eval)]
    episode_rewards = [0 for _ in range(num_eval)]

    assert trainer.ema_model.condition_guidance_w == Config.condition_guidance_w
    returns = to_device(torch.ones(num_eval, 1) * Config.test_ret, device)
    # partial = 0.8
    # return_part_1 = returns[:int(partial * num_eval), :] * Config.test_ret
    # return_part_2 = returns[int(partial * num_eval):, :] * 0.5
    # returns = torch.cat((return_part_1, return_part_2), dim=0)

    t = 0
    obs_list = [env.reset()[None] for env in env_list]
    obs = np.concatenate(obs_list, axis=0)
    recorded_obs = [deepcopy(obs[:, None])]

    reward_list = [[] for i in range(num_eval)]
    true_list = [[] for i in range(num_eval)]

    traj_length = 1000
    state_dim = env_list[0].observation_space.shape[0]
    action_dim = env_list[0].action_space.shape[0]
    length = [0 for i in range(num_eval)]
    traj = [Trajectory(length=traj_length, state_dim=state_dim, action_dim=action_dim) for i in range(num_eval)]

    # while sum(dones) < num_eval:
    #     obs = dataset.normalizer.normalize(obs, 'observations')
    #     conditions = {0: to_torch(obs, device=device)}
    #     samples = trainer.ema_model.conditional_sample(conditions, returns=returns)
    #     samples, sample_reward = samples[:, :, :-1], samples[:, :, -1]
    #     obs_comb = torch.cat([samples[:, 0, :], samples[:, 1, :]], dim=-1)  # TODO:increase window length
    #     obs_comb = obs_comb.reshape(-1, 2 * observation_dim)
    #     action = trainer.ema_model.inv_model(obs_comb)
    #
    #     samples = to_np(samples)
    #     # samples = dataset.normalizer.unnormalize(samples, 'observations')
    #     action = to_np(action)
    #     sample_t = to_np(samples[:, 0, :])
    #
    #     reward_t = to_np(sample_reward[:, 0])
    #     reward_t_1 = to_np(sample_reward[:, 1])
    #
    #     # action = dataset.normalizer.unnormalize(action, 'actions')
    #
    #     # if t == 0:
    #     #     normed_observations = samples[:, :, :]
    #     #     observations = dataset.normalizer.unnormalize(normed_observations, 'observations')
    #     #     savepath = os.path.join('images', 'sample-planned.png')
    #     #     renderer.composite(savepath, observations)
    #
    #     obs_list = []
    #
    #     reward_t = np.array(reward_t)
    #     reward_t = reward_t
    #
    #     for i in range(num_eval):
    #         traj[i].add_item(input_state=sample_t[i, :], input_action=action[i], input_reward=reward_t[i],
    #                          idx=length[i])
    #         length[i] += 1
    #         this_obs, this_reward, this_done, _ = env_list[i].step(action[i])
    #         true_list[i].append(this_reward)
    #         reward_list[i].append(reward_t)
    #         # logger.print(f"R_t: {R_t[i] * 400 * 4}")    #for horizon 100 (100 * 3)
    #         # logger.print(f"R_t_1: {R_t_1[i] * 400 * 4}")
    #         # logger.print(f"this reward {this_reward}")
    #
    #         obs_list.append(this_obs[None])
    #         if this_done:
    #             if dones[i] == 1:
    #                 pass
    #             else:
    #                 dones[i] = 1
    #                 episode_rewards[i] += this_reward
    #                 logger.print(f"Episode ({i}): {episode_rewards[i]}", color='green')
    #         else:
    #             if dones[i] == 1:
    #                 pass
    #             else:
    #                 episode_rewards[i] += this_reward
    #
    #     obs = np.concatenate(obs_list, axis=0)
    #     recorded_obs.append(deepcopy(obs[:, None]))
    #     t += 1
    q = queue.Queue(1)

    while sum(dones) < num_eval:
        conditions = {}
        obs = dataset.normalizer.normalize(obs, 'observations')
        if q.full():
            _ = q.get()
            q.put(obs)
        else:
            q.put(obs)
        for i, item in enumerate(q.queue):
            conditions.update({i: to_torch(item, device=device)})
        conditions_length = len(conditions)
        # conditions = {0: to_torch(obs, device=device)}
        samples = trainer.ema_model.conditional_sample(conditions, returns=returns, horizon=Config.horizon + 20)
        samples, sample_reward = samples[:, :, :-1], samples[:, :, -1]
        obs_comb = torch.cat([samples[:, conditions_length - 1, :], samples[:, conditions_length, :]], dim=-1)      #TODO:increase window length
        obs_comb = obs_comb.reshape(-1, 2*observation_dim)
        action = trainer.ema_model.inv_model(obs_comb)
        sample_t = to_np(samples[:, conditions_length - 1, :])

        samples = to_np(samples)
        action = to_np(action)

        reward_t = to_np(sample_reward[:, conditions_length - 1])
        reward_t_1 = to_np(sample_reward[:, 1])

        action = dataset.normalizer.unnormalize(action, 'actions')


        # if t == 0:
        #     normed_observations = samples[:, :, :]
        #     observations = dataset.normalizer.unnormalize(normed_observations, 'observations')
        #     savepath = os.path.join('images', 'sample-planned.png')
        #     renderer.composite(savepath, observations)

        obs_list = []
        for i in range(num_eval):

            length[i] += 1
            this_obs, this_reward, this_done, _ = env_list[i].step(action[i])

            traj[i].add_item(input_state=sample_t[i, :], input_action=action[i], input_reward=reward_t[i],
                               idx=length[i])
            # traj[i].add_item(input_state=sample_t[i, :], input_action=action[i], input_reward=this_reward,
            #                  idx=length[i])

            true_list[i].append(this_reward)
            reward_list[i].append(reward_t[i])
            # logger.print(f"R_t: {R_t[i] * 400 * 4}")    #for horizon 100 (100 * 3)
            # logger.print(f"R_t_1: {R_t_1[i] * 400 * 4}")
            # logger.print(f"this reward {this_reward}")

            obs_list.append(this_obs[None])
            if this_done:
                if dones[i] == 1:
                    pass
                else:
                    dones[i] = 1
                    episode_rewards[i] += this_reward
                    # total_returns.append(episode_rewards[i])
                    logger.print(f"Episode ({i}): {episode_rewards[i]}", color='green')
            else:
                if dones[i] == 1:
                    pass
                else:
                    episode_rewards[i] += this_reward
                    print("this reward", this_reward)

        obs = np.concatenate(obs_list, axis=0)
        recorded_obs.append(deepcopy(obs[:, None]))
        t += 1

    save_traj(traj, Config.bucket)

    new_paths = []

    for path_num in range(len(traj)):
        length = len(traj[path_num].state)
        observations = np.array([])
        actions = np.array([])
        rewards = np.array([])
        next_state = np.array([])
        for idx in range(length):
            if idx == 0:
                observations, actions, rewards, next_state = traj[path_num].get_item(idx)
                observations = np.array([observations])
                actions = np.array([actions])
                rewards = np.array([[rewards]])
                next_state = np.array([next_state])
            else:
                temp0, temp1, temp2, temp3 = traj[path_num].get_item(idx)
                observations = np.concatenate((observations, np.array([temp0])), axis=0)
                actions = np.concatenate((actions, np.array([temp1])), axis=0)
                rewards = np.concatenate((rewards, np.array([[temp2]])), axis=0)
                next_state = np.concatenate((next_state, np.array([temp3])), axis=0)
        # print("Current EP reward:", np.sum(rewards) * 4)
        if "kitchen" in Config.dataset:
            rewards = np.expand_dims(np.cumsum(rewards), axis=1) / 3
        paths.append([observations, actions, rewards, next_state])

    new_paths = paths
    savepath = os.path.join(Config.bucket, logger.prefix, 'checkpoint')
    os.makedirs(savepath, exist_ok=True)
    filename = "new_dataset_test.dat"
    savepath = os.path.join(savepath, filename)
    with open(savepath, "wb") as f:
        pickle.dump(new_paths, f)

    path_num = len(new_paths)
    traj_length = 1000
    new_traj = [Trajectory(length=traj_length, state_dim=state_dim, action_dim=action_dim) for i in range(path_num)]
    for path_ind in range(path_num):
        length = paths[path_ind][0].shape[0]
        for i in range(length):
            new_traj[path_ind].add_item(input_state=paths[path_ind][0][i], input_action=paths[path_ind][1][i], input_reward=paths[path_ind][2][i], idx=i)

    # for i in range(num_eval):
    #     print(f"true {i}", true_list[i])
    #     print(f"reward {i}", reward_list[i] * 4)
    #
    # recorded_obs = np.concatenate(recorded_obs, axis=1)
    # savepath = os.path.join('images', f'sample-executed.png')
    # renderer.composite(savepath, recorded_obs)
    # episode_rewards = np.array(episode_rewards)
    #
    # logger.print(f"average_ep_reward: {np.mean(episode_rewards)}, std_ep_reward: {np.std(episode_rewards)}",
    #              color='green')
    # logger.log_metrics_summary(
    #     {'average_ep_reward': np.mean(episode_rewards), 'std_ep_reward': np.std(episode_rewards)})
    train_val(new_traj, state_dim, action_dim, Config.device, Config.seed, Config.bucket)
