'''
DDS + ControlNet
(I have a little of comments here. Please check out try_dds_controlnet_diff_init.py for more comments)
'''

from PIL import Image
import cv2
import os
import warnings
from pathlib import Path
import json

from transformers import CLIPTextModel, CLIPTokenizer
from diffusers import (
    AutoencoderKL, 
    UNet2DConditionModel, 
    UniPCMultistepScheduler,
    DDIMScheduler,
)

import torch
import torch.nn as nn
import torch.nn.functional as F

from functools import partial
from tqdm.auto import tqdm
import math
import sys
from d3dr.diffusion.sd_utils_controlnet import SDControlNet

import matplotlib.pyplot as plt
import numpy as np
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--prompt_1", type=str, default="", help="desired prompt")
parser.add_argument("--prompt_2", type=str, default="", help="initial prompt")
parser.add_argument("--exp_desc", type=str, default="")
parser.add_argument("--seed", type=int, default=0)
parser.add_argument("--sd_model_name", type=str, default="stabilityai/stable-diffusion-2-1-base")
parser.add_argument("--controlnet_model_name", type=str, default="thibaud/controlnet-sd21-depth-diffusers")
parser.add_argument("--guidance_scale", type=float, default=10.0)
parser.add_argument("--width", type=int, default=512)
parser.add_argument("--height", type=int, default=512)
parser.add_argument("--controlnet_image_path", type=str, default=None)
parser.add_argument("--controlnet_conditioning_scale", type=float, default=1.0)

parser.add_argument("--image_path", type=str, default=None)
parser.add_argument("--mask_path", type=str, default=None)

parser.add_argument("--num_train_iterations", type=int, default=200)
parser.add_argument("--show_iter", type=int, default=20)
parser.add_argument("--acc_step", type=int, default=1)
parser.add_argument("--lr", type=float, default=1e-1)
parser.add_argument("--power", type=float, default=0.5)
parser.add_argument("--save_dir", type=str, default="./dds_harmonization_play_output/")

parser.add_argument("--exp", type=str, default="optimize_alpha", choices=["optimize_alpha", "implicit_dds", "explicit_dds"])
parser.add_argument("--add_random_noise_mask", action="store_true")
parser.add_argument("--mask_grad", type=int, default=0)
parser.add_argument("--use_random_noise", type=int, default=1)
parser.add_argument("--use_step_ratio", type=int, default=0)
parser.add_argument("--initial_step", type=float, default=0.0)
parser.add_argument("--fp16", action="store_true")

args = parser.parse_args()

torch_device = "cuda"

guidance = SDControlNet(
    device="cuda",
    sd_version=args.sd_model_name,
    controlnet_name=args.controlnet_model_name,
    height=args.height,
    width=args.width,
)

def center_crop_to_square(cv2_image):
    h, w = cv2_image.shape[:2]
    if h > w:
        start = (h - w) // 2
        return cv2_image[start: h - start, ...]
    elif w > h:
        start = (w - h) // 2
        return cv2_image[:, start: w - start, ...]
    else:
        return cv2_image

init_image = cv2.imread(args.image_path)
init_image = cv2.cvtColor(init_image, cv2.COLOR_BGR2RGB)
init_image = center_crop_to_square(init_image)
cv2.imwrite("cropped_image.jpg", cv2.cvtColor(init_image, cv2.COLOR_RGB2BGR))
init_image = cv2.resize(init_image, (args.height, args.width))[None, ...]
with torch.no_grad():
    init_image_torch = guidance.np2torch(init_image)
    init_latent = guidance.torch2latents(init_image_torch)

init_mask = (cv2.imread(args.mask_path, cv2.IMREAD_GRAYSCALE))
init_mask = center_crop_to_square(init_mask)
init_mask = (cv2.resize(init_mask, (args.height, args.width))[None, None, ...] / 255.0 > 0.95).astype(np.float32)
with torch.no_grad():
    mask = torch.from_numpy(init_mask).to(device=torch_device, dtype=guidance.precision_t)
    mask_small = F.interpolate(mask, size=(args.height // 8, args.width // 8), mode="bilinear", align_corners=False)

controlnet_image = cv2.imread(args.controlnet_image_path)
controlnet_image = center_crop_to_square(controlnet_image) # depth in my case
controlnet_image = cv2.resize(controlnet_image, (args.height, args.width)) / 255.0
controlnet_image_emb = guidance.get_image_embeds(controlnet_image).to(torch_device)

torch.manual_seed(args.seed)

sds_image = init_latent.detach().clone().requires_grad_(True)
if args.add_random_noise_mask:
    with torch.no_grad():
        # sds_image.data = sds_image.data * (1.0 - mask_small) + torch.randn_like(sds_image) * mask_small
        sds_image.data = guidance.torch2latents(init_image_torch * (1.0 - mask) + torch.randn_like(init_image_torch) * mask)

optimizer = torch.optim.SGD([sds_image], lr=args.lr)
opt_scheduler = torch.optim.lr_scheduler.PolynomialLR(
    optimizer, 
    total_iters=args.num_train_iterations // args.acc_step, 
    power=args.power
)

args.save_dir = guidance.get_save_dir(args.save_dir)
print("Save dir:", args.save_dir)

with open(Path(args.save_dir) / "args.json", "w") as f:
    json.dump(vars(args), f, indent=4)

prompt_desired = guidance.get_text_embeds(args.prompt_1)
prompt_initial = guidance.get_text_embeds(args.prompt_2)
uncond_emb = guidance.get_text_embeds("")
text_embeddings_desired=torch.cat([uncond_emb, prompt_desired])
text_embeddings_initial=torch.cat([uncond_emb, prompt_initial]) 

step_ratio = None
noise = None
if args.use_random_noise == 0: # use random noise
    noise = torch.randn_like(init_latent)

for i in tqdm(range(args.num_train_iterations)):
    # zero_grad
    optimizer.zero_grad()
    if args.use_step_ratio != 0:
        step_ratio = min(1, (1 - args.initial_step) + args.initial_step * i / args.num_train_iterations)

    loss = guidance.train_step_dds(
        text_embeddings_initial=text_embeddings_initial, 
        text_embeddings_desired=text_embeddings_desired, 
        image_embeddings_initial=controlnet_image_emb, 
        image_embeddings_desired=controlnet_image_emb, # they are the same
        latents_initial=init_latent, 
        rgb_pred=sds_image, 
        guidance_scale=args.guidance_scale,
        as_latent=True, 
        step_ratio=step_ratio,
        noise=noise,
        use_weights=False,
    )

    loss.backward()

    if args.mask_grad != 0:
        sds_image.grad = sds_image.grad * mask_small

    optimizer.step()
    opt_scheduler.step()

    if (i + 1) % args.show_iter == 0:
        result_image = guidance.torch2np(guidance.latents2torch(sds_image))
        # with torch.no_grad():
        #     alpha_image_big = F.interpolate(alpha_image, size=(args.height, args.width), mode="bilinear", align_corners=False)
        #     alpha_image_np = guidance.torch2np(alpha_image_big * 2.0 - 1.0)
        guidance.save_images(
            images=result_image, 
            save_name=f"dds_image_{i + 1}.png", 
            prompt=f"{args.prompt_2} -> {args.prompt_1}",
            save_dir=args.save_dir,
            exp_desc=args.exp_desc,
        )
        # guidance.save_images(
        #     images=alpha_image_np, 
        #     save_name=f"alpha_image_{i + 1}.png", 
        #     prompt=args.prompt,
        #     save_dir=args.save_dir,
        #     exp_desc=args.exp_desc,
        # )

