import torch
from torch import nn
from transformers import LayoutLMConfig, LayoutLMModel, LayoutLMTokenizer, LayoutLMv2FeatureExtractor
from transformers import LayoutLMv2Config, LayoutLMv2Model
from transformers import LayoutXLMTokenizer
from transformers import XLMRobertaConfig, AutoTokenizer, XLMRobertaModel


from model.relation_extractor import RelationExtractor
from utils import load_checkpoint


class DocumentKVUModel(nn.Module):
    def __init__(self, cfg):
        super().__init__()

        self.model_cfg = cfg.model
        self.freeze = cfg.train.freeze
        self.train_cfg = cfg.train
        
        self._get_backbones(self.model_cfg.backbone)
        # if 'pth' in self.model_cfg.ckpt_model_file:
        #     self.backbone = load_checkpoint(self.model_cfg.ckpt_model_file, self.backbone, 'backbone')

        self._create_head()
        
        self.loss_func = nn.CrossEntropyLoss()

    def _create_head(self):
        self.backbone_hidden_size = self.backbone_config.hidden_size
        self.head_hidden_size = self.model_cfg.head_hidden_size
        self.head_p_dropout = self.model_cfg.head_p_dropout
        self.n_classes = self.model_cfg.n_classes + 1
        self.relations = self.model_cfg.n_relations
        # self.repr_hiddent_size = self.backbone_hidden_size + self.n_classes + (self.train_cfg.max_seq_length + 1) * 3
        self.repr_hiddent_size = self.backbone_hidden_size

        # (1) Initial token classification
        self.itc_layer = nn.Sequential(
            nn.Dropout(self.head_p_dropout),
            nn.Linear(self.backbone_hidden_size, self.backbone_hidden_size),
            nn.Dropout(self.head_p_dropout),
            nn.Linear(self.backbone_hidden_size, self.n_classes),
        )
        # (2) Subsequent token classification
        self.stc_layer = RelationExtractor(
            n_relations=1, #1
            backbone_hidden_size=self.backbone_hidden_size,
            head_hidden_size=self.head_hidden_size,
            head_p_dropout=self.head_p_dropout,
        )
        
        # (3) Linking token classification
        self.relation_layer = RelationExtractor(
            n_relations=self.relations, #1
            backbone_hidden_size=self.backbone_hidden_size,
            head_hidden_size=self.head_hidden_size,
            head_p_dropout=self.head_p_dropout,
        )
        
        # (4) Linking token classification
        self.relation_layer_from_key = RelationExtractor(
            n_relations=self.relations, #1
            backbone_hidden_size=self.backbone_hidden_size,
            head_hidden_size=self.head_hidden_size,
            head_p_dropout=self.head_p_dropout,
        )

        # Classfication Layer for whole document
        # (1) Initial token classification
        self.itc_layer_document = nn.Sequential(
            nn.Dropout(self.head_p_dropout),
            nn.Linear(self.repr_hiddent_size, self.repr_hiddent_size),
            nn.Dropout(self.head_p_dropout),
            nn.Linear(self.repr_hiddent_size, self.n_classes),
        )
        # (2) Subsequent token classification
        self.stc_layer_document = RelationExtractor(
            n_relations=1,
            backbone_hidden_size=self.repr_hiddent_size,
            head_hidden_size=self.head_hidden_size,
            head_p_dropout=self.head_p_dropout,
        )
        # (3) Linking token classification
        self.relation_layer_document = RelationExtractor(
            n_relations=self.relations,
            backbone_hidden_size=self.repr_hiddent_size,
            head_hidden_size=self.head_hidden_size,
            head_p_dropout=self.head_p_dropout,
        )
        # (4) Linking token classification
        self.relation_layer_from_key_document = RelationExtractor(
            n_relations=self.relations,
            backbone_hidden_size=self.repr_hiddent_size,
            head_hidden_size=self.head_hidden_size,
            head_p_dropout=self.head_p_dropout,
        )
        
        self.itc_layer.apply(self._init_weight)
        self.stc_layer.apply(self._init_weight)
        self.relation_layer.apply(self._init_weight)
        self.relation_layer_from_key.apply(self._init_weight)

        self.itc_layer_document.apply(self._init_weight)
        self.stc_layer_document.apply(self._init_weight)
        self.relation_layer_document.apply(self._init_weight)
        self.relation_layer_from_key_document.apply(self._init_weight)


    def _get_backbones(self, config_type):
        configs = {
            'layoutlm': {'config': LayoutLMConfig, 'tokenizer': LayoutLMTokenizer, 'backbone': LayoutLMModel, 'feature_extrator': LayoutLMv2FeatureExtractor},
            'layoutxlm': {'config': LayoutLMv2Config, 'tokenizer': LayoutXLMTokenizer, 'backbone': LayoutLMv2Model, 'feature_extrator': LayoutLMv2FeatureExtractor},
            'xlm-roberta': {'config': XLMRobertaConfig, 'tokenizer': AutoTokenizer, 'backbone': XLMRobertaModel, 'feature_extrator': LayoutLMv2FeatureExtractor},
        }
        
        self.backbone_config = configs[config_type]['config'].from_pretrained(self.model_cfg.pretrained_model_path)
        if config_type != 'xlm-roberta':
            self.tokenizer = configs[config_type]['tokenizer'].from_pretrained(self.model_cfg.pretrained_model_path)
        else:
            self.tokenizer = configs[config_type]['tokenizer'].from_pretrained(self.model_cfg.pretrained_model_path, use_fast=False)
        self.feature_extractor = configs[config_type]['feature_extrator'](apply_ocr=False)
        self.backbone = configs[config_type]['backbone'].from_pretrained(self.model_cfg.pretrained_model_path)        

    @staticmethod
    def _init_weight(module):
        init_std = 0.02
        if isinstance(module, nn.Linear):
            nn.init.normal_(module.weight, 0.0, init_std)
            if module.bias is not None:
                nn.init.constant_(module.bias, 0.0)
        elif isinstance(module, nn.LayerNorm):
            nn.init.normal_(module.weight, 1.0, init_std)
            if module.bias is not None:
                nn.init.constant_(module.bias, 0.0)

    def forward(self, batches):
        head_outputs_list = []
        loss = 0.0
        for batch in batches["windows"]:
            image = batch["image"]
            input_ids = batch["input_ids"]
            bbox = batch["bbox"]
            attention_mask = batch["attention_mask"]
        
            if self.freeze:
                for param in self.backbone.parameters():
                    param.requires_grad = False
                    
            if self.model_cfg.backbone == 'layoutxlm':
                backbone_outputs = self.backbone(
                    image=image, input_ids=input_ids, bbox=bbox, attention_mask=attention_mask
                )
            else:
                backbone_outputs = self.backbone(input_ids, attention_mask=attention_mask)
            
            last_hidden_states = backbone_outputs.last_hidden_state[:, :512, :]            
            last_hidden_states = last_hidden_states.transpose(0, 1).contiguous()

            itc_outputs = self.itc_layer(last_hidden_states).transpose(0, 1).contiguous()
            stc_outputs = self.stc_layer(last_hidden_states, last_hidden_states).squeeze(0)
            el_outputs = self.relation_layer(last_hidden_states, last_hidden_states).squeeze(0)
            el_outputs_from_key = self.relation_layer_from_key(last_hidden_states, last_hidden_states).squeeze(0)

            window_repr = last_hidden_states.transpose(0, 1).contiguous()

            head_outputs = {"window_repr": window_repr,
                            "itc_outputs": itc_outputs,
                            "stc_outputs": stc_outputs,
                            "el_outputs": el_outputs,
                            "el_outputs_from_key": el_outputs_from_key}

            if any(['labels' in key for key in batch.keys()]):
                loss += self._get_loss(head_outputs, batch)
            
            head_outputs_list.append(head_outputs)

        batch = batches["documents"]
        
        document_repr = torch.cat([w['window_repr'] for w in head_outputs_list], dim=1)
        document_repr = document_repr.transpose(0, 1).contiguous()

        itc_outputs = self.itc_layer_document(document_repr).transpose(0, 1).contiguous()
        stc_outputs = self.stc_layer_document(document_repr, document_repr).squeeze(0)
        el_outputs = self.relation_layer_document(document_repr, document_repr).squeeze(0)
        el_outputs_from_key = self.relation_layer_from_key_document(document_repr, document_repr).squeeze(0)

        head_outputs = {"itc_outputs": itc_outputs,
                        "stc_outputs": stc_outputs,
                        "el_outputs": el_outputs,
                        "el_outputs_from_key": el_outputs_from_key}
        
        if any(['labels' in key for key in batch.keys()]):
            loss += self._get_loss(head_outputs, batch)

        return head_outputs, loss

    def _get_loss(self, head_outputs, batch):
        itc_outputs = head_outputs["itc_outputs"]
        stc_outputs = head_outputs["stc_outputs"]
        el_outputs = head_outputs["el_outputs"]
        el_outputs_from_key = head_outputs["el_outputs_from_key"]

        itc_loss = self._get_itc_loss(itc_outputs, batch)
        stc_loss = self._get_stc_loss(stc_outputs, batch)
        el_loss = self._get_el_loss(el_outputs, batch)
        el_loss_from_key = self._get_el_loss(el_outputs_from_key, batch, from_key=True)

        loss = itc_loss + stc_loss + el_loss + el_loss_from_key

        return loss

    def _get_itc_loss(self, itc_outputs, batch):
        itc_mask = batch["are_box_first_tokens"].view(-1)

        itc_logits = itc_outputs.view(-1, self.model_cfg.n_classes + 1)
        itc_logits = itc_logits[itc_mask]

        itc_labels = batch["itc_labels"].view(-1)
        itc_labels = itc_labels[itc_mask]
        
        itc_loss = self.loss_func(itc_logits, itc_labels)

        return itc_loss

    def _get_stc_loss(self, stc_outputs, batch):
        inv_attention_mask = 1 - batch["attention_mask"]

        bsz, max_seq_length = inv_attention_mask.shape
        device = inv_attention_mask.device

        invalid_token_mask = torch.cat(
            [inv_attention_mask, torch.zeros([bsz, 1]).to(device)], axis=1
        ).bool()
        stc_outputs.masked_fill_(invalid_token_mask[:, None, :], -10000.0)

        self_token_mask = (
            torch.eye(max_seq_length, max_seq_length + 1).to(device).bool()
        )
        stc_outputs.masked_fill_(self_token_mask[None, :, :], -10000.0)

        stc_mask = batch["attention_mask"].view(-1).bool()

        stc_logits = stc_outputs.view(-1, max_seq_length + 1)
        stc_logits = stc_logits[stc_mask]

        stc_labels = batch["stc_labels"].view(-1)
        stc_labels = stc_labels[stc_mask]

        stc_loss = self.loss_func(stc_logits, stc_labels)

        return stc_loss
    
    def _get_el_loss(self, el_outputs, batch, from_key=False):
        bsz, max_seq_length = batch["attention_mask"].shape
        device = batch["attention_mask"].device

        self_token_mask = (
            torch.eye(max_seq_length, max_seq_length + 1).to(device).bool()
        )

        box_first_token_mask = torch.cat(
            [
                (batch["are_box_first_tokens"] == False),
                torch.zeros([bsz, 1], dtype=torch.bool).to(device),
            ],
            axis=1,
        )
        el_outputs.masked_fill_(box_first_token_mask[:, None, :], -10000.0)
        el_outputs.masked_fill_(self_token_mask[None, :, :], -10000.0)

        mask = batch["are_box_first_tokens"].view(-1)
    
        logits = el_outputs.view(-1, max_seq_length + 1)
        logits = logits[mask]

        if from_key:
            el_labels = batch["el_labels_from_key"]
        else:
            el_labels = batch["el_labels"]
        labels = el_labels.view(-1)
        labels = labels[mask]
        
        loss = self.loss_func(logits, labels)
        return loss
