import argparse
import torch
import numpy as np
import random
import os
import json
from tqdm import tqdm
import shortuuid

from datasets import load_dataset
from bunny.constants import IMAGE_TOKEN_INDEX, DEFAULT_IMAGE_TOKEN, DEFAULT_IM_START_TOKEN, DEFAULT_IM_END_TOKEN
from bunny.conversation import conv_templates
from bunny.model.builder import load_pretrained_model
from bunny.util.mm_utils import tokenizer_image_token, process_images, get_model_name_from_path
from torch.utils.data import Dataset

import math


def split_list(lst, n):
    """Split a list into n (roughly) equal-sized chunks"""
    chunk_size = math.ceil(lst / n)  # integer division
    return [[i,i+chunk_size-1] for i in range(0, lst, chunk_size)]


def get_chunk(lst, n, k):
    # get kth chunk out of n chunks cut from lst length
    chunks = split_list(lst, n)
    return chunks[k]


# Custom dataset class
class CustomDataset(Dataset):
    def __init__(self, args, questions, tokenizer, image_processor, image_processor_aux ,model_config):
        self.questions = questions
        self.tokenizer = tokenizer
        self.image_processor = image_processor
        self.image_processor_aux = image_processor_aux
        self.model_config = model_config
        self.question_extension = args.question_extension
        self.conv_mode = args.conv_mode

    def __getitem__(self, index):
        line = self.questions[index]
        qs = line["question"]
        keys = ["A", "B", "C", "D"]
        for i in range(len(line["options"])):
            option = line["options"][i]
            key = keys[i]
            qs += f"\n{key}. {option}"

        qs += f"\n{self.question_extension}"
        if line["image"] is not None:
            if self.model_config.mm_use_im_start_end:
                qs = DEFAULT_IM_START_TOKEN + DEFAULT_IMAGE_TOKEN + DEFAULT_IM_END_TOKEN + '\n' + qs
            else:
                qs = DEFAULT_IMAGE_TOKEN + '\n' + qs

        conv = conv_templates[self.conv_mode].copy()
        conv.append_message(conv.roles[0], qs)
        conv.append_message(conv.roles[1], None)
        prompt = conv.get_prompt()

        image_processor = self.image_processor
        image_processor_aux = self.image_processor_aux
        model_config = self.model_config

        if line["image"] is None:
            image = None
            image_size = None
            image_tensor = None
        else:
            image = line["image"].convert('RGB')
            image_size = [image.size]
            image_tensor = process_images([image], image_processor, model_config)
            if self.image_processor_aux is not None:
                image_tensor_aux = process_images([image], image_processor_aux, model_config)
            else:
                image_tensor_aux = None
        input_ids = tokenizer_image_token(prompt, self.tokenizer, IMAGE_TOKEN_INDEX, return_tensors='pt').unsqueeze(0).cuda()



        return input_ids, image_tensor, image_tensor_aux ,image_size, prompt

    def __len__(self):
        return len(self.questions)


def eval_model(args):
    torch.manual_seed(args.seed)
    np.random.seed(args.seed)
    random.seed(args.seed)
    torch.backends.cudnn.deterministic = True
    torch.backends.cudnn.benchmark = False

    # Model
    # disable_torch_init()  # DO NOT ENABLE THIS: KILLS PERFORMANCE
    model_path = os.path.expanduser(args.model_path)
    model_name = get_model_name_from_path(model_path)
    tokenizer, model, image_processor, image_processor_aux ,context_len = load_pretrained_model(model_path, args.model_base, model_name,args.model_type)

    questions = load_dataset("lmms-lab/ai2d", split="test")

    answers_file = os.path.expanduser(args.answers_file)
    if not answers_file.endswith(".jsonl"):
        raise ValueError("Answers file must be a jsonl file")

    basename = os.path.basename(answers_file)
    basename = os.path.splitext(basename)[0]
    answers_dir = os.path.dirname(answers_file)
    chunk_fname = f"{basename}_{args.chunk_idx}.jsonl"
    chunk_file = os.path.join(answers_dir, chunk_fname)
    os.makedirs(os.path.dirname(chunk_file), exist_ok=True)

    ans_file = open(chunk_file, "w")
    dataset = CustomDataset(args, questions, tokenizer, image_processor, image_processor_aux,model.config)

    idx = -1
    valid_chunk = get_chunk(len(questions), args.num_chunks, args.chunk_idx)
    print(valid_chunk)
    for line in tqdm(questions, total=len(questions)):
        idx = idx+1
        if idx<valid_chunk[0] or idx>valid_chunk[1]:
            continue

        input_ids, image_tensor, image_tensor_aux ,image_sizes, prompt = dataset[idx]
        gt_answer = line["answer"]
        input_ids = input_ids.to(device='cuda', non_blocking=True)
        input_token_len = input_ids.shape[1]
        with torch.inference_mode():
            output_ids = model.generate(
                input_ids,
                images=image_tensor.to(dtype=model.dtype, device='cuda', non_blocking=True),
                images_aux=image_tensor_aux.to(dtype=model.dtype, device='cuda', non_blocking=True) if image_tensor_aux is not None else  None ,
                do_sample=True if args.temperature > 0 else False,
                temperature=args.temperature,
                top_p=args.top_p,
                num_beams=args.num_beams,
                max_new_tokens=args.max_new_tokens,
                use_cache=True)

        # outputs = tokenizer.batch_decode(output_ids, skip_special_tokens=True)[0].strip()
        outputs = tokenizer.batch_decode(output_ids[:, input_token_len:], skip_special_tokens=True)[0].strip()

        ans_file.write(json.dumps({
            "question_id": idx,
            "prompt": prompt,
            "answer": outputs,
            "gt_answer": gt_answer,
            "text_answer": line["options"][int(gt_answer)],
            "model_id": model_name
        }) + "\n")
        ans_file.flush()
    ans_file.close()


if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("--model_path", type=str, default="liuhaotian/llava-v1.5-7b")
    parser.add_argument("--model_base", type=str, default=None)
    parser.add_argument("--model_type", type=str, default="llava")
    parser.add_argument("--answers_file", type=str, default="./answers/ai2d_answers.jsonl")
    parser.add_argument("--question_extension", type=str, default="Answer with the option's letter from the given choices directly.")
    # parser.add_argument("--question_extension", type=str, default="---\nAnswer only with the character of the correct option. Choices: {'A', 'B', 'C', 'D'}")
    parser.add_argument("--conv_mode", type=str, default="vicuna_v1")
    parser.add_argument("--num_chunks", type=int, default=1)
    parser.add_argument("--chunk_idx", type=int, default=0)
    parser.add_argument("--temperature", type=float, default=0)
    parser.add_argument("--top_p", type=float, default=None)
    parser.add_argument("--num_beams", type=int, default=1)
    parser.add_argument("--max_new_tokens", type=int, default=512)
    parser.add_argument("--seed", type=int, default=42)
    args = parser.parse_args()

    eval_model(args)