import os.path as osp

import torch
import torch.nn as nn
from torch.nn import functional as F
from torch.cuda.amp import GradScaler, autocast

from dassl.engine import TRAINER_REGISTRY, TrainerX
from dassl.metrics import compute_accuracy
from dassl.utils import load_pretrained_weights, load_checkpoint
from dassl.optim import build_optimizer, build_lr_scheduler

from clip_w_local import clip
from clip_w_local.simple_tokenizer import SimpleTokenizer as _Tokenizer
import numpy as np
from tqdm import tqdm
from PIL import Image


_tokenizer = _Tokenizer()
softmax = nn.Softmax(dim=1).cuda()


def entropy_select_topk(p, top_k, label, num_of_local_feature, nat_probs):
    """
    Extract non-Top-K regions and calculate entropy.
    """
    true_probs = torch.gather(nat_probs, 1, (label.unsqueeze(1)).long()).squeeze()
    true_probs_repeat = true_probs.repeat_interleave(num_of_local_feature)

    label_repeat = label.repeat_interleave(num_of_local_feature)
    p = F.softmax(p, dim=-1)
    pred_topk = torch.topk(p, k=top_k, dim=1)[1]
    contains_label = pred_topk.eq(torch.tensor(label_repeat).unsqueeze(1)).any(dim=1)
    selected_p = p[~contains_label]
    selected_true_probs = true_probs_repeat[~contains_label]
    #print('selected_true_prob.shape',selected_true_probs.shape)
    #print('contains_label.shape',contains_label.shape)

    if selected_p.shape[0] == 0:
        return torch.tensor([0]).cuda()

    return -torch.mean(torch.sum(selected_p * torch.log(selected_p+1e-5), 1) * (0.0000001 + selected_true_probs) ),contains_label,true_probs_repeat

def load_clip_to_cpu(cfg):
    backbone_name = cfg.MODEL.BACKBONE.NAME
    url = clip._MODELS[backbone_name]
    model_path = clip._download(url)

    try:
        # loading JIT archive
        model = torch.jit.load(model_path, map_location="cpu").eval()
        state_dict = None

    except RuntimeError:
        state_dict = torch.load(model_path, map_location="cpu")

    model = clip.build_model(state_dict or model.state_dict())

    return model


class TextEncoder(nn.Module):
    def __init__(self, clip_model):
        super().__init__()
        self.transformer = clip_model.transformer
        self.positional_embedding = clip_model.positional_embedding
        self.ln_final = clip_model.ln_final
        self.text_projection = clip_model.text_projection
        self.dtype = clip_model.dtype
        
    def forward(self, prompts, tokenized_prompts):
        x = prompts 
        #W=x[:,:,:].T
        W = x.permute(0, 1, 2)[0,:,:] 
        x=x+ self.positional_embedding.type(self.dtype)
        #W = x.permute(0, 1, 2) 
        #W=x[:,:,:].T
        x = x.permute(1, 0, 2)  # NLD -> LND
        x, _, _, _ = self.transformer(x)
        x = x.permute(1, 0, 2)  # LND -> NLD
        x = self.ln_final(x).type(self.dtype)
        #W=x[:,:,:].mean(dim=0).T
        

        # x.shape = [batch_size, n_ctx, transformer.width]
        # take features from the eot embedding (eot_token is the highest number in each sequence)
        x = x[torch.arange(x.shape[0]), tokenized_prompts.argmax(dim=-1)] @ self.text_projection

        return x,W

class PromptLearner(nn.Module):
    def __init__(self, cfg, classnames, clip_model):
        super().__init__()
        n_cls = len(classnames)
        n_ctx = cfg.TRAINER.LOCOOP.N_CTX
        ctx_init = cfg.TRAINER.LOCOOP.CTX_INIT
        dtype = clip_model.dtype
        ctx_dim = clip_model.ln_final.weight.shape[0]
        clip_imsize = clip_model.visual.input_resolution
        cfg_imsize = cfg.INPUT.SIZE[0]
        assert cfg_imsize == clip_imsize, f"cfg_imsize ({cfg_imsize}) must equal to clip_imsize ({clip_imsize})"

        if ctx_init:
            # use given words to initialize context vectors
            ctx_init = ctx_init.replace("_", " ")
            n_ctx = len(ctx_init.split(" "))
            prompt = clip.tokenize(ctx_init)
            with torch.no_grad():
                embedding = clip_model.token_embedding(prompt).type(dtype)
            ctx_vectors = embedding[0, 1 : 1 + n_ctx, :]
            prompt_prefix = ctx_init

        else:
            # random initialization
            if cfg.TRAINER.LOCOOP.CSC:
                print("Initializing class-specific contexts")
                ctx_vectors = torch.empty(n_cls, n_ctx, ctx_dim, dtype=dtype)
            else:
                print("Initializing a generic context")
                ctx_vectors = torch.empty(n_ctx, ctx_dim, dtype=dtype)
            nn.init.normal_(ctx_vectors, std=0.02)
            prompt_prefix = " ".join(["X"] * n_ctx)

        print(f'Initial context: "{prompt_prefix}"')
        print(f"Number of context words (tokens): {n_ctx}")

        self.ctx = nn.Parameter(ctx_vectors)  # to be optimized

        classnames = [name.replace("_", " ") for name in classnames]
        name_lens = [len(_tokenizer.encode(name)) for name in classnames]
        prompts = [prompt_prefix + " " + name + "." for name in classnames]

        tokenized_prompts = torch.cat([clip.tokenize(p) for p in prompts])
        with torch.no_grad():
            embedding = clip_model.token_embedding(tokenized_prompts).type(dtype)

        # These token vectors will be saved when in save_model(),
        # but they should be ignored in load_model() as we want to use
        # those computed using the current class names
        self.register_buffer("token_prefix", embedding[:, :1, :])  # SOS
        self.register_buffer("token_suffix", embedding[:, 1 + n_ctx :, :])  # CLS, EOS

        self.n_cls = n_cls
        self.n_ctx = n_ctx
        self.tokenized_prompts = tokenized_prompts  # torch.Tensor
        self.name_lens = name_lens
        self.class_token_position = cfg.TRAINER.LOCOOP.CLASS_TOKEN_POSITION

    def forward(self):
        ctx = self.ctx
        if ctx.dim() == 2:
            ctx = ctx.unsqueeze(0).expand(self.n_cls, -1, -1)

        prefix = self.token_prefix
        suffix = self.token_suffix

        if self.class_token_position == "end":
            prompts = torch.cat(
                [
                    prefix,  # (n_cls, 1, dim)
                    ctx,     # (n_cls, n_ctx, dim)
                    suffix,  # (n_cls, *, dim)
                ],
                dim=1,
            )

        elif self.class_token_position == "middle":
            half_n_ctx = self.n_ctx // 2
            prompts = []
            for i in range(self.n_cls):
                name_len = self.name_lens[i]
                prefix_i = prefix[i : i + 1, :, :]
                class_i = suffix[i : i + 1, :name_len, :]
                suffix_i = suffix[i : i + 1, name_len:, :]
                ctx_i_half1 = ctx[i : i + 1, :half_n_ctx, :]
                ctx_i_half2 = ctx[i : i + 1, half_n_ctx:, :]
                prompt = torch.cat(
                    [
                        prefix_i,     # (1, 1, dim)
                        ctx_i_half1,  # (1, n_ctx//2, dim)
                        class_i,      # (1, name_len, dim)
                        ctx_i_half2,  # (1, n_ctx//2, dim)
                        suffix_i,     # (1, *, dim)
                    ],
                    dim=1,
                )
                prompts.append(prompt)
            prompts = torch.cat(prompts, dim=0)

        elif self.class_token_position == "front":
            prompts = []
            for i in range(self.n_cls):
                name_len = self.name_lens[i]
                prefix_i = prefix[i : i + 1, :, :]
                class_i = suffix[i : i + 1, :name_len, :]
                suffix_i = suffix[i : i + 1, name_len:, :]
                ctx_i = ctx[i : i + 1, :, :]
                prompt = torch.cat(
                    [
                        prefix_i,  # (1, 1, dim)
                        class_i,   # (1, name_len, dim)
                        ctx_i,     # (1, n_ctx, dim)
                        suffix_i,  # (1, *, dim)
                    ],
                    dim=1,
                )
                prompts.append(prompt)
            prompts = torch.cat(prompts, dim=0)

        else:
            raise ValueError

        return prompts


class CustomCLIP(nn.Module):
    def __init__(self, cfg, classnames, clip_model):
        super().__init__()
        self.prompt_learner = PromptLearner(cfg, classnames, clip_model)
        self.tokenized_prompts = self.prompt_learner.tokenized_prompts
        self.image_encoder = clip_model.visual
        self.text_encoder = TextEncoder(clip_model)
        self.logit_scale = clip_model.logit_scale
        self.dtype = clip_model.dtype

    def forward(self, image):
        image_features, local_image_features = self.image_encoder(image.type(self.dtype))

        prompts = self.prompt_learner()
        #print('image_features.shape',image_features.shape)
        #print('local_image_features.shape',local_image_features.shape)
        tokenized_prompts = self.tokenized_prompts
        #text_features = self.text_encoder(prompts, tokenized_prompts)
        text_features,W = self.text_encoder(prompts, tokenized_prompts)
        epsilon = 1e-2
        W_fp32 = W.float() 
        #print('W_fp32',W_fp32)
        N, D = W_fp32.shape
        epsilon = W_fp32.new_tensor(1e-3)
        I = torch.eye(N, device=W.device, dtype=W_fp32.dtype)
        I2 = torch.eye(D, device=W.device, dtype=W_fp32.dtype)
        P =I2- W_fp32.T @ torch.linalg.pinv(W_fp32 @ W_fp32.T + epsilon * I) @ W_fp32 
        P2 = W_fp32.T @ torch.linalg.pinv(W_fp32 @ W_fp32.T + epsilon * I) @ W_fp32 
        P = P.to(W.dtype)
        P2 = P2.to(W.dtype)  
        project_text = text_features @ P2
        image_features = image_features / image_features.norm(dim=-1, keepdim=True)
        local_image=local_image_features
        local_image_features = local_image_features / local_image_features.norm(dim=-1, keepdim=True)
        reg_loss_text =torch.norm(project_text, p=2, dim=1)
        text_features = text_features / text_features.norm(dim=-1, keepdim=True)
        logit_scale = self.logit_scale.exp()
        logits = logit_scale * image_features @ text_features.t()
        logits_local = logit_scale * local_image_features @ text_features.T
        return logits, logits_local, P,P2,local_image


@TRAINER_REGISTRY.register()
class LoCoOp(TrainerX):
    """Local regularized Context Optimization (LoCoOp).
    """

    def check_cfg(self, cfg):
        assert cfg.TRAINER.LOCOOP.PREC in ["fp16", "fp32", "amp"]

    def build_model(self):
        cfg = self.cfg
        classnames = self.dm.dataset.classnames

        self.lambda_value = cfg.lambda_value
        self.top_k = cfg.topk

        print(f"Loading CLIP (backbone: {cfg.MODEL.BACKBONE.NAME})")
        clip_model = load_clip_to_cpu(cfg)

        if cfg.TRAINER.LOCOOP.PREC == "fp32" or cfg.TRAINER.LOCOOP.PREC == "amp":
            # CLIP's default precision is fp16
            clip_model.float()

        print("Building custom CLIP")
        self.model = CustomCLIP(cfg, classnames, clip_model)

        print("Turning off gradients in both the image and the text encoder")
        for name, param in self.model.named_parameters():
            if "prompt_learner" not in name:
                param.requires_grad_(False)

        if cfg.MODEL.INIT_WEIGHTS:
            load_pretrained_weights(self.model.prompt_learner, cfg.MODEL.INIT_WEIGHTS)

        self.model.to(self.device)
        # NOTE: only give prompt_learner to the optimizer
        self.optim = build_optimizer(self.model.prompt_learner, cfg.OPTIM)
        self.sched = build_lr_scheduler(self.optim, cfg.OPTIM)
        self.register_model("prompt_learner", self.model.prompt_learner, self.optim, self.sched)

        self.scaler = GradScaler() if cfg.TRAINER.LOCOOP.PREC == "amp" else None

        # Note that multi-gpu training could be slow because CLIP's size is
        # big, which slows down the copy operation in DataParallel
        device_count = torch.cuda.device_count()
        if device_count > 1:
            print(f"Multiple GPUs detected (n_gpus={device_count}), use all of them!")
            self.model = nn.DataParallel(self.model)

    def forward_backward(self, batch):
        image, label = self.parse_batch_train(batch)

        prec = self.cfg.TRAINER.LOCOOP.PREC

        if prec == "amp":
            with autocast():
                output, output_local,_,_,_= self.model(image)
                # calculate CoOp loss
                #loss_id = F.cross_entropy(output, label)
                label_onehot = F.one_hot(label, num_classes=self.num_classes)
                probs = F.softmax(output, dim=1)
                true_probs = torch.gather(probs, 1, (label.unsqueeze(1)).long()).squeeze()
                loss = -torch.sum(label_onehot * torch.log(probs + 1e-12), dim=1) * (1.0000001 - true_probs)
                loss_id = loss.mean()

                # calculate OOD regularization loss
                batch_size, num_of_local_feature = output_local.shape[0], output_local.shape[1]
                output_local = output_local.view(batch_size * num_of_local_feature, -1)
                loss_en = - entropy_select_topk(output_local, self.top_k, label, num_of_local_feature, probs)

                # calculate total loss for sct
                loss = loss_id + self.lambda_value * loss_en

            self.optim.zero_grad()
            self.scaler.scale(loss).backward()
            self.scaler.step(self.optim)
            self.scaler.update()
        else:
            output, output_local, P,P2, local_image_features = self.model(image)

            # calculate CoOp loss
            #loss_id = F.cross_entropy(output, label)
            label_onehot = F.one_hot(label, num_classes=self.num_classes)
            probs = F.softmax(output, dim=1)
            true_probs = torch.gather(probs, 1, (label.unsqueeze(1)).long()).squeeze()
            loss = -torch.sum(label_onehot * F.log_softmax(output, dim=1), dim=1) * (1.0000001 - true_probs)
            loss_id = loss.mean()

            # calculate OOD regularization loss
            batch_size, num_of_local_feature = output_local.shape[0], output_local.shape[1]
            output_local = output_local.view(batch_size * num_of_local_feature, -1)     
            loss_en2,contains_label,true_probs_repeat =  entropy_select_topk(output_local, self.top_k, label, num_of_local_feature,probs)
            loss_en=-loss_en2
            local2=local_image_features.view(batch_size * num_of_local_feature, -1)
            local3 = local2[contains_label]
            #print('local3',local3)
            local4 = local2[~contains_label]
            #print('local3.shape',local3.shape)
            #print('local_image_features.shape',local_image_features.shape)
            #print('local4',local4)
            #project_img_local = local3 @ P
            #project_img_local2 = local4 @ P2
            project_img_local = torch.matmul(local3, P) 
            project_img_local2= torch.matmul(local4, P2) 
            #print('project_img_local.shape',project_img_local.shape)
            reg_loss_img_local = torch.norm(project_img_local, p=2, dim=-1) / torch.norm(local3, p=2, dim=-1)    
            reg_loss_img_local2=torch.norm(project_img_local2, p=2, dim=-1) / torch.norm(local4, p=2, dim=-1) 
            reg_loss_img_local=reg_loss_img_local*(0.00001+true_probs_repeat[contains_label].T)
            reg_loss_img_local2=reg_loss_img_local2*(0.00001+true_probs_repeat[~contains_label].T)
            #print('true_probs_repeat~contains_label',true_probs_repeat[~contains_label])
            #print('project_img_local',project_img_local)
            #print('project_img_local2',reg_loss_img_local2)
            
            tt=reg_loss_img_local.mean()
            img=reg_loss_img_local2.mean()
            #loss = 1.5*loss_id + self.lambda_value * loss_en +1*reg_loss_img_local.mean()+1*reg_loss_img_local2.mean()
            loss = 1.5*loss_id + self.lambda_value * loss_en +2*reg_loss_img_local.mean()+5*reg_loss_img_local2.mean()

            self.model_backward_and_update(loss)

        loss_summary = {
            "loss": loss.item(),
            "loss_id": loss_id.item(),
            "loss_en": loss_en.item(),
            "acc": compute_accuracy(output, label)[0].item(),
        }

        if (self.batch_idx + 1) == self.num_batches:
            self.update_lr()

        return loss_summary
    def parse_batch_train(self, batch):
        input = batch["img"]
        label = batch["label"]
        input = input.to(self.device)
        label = label.to(self.device)
        return input, label

    def load_model(self, directory, epoch=None):
        if not directory:
            print("Note that load_model() is skipped as no pretrained model is given")
            return

        names = self.get_model_names()

        # By default, the best model is loaded
        model_file = "model-best.pth.tar"

        if epoch is not None:
            model_file = "model.pth.tar-" + str(epoch)

        for name in names:
            model_path = osp.join(directory, name, model_file)

            if not osp.exists(model_path):
                raise FileNotFoundError('Model not found at "{}"'.format(model_path))

            checkpoint = load_checkpoint(model_path)
            state_dict = checkpoint["state_dict"]
            epoch = checkpoint["epoch"]

            # Ignore fixed token vectors
            if "token_prefix" in state_dict:
                del state_dict["token_prefix"]

            if "token_suffix" in state_dict:
                del state_dict["token_suffix"]

            print("Loading weights to {} " 'from "{}" (epoch = {})'.format(name, model_path, epoch))
            # set strict=False
            self._models[name].load_state_dict(state_dict, strict=False)

    @torch.no_grad()
    def test(self, split=None):
        """A generic testing pipeline."""
        self.set_model_mode("eval")
        self.evaluator.reset()

        if split is None:
            split = self.cfg.TEST.SPLIT

        if split == "val" and self.val_loader is not None:
            data_loader = self.val_loader
        else:
            split = "test"  # in case val_loader is None
            data_loader = self.test_loader

        print(f"Evaluate on the *{split}* set")

        for batch_idx, batch in enumerate(tqdm(data_loader)):
            input, label = self.parse_batch_test(batch)
            output = self.model_inference(input)
            if len(output) == 4:
                output = output[0]
            self.evaluator.process(output, label)

        results = self.evaluator.evaluate()

        for k, v in results.items():
            tag = f"{split}/{k}"
            self.write_scalar(tag, v, self.epoch)

        return list(results.values())[0]

    @torch.no_grad()
    def test_ood(self, data_loader, T, id_flag):
        """Test-time OOD detection pipeline."""
        to_np = lambda x: x.data.cpu().numpy()
        concat = lambda x: np.concatenate([t.detach().cpu().numpy() if isinstance(t, torch.Tensor) else t for t in x], axis=0)

        self.set_model_mode("eval")
        self.evaluator.reset()

        glmcm_score = []
        mcm_score = []
        correct = 0
        for batch_idx, (images, labels) in enumerate(tqdm(data_loader)):
            images = images.cuda()
            output, output_local, P,P2, local_image_features= self.model_inference(images)

            if id_flag:
                correct += (output.argmax(1) == labels.cuda()).float().cpu().sum().item()

            output /= 100.0
            output_local /= 100.0
            batch_size, num_of_local_feature = output_local.shape[0], output_local.shape[1]
            local2=local_image_features.view(batch_size * num_of_local_feature, -1)
            project_img_local2 = torch.matmul(local_image_features, P2) 
            reg_loss_img_local2=torch.norm(project_img_local2, p=2, dim=-1)/torch.norm(local_image_features, p=2, dim=-1)
            reg_loss_img_local2=-reg_loss_img_local2.mean(dim=1)


            smax_global = to_np(F.softmax(output/T, dim=-1))
            smax_local = to_np(F.softmax(output_local/T, dim=-1))
            mcm_global_score = -np.max(smax_global, axis=1)
            mcm_local_score = -np.max(smax_local, axis=(1, 2))
            #mcm_score.append(mcm_global_score)
            mcm_score.append(mcm_global_score+mcm_local_score)
            #mcm_score.append(mcm_local_score)
            combined_score =0.0001*reg_loss_img_local2.detach().cpu().numpy()+mcm_global_score+mcm_local_score
            glmcm_score.append(combined_score)

        if id_flag:
            return concat(mcm_score)[:len(data_loader.dataset)].copy(), concat(glmcm_score)[:len(
                data_loader.dataset)].copy(), correct / len(data_loader.dataset)
        else:
            return concat(mcm_score)[:len(data_loader.dataset)].copy(), concat(glmcm_score)[:len(
                data_loader.dataset)].copy()

    @torch.no_grad()
    def test_visualize(self, img_path, label):
        """code for visualization results"""
        self.set_model_mode("eval")
        self.evaluator.reset()

        device = "cuda" if torch.cuda.is_available() else "cpu"
        model, preprocess = clip.load("ViT-B/16", device=device)

        image = preprocess(Image.open(img_path)).unsqueeze(0).to(device)
        output, output_local,_,_ = self.model_inference(image)

        num_regions = output_local.shape[1]
        label = torch.tensor(label).cuda()
        label_repeat = label.repeat_interleave(num_regions)
        output_local = F.softmax(output_local, dim=-1)

        output_local = output_local.view(num_regions, -1)

        # -----top 200--------
        pred_topk = torch.topk(output_local, k=200, dim=1)[1]
        contains_label = pred_topk.eq(torch.tensor(label_repeat).unsqueeze(1)).any(dim=1)

        return contains_label
