import logging
import random

import torch
from torch.cuda.amp import autocast as autocast
import torch.nn as nn

from seechat.common.registry import registry
from seechat.models.blip2 import Blip2Base, disabled_train
from seechat.models.modeling_llama import LlamaForCausalLM
from transformers import LlamaTokenizer
from transformers import AutoTokenizer, AutoModel, AutoConfig


@registry.register_model("see_chat")
class SEEChat(Blip2Base):
    """
    BLIP2 GPT-LLAMA model.
    """

    PRETRAINED_MODEL_CONFIG_DICT = {
        "pretrain_vicuna": "configs/models/seechat.yaml",
        "pretrain_glm": "configs/models/seechat_glm.yaml",
    }

    def __init__(
            self,
            vit_model="eva_clip_g",
            q_former_model="https://storage.googleapis.com/sfr-vision-language-research/LAVIS/models/BLIP2/blip2_pretrained_flant5xxl.pth",
            img_size=224,
            drop_path_rate=0,
            use_grad_checkpoint=False,
            vit_precision="fp16",
            freeze_vit=True,
            freeze_qformer=True,
            num_query_token=32,
            llama_model="",
            model_type="",
            prompt_path="",
            prompt_template="",
            max_txt_len=32,
            end_sym='\n',
            low_resource=False,  # use 8 bit and put vit in cpu
            device_8bit=0,  # the device of 8bit model should be set when loading and cannot be changed anymore.
    ):
        super().__init__()

        self.model_type = model_type
        self.tokenizer = self.init_tokenizer(llama_model)  # 似乎没人用？？
        self.low_resource = low_resource

        print('Loading VIT')
        self.visual_encoder, self.ln_vision = self.init_vision_encoder(
            vit_model, img_size, drop_path_rate, use_grad_checkpoint, vit_precision
        )
        if freeze_vit:
            for name, param in self.visual_encoder.named_parameters():
                param.requires_grad = False
            self.visual_encoder = self.visual_encoder.eval()
            self.visual_encoder.train = disabled_train

            for name, param in self.ln_vision.named_parameters():
                param.requires_grad = False
            self.ln_vision = self.ln_vision.eval()
            self.ln_vision.train = disabled_train
            logging.info("freeze vision encoder")
        print('Loading VIT Done')

        print('Loading Q-Former')
        self.Qformer, self.query_tokens = self.init_Qformer(
            num_query_token, self.visual_encoder.num_features
        )
        self.Qformer.cls = None
        self.Qformer.bert.embeddings.word_embeddings = None
        self.Qformer.bert.embeddings.position_embeddings = None
        for layer in self.Qformer.bert.encoder.layer:
            layer.output = None
            layer.intermediate = None
        # load Qformer model
        self.load_from_pretrained(url_or_filename=q_former_model)

        if freeze_qformer:
            for name, param in self.Qformer.named_parameters():
                param.requires_grad = False
            self.Qformer = self.Qformer.eval()
            self.Qformer.train = disabled_train
            self.query_tokens.requires_grad = False
            logging.info("freeze Qformer")
        else:
            logging.info("!!! NOT freeze Qformer")
        print('Loading Q-Former Done')

        print('Loading LLM')
        if "vicuna" in self.model_type:
            self.llama_tokenizer = LlamaTokenizer.from_pretrained(llama_model, use_fast=False)
            self.llama_tokenizer.pad_token = self.llama_tokenizer.eos_token  # vicuna: </s>  glm:<eop>
        elif "glm" in self.model_type:
            self.llama_tokenizer = AutoTokenizer.from_pretrained("../models/chatglm-6b",
                                                                 trust_remote_code=True)
        else:
            print("not a valid LLM")

        if self.low_resource: 
            if "vicuna" in self.model_type:
                self.llama_model = LlamaForCausalLM.from_pretrained(
                    llama_model,
                    torch_dtype=torch.float16,
                    load_in_8bit=True,
                    device_map={'': device_8bit}
                )
            elif "glm" in self.model_type:
                self.llama_model = AutoModel.from_pretrained("../models/chatglm-6b",
                                                             trust_remote_code=True).half().cuda()
            else:
                print(f"{self.model_type} is not implemented")
        else:
            if "vicuna" in self.model_type:
                self.llama_model = LlamaForCausalLM.from_pretrained(
                    llama_model, torch_dtype=torch.float16,
                )
            elif "glm" in self.model_type:
                config = AutoConfig.from_pretrained("../models/chatglm-6b/",
                                                    trust_remote_code=True)
                config.pre_seq_len = 128
                config.prefix_projection = False
                self.llama_model = AutoModel.from_pretrained("../models/chatglm-6b/",
                                                             config=config, local_files_only=True,
                                                             trust_remote_code=True).half().cuda()

        for name, param in self.llama_model.named_parameters():
            param.requires_grad = False
        print('Loading LLM Done')

        self.llama_proj = nn.Linear(
            self.Qformer.config.hidden_size, self.llama_model.config.hidden_size
        )
        self.max_txt_len = max_txt_len
        self.end_sym = end_sym


        if prompt_path:
            with open(prompt_path, 'r') as f:
                raw_prompts = f.read().splitlines()
            filted_prompts = [raw_prompt for raw_prompt in raw_prompts if "<ImageHere>" in raw_prompt]
            self.prompt_list = [prompt_template.format(p) for p in filted_prompts]
        else:
            self.prompt_list = []

    def vit_to_cpu(self):
        self.ln_vision.to("cpu")
        self.ln_vision.float()
        self.visual_encoder.to("cpu")
        self.visual_encoder.float()


    def encode_img(self, image):
        device = image.device
        if self.low_resource:
            self.vit_to_cpu()
            image = image.to("cpu")

        with self.maybe_autocast():
            image_embeds = self.ln_vision(self.visual_encoder(image)).to(device)
            image_atts = torch.ones(image_embeds.size()[:-1], dtype=torch.long).to(device)
            query_tokens = self.query_tokens.expand(image_embeds.shape[0], -1, -1)
            query_output = self.Qformer.bert(
                query_embeds=query_tokens,
                encoder_hidden_states=image_embeds,
                encoder_attention_mask=image_atts,
                return_dict=True,
            )


            inputs_llama = self.llama_proj(query_output.last_hidden_state)
            atts_llama = torch.ones(inputs_llama.size()[:-1], dtype=torch.long).to(image.device)
        return inputs_llama, atts_llama


    def prompt_wrap(self, img_embeds, atts_img, prompt):
        if prompt:
            batch_size = img_embeds.shape[0]
            p_before, p_after = prompt.split('<ImageHere>')
            p_before_tokens = self.llama_tokenizer(
                p_before, return_tensors="pt", add_special_tokens=False).to(img_embeds.device)
            p_after_tokens = self.llama_tokenizer(
                p_after, return_tensors="pt", add_special_tokens=False).to(img_embeds.device)
            p_before_embeds = self.llama_model.model.embed_tokens(p_before_tokens.input_ids).expand(batch_size, -1, -1)
            p_after_embeds = self.llama_model.model.embed_tokens(p_after_tokens.input_ids).expand(batch_size, -1, -1)

            wrapped_img_embeds = torch.cat([p_before_embeds, img_embeds, p_after_embeds], dim=1)
            wrapped_atts_img = atts_img[:, :1].expand(-1, wrapped_img_embeds.shape[1])
            return wrapped_img_embeds, wrapped_atts_img
        else:
            return img_embeds, atts_img

    def forward(self, samples):
        # samples = { "image":,  "text_input": }
        if "vicuna" in self.model_type:

            image = samples["image"]
            img_embeds, atts_img = self.encode_img(image)


            if hasattr(samples, 'question_split'):  # VQA dataset
                print('VQA Batch')
                vqa_prompt = '###Human: <Img><ImageHere></Img> '
                img_embeds, atts_img = self.prompt_wrap(img_embeds, atts_img, vqa_prompt)
            elif self.prompt_list:  # stage2
                prompt = random.choice(self.prompt_list)
                img_embeds, atts_img = self.prompt_wrap(img_embeds, atts_img, prompt)

            empty_targets = (
                torch.ones([atts_img.shape[0], atts_img.shape[1] + 1],
                           dtype=torch.long).to(image.device).fill_(-100)  # plus one for bos
            )

            # llama_tokenizer
            self.llama_tokenizer.padding_side = "right"

            # text embedding, attention mask, target
            text = [t + self.end_sym for t in samples["text_input"]]
            to_regress_tokens = self.llama_tokenizer(
                text,
                return_tensors="pt",
                padding="longest",
                truncation=True,
                max_length=self.max_txt_len,
                add_special_tokens=False
            ).to(image.device)

            to_regress_embeds = self.llama_model.model.embed_tokens(to_regress_tokens.input_ids)

            targets = to_regress_tokens.input_ids.masked_fill(
                to_regress_tokens.input_ids == self.llama_tokenizer.pad_token_id, -100
            )


            batch_size = img_embeds.shape[0]
            bos = torch.ones([batch_size, 1],
                             dtype=to_regress_tokens.input_ids.dtype,
                             device=to_regress_tokens.input_ids.device) * self.llama_tokenizer.bos_token_id

            bos_embeds = self.llama_model.model.embed_tokens(bos)
            atts_bos = atts_img[:, :1]

            inputs_embeds = torch.cat([bos_embeds, img_embeds, to_regress_embeds], dim=1)

            attention_mask = torch.cat([atts_bos, atts_img, to_regress_tokens.attention_mask], dim=1)

            targets = torch.cat([empty_targets, targets], dim=1)

            with self.maybe_autocast():
                outputs = self.llama_model(
                    inputs_embeds=inputs_embeds,
                    attention_mask=attention_mask,
                    return_dict=True,
                    labels=targets,
                )
            loss = outputs.loss

        elif "glm" in self.model_type:
            image = samples["image"]
            img_embeds, _ = self.encode_img(image)

            if hasattr(samples, 'question_split'):  # VQA dataset
                print('VQA Batch')
                vqa_prompt = '###Human: <Img><ImageHere></Img> '
                img_embeds, _ = self.prompt_wrap(img_embeds, atts_img, vqa_prompt)
            elif self.prompt_list:
                prompt = random.choice(self.prompt_list)
                img_embeds, _ = self.prompt_wrap(img_embeds, atts_img, prompt)

            img_text = "我" * img_embeds.shape[1]

            # input_ids  labels
            input_ids_ = []
            labels_ = []
            text = [t for t in samples["text_input"]]
            tokenizer = self.llama_tokenizer
            max_source_length = 64
            max_target_length = 64
            max_seq_length = max_source_length + max_target_length
            ignore_pad_token_for_loss = True

            for answer in text:
                prompt = img_text
                a_ids = tokenizer.encode(text=prompt, add_special_tokens=False)
                b_ids = tokenizer.encode(text=answer, add_special_tokens=False)

                if len(a_ids) > max_source_length - 1:  # gmask
                    a_ids = a_ids[: max_source_length - 1]
                if len(b_ids) > max_target_length - 2:  # bos eos
                    b_ids = b_ids[: max_target_length - 2]

                input_ids = tokenizer.build_inputs_with_special_tokens(a_ids, b_ids)

                context_length = input_ids.index(tokenizer.bos_token_id)
                mask_position = context_length - 1
                labels = [-100] * context_length + input_ids[mask_position + 1:]

                pad_len = max_seq_length - len(input_ids)
                input_ids = input_ids + [tokenizer.pad_token_id] * pad_len
                labels = labels + [tokenizer.pad_token_id] * pad_len

                if ignore_pad_token_for_loss:  # True
                    labels = [(l if l != tokenizer.pad_token_id else -100) for l in labels]

                input_ids_.append(input_ids)
                labels_.append(labels)

            outputs = self.llama_model(
                input_ids=torch.tensor(input_ids_).to(image.device),
                labels=torch.tensor(labels_).to(image.device),
                my_flag=True,
                img_embeds=img_embeds
            )

            loss = outputs.loss

        return {"loss": loss}

    @classmethod
    def from_config(cls, cfg):
        vit_model = cfg.get("vit_model", "eva_clip_g")
        q_former_model = cfg.get("q_former_model",
                                 "https://storage.googleapis.com/sfr-vision-language-research/LAVIS/models/BLIP2/blip2_pretrained_flant5xxl.pth")
        img_size = cfg.get("image_size")
        num_query_token = cfg.get("num_query_token")
        llama_model = cfg.get("llama_model")
        model_type = cfg.get("model_type")  # zhyj

        drop_path_rate = cfg.get("drop_path_rate", 0)
        use_grad_checkpoint = cfg.get("use_grad_checkpoint", False)
        vit_precision = cfg.get("vit_precision", "fp16")
        freeze_vit = cfg.get("freeze_vit", True)
        freeze_qformer = cfg.get("freeze_qformer", True)
        low_resource = cfg.get("low_resource", False)
        device_8bit = cfg.get("device_8bit", 0)

        prompt_path = cfg.get("prompt_path", "")
        prompt_template = cfg.get("prompt_template", "")
        max_txt_len = cfg.get("max_txt_len", 32)
        end_sym = cfg.get("end_sym", '\n')

        model = cls(
            vit_model=vit_model,
            q_former_model=q_former_model,
            img_size=img_size,
            drop_path_rate=drop_path_rate,
            use_grad_checkpoint=use_grad_checkpoint,
            vit_precision=vit_precision,
            freeze_vit=freeze_vit,
            freeze_qformer=freeze_qformer,
            num_query_token=num_query_token,
            llama_model=llama_model,
            model_type=model_type,
            prompt_path=prompt_path,
            prompt_template=prompt_template,
            max_txt_len=max_txt_len,
            end_sym=end_sym,
            low_resource=low_resource,
            device_8bit=device_8bit,
        )

        ckpt_path = cfg.get("ckpt", "")  # load weights of MiniGPT-4
        if ckpt_path:
            print("Load BLIP2-LLM Checkpoint: {}".format(ckpt_path))
            ckpt = torch.load(ckpt_path, map_location="cpu")
            msg = model.load_state_dict(ckpt['model'], strict=False)

        return model
