import os
import numpy as np
import cv2
import torch
import torch.nn.functional as tnf


def compute_bpp(prob: torch.Tensor, num_pixels: int, batch_reduction='mean'):
    """ bits per pixel

    Args:
        prob (torch.Tensor): probabilities
        num_pixels (int): number of pixels
    """
    assert isinstance(prob, torch.Tensor) and prob.dim() == 4
    p_min = prob.detach().min()
    if p_min < 0: 
        print(f'Error: prob: {prob.shape}, min={p_min} is less than 0.')
    elif p_min == 0:
        print(f'Warning: prob: {prob.shape}, min={p_min} equals to 0.')
    elif torch.isnan(p_min):
        num = torch.isnan(prob).sum()
        print(f'Error: prob: {prob.shape}, {num} of it is nan.')

    nB = prob.shape[0]
    bpp = - torch.log2(prob).sum() / num_pixels
    if batch_reduction == 'mean':
        bpp = bpp / nB
    elif batch_reduction == 'sum':
        pass
    else:
        raise ValueError()
    return bpp


def _gaussian(window_size, sigma):
    gauss = torch.Tensor(
        [np.exp(-(x - window_size//2)**2 / float(2*sigma**2)) for x in range(window_size)]
    )
    return gauss/gauss.sum()


def _create_window(window_size, sigma, channel):
    _1D_window = _gaussian(window_size, sigma).unsqueeze(1)
    _2D_window = _1D_window.mm(
        _1D_window.t()).float().unsqueeze(0).unsqueeze(0)
    window = _2D_window.expand(channel, 1, window_size, window_size).contiguous()
    return window


class MS_SSIM(torch.nn.Module):
    ''' Adapted from https://github.com/lizhengwei1992/MS_SSIM_pytorch
    '''
    def __init__(self, max_val=1.0, reduction='mean'):
        super().__init__()
        self.channel = 3
        self.max_val = max_val
        self.weight = torch.Tensor([0.0448, 0.2856, 0.3001, 0.2363, 0.1333])
        assert reduction in {'mean'}, 'Invalid reduction'
        self.reduction = reduction

    def _ssim(self, img1: torch.Tensor, img2: torch.Tensor):
        _, c, w, h = img1.size()
        window_size = min(w, h, 11)
        sigma = 1.5 * window_size / 11

        window = _create_window(window_size, sigma, self.channel)
        window = window.to(device=img1.device, dtype=img1.dtype)

        mu1 = tnf.conv2d(img1, window, padding=window_size //
                       2, groups=self.channel)
        mu2 = tnf.conv2d(img2, window, padding=window_size //
                       2, groups=self.channel)

        mu1_sq = mu1.pow(2)
        mu2_sq = mu2.pow(2)
        mu1_mu2 = mu1*mu2

        sigma1_sq = tnf.conv2d(
            img1*img1, window, padding=window_size//2, groups=self.channel) - mu1_sq
        sigma2_sq = tnf.conv2d(
            img2*img2, window, padding=window_size//2, groups=self.channel) - mu2_sq
        sigma12 = tnf.conv2d(img1*img2, window, padding=window_size //
                           2, groups=self.channel) - mu1_mu2

        C1 = (0.01*self.max_val)**2
        C2 = (0.03*self.max_val)**2
        V1 = 2.0 * sigma12 + C2
        V2 = sigma1_sq + sigma2_sq + C2
        ssim_map = ((2*mu1_mu2 + C1)*V1)/((mu1_sq + mu2_sq + C1)*V2)
        mcs_map = V1 / V2
        if self.reduction == 'mean':
            ssim_map = ssim_map.mean()
            mcs_map = mcs_map.mean()
        return ssim_map, mcs_map

    def forward(self, img1, img2):
        assert img1.shape == img2.shape and img1.device == img2.device
        self.weight = self.weight.to(device=img1.device)
        levels = 5

        if min(img1.shape[2:4]) < 2**levels:
            return torch.zeros(1)

        msssim = []
        mcs = []
        for i in range(levels):
            ssim_map, mcs_map = self._ssim(img1, img2)
            msssim.append(ssim_map)
            mcs.append(mcs_map)
            filtered_im1 = tnf.avg_pool2d(img1, kernel_size=2, stride=2)
            filtered_im2 = tnf.avg_pool2d(img2, kernel_size=2, stride=2)
            img1 = filtered_im1
            img2 = filtered_im2
        msssim = torch.stack(msssim)
        mcs = torch.stack(mcs)
        value = torch.prod(mcs[0:levels-1] ** self.weight[0:levels-1]) \
                * (msssim[levels-1] ** self.weight[levels-1])
        value: torch.Tensor
        return value


@torch.no_grad()
def get_important_channels(model, topk=16):
    from mycv.paths import IMPROC_DIR
    device = next(model.parameters()).device
    img_dir = IMPROC_DIR / 'kodak'
    img_names = os.listdir(img_dir)
    # img_names = img_names[:1]

    entropy_sum = None
    value_max = None
    for imname in img_names:
        impath = img_dir / imname
        im = cv2.imread(str(impath))
        im = cv2.cvtColor(im, cv2.COLOR_BGR2RGB)

        input_ = torch.from_numpy(im).permute(2,0,1).float() / 255.0
        input_ = input_.unsqueeze(0)
        input_ = input_.to(device=device)
        ih, iw = input_.shape[2:4]

        # encode
        outputs = model(input_)
        likelihoods = outputs['likelihoods']
        p_main = likelihoods['y']
        z = model.g_a(input_)
        zhat = torch.round(z)
        # rec = rec.clamp_(min=0, max=1)
        # rec = (rec.squeeze(0).cpu().permute(1,2,0) * 255).to(dtype=torch.uint8).numpy()
        # plt.imshow(rec); plt.show()

        # entropys
        assert p_main.shape[2:4] == (ih//16, iw//16)
        Hs = -torch.log2(p_main)
        Hs = Hs.sum(dim=3).sum(dim=2).squeeze(0).cpu()
        # plt.figure(); plt.bar(np.arange(len(Hs)), Hs)
        # plt.figure(); plt.bar(np.arange(len(Es)), Es); plt.show()

        # max values within channels
        assert zhat.shape[2:4] == (ih//16, iw//16)
        zmax = torch.amax(zhat, dim=(2,3)).squeeze(0).cpu()

        entropy_sum = entropy_sum + Hs if entropy_sum is not None else Hs
        value_max = torch.maximum(value_max, zmax) if value_max is not None else zmax

    entropy_mean = entropy_sum / len(img_names)
    # plt.bar(np.arange(len(entropy_mean)), entropy_mean); plt.show()
    entropys, indexs = torch.sort(entropy_mean, descending=True)
    indexs = indexs[:topk]
    debug = 1
    # plt.bar(np.arange(len(entropys)), entropys)
    # plt.xlabel('Rank')
    # plt.ylabel('Entropy')
    # plt.show()
    # exit()
    return indexs, value_max, zhat.shape[1]


@torch.no_grad()
def plot_response(model: torch.nn.Module, topk, save_path):
    assert not model.training
    device = next(model.parameters()).device

    if hasattr(model, 'decoder'):
        decode_func = model.decoder
    elif hasattr(model, 'g_s'):
        decode_func = model.g_s
    else:
        raise NotImplementedError()

    channel_indices, value_max, nC = get_important_channels(model, topk)

    images = []
    for k, chi in enumerate(channel_indices):
        # print(f'running channel {chi}')

        x = torch.zeros(1, nC, 3, 3, device=device)
        a = value_max[chi] * 1.2
        x[0, chi, 1, 1] = a
        rec1 = decode_func(x)
        # x[0, chi, 1, 1] = -a
        # rec2 = decode_func(x)
        # rec = torch.cat([rec1, rec2], dim=2)
        rec = rec1
        rec = rec.clamp_(min=0, max=1)
        rec = (rec.squeeze(0).cpu().permute(1,2,0) * 255).to(dtype=torch.uint8).numpy()
        # assert rec.shape == (48,48,3)
        h,w = rec.shape[0:2]
        # rec[h//2,:,:] = 255
        # resize images
        rec = cv2.resize(rec, (w*2,h*2), interpolation=cv2.INTER_NEAREST)
        rec = rec.copy()
        # label the image
        cv2.putText(rec, f'{k+1}', (4,20),
                    cv2.FONT_HERSHEY_SIMPLEX, 0.8, (255,255,255), 2)
        # cv2.putText(rec, f'Rank {k}, channel {chi}', (0,16),
        #             cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255,255,255), 1)
        # plt.imshow(rec); plt.show()
        images.append(rec)

    if len(images) == 8:
        h,w = images[0].shape[0:2]
        assert h == w
        border = (np.ones((h//16, w, 3)) * 255).astype(np.uint8)
        column1 = _insert_between(images[:4], border)
        column1 = np.concatenate(column1, axis=0)
        column2 = _insert_between(images[4:], border)
        column2 = np.concatenate(column2, axis=0)
        ch = column1.shape[0]
        middle = (np.ones((ch, w//16, 3)) * 255).astype(np.uint8)
        img = np.concatenate([column1, middle, column2], axis=1)
        # plt.imshow(img); plt.show()
        img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
        cv2.imwrite(str(save_path), img)
    else:
        # add border
        border = (np.ones((h*2, w//8, 3)) * 255).astype(np.uint8)
        to_concat = _insert_between(images, border)
        # concat and save
        img = np.concatenate(to_concat, axis=1)
        # plt.imshow(img); plt.show()
        # convert color and save
        img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
        cv2.imwrite(str(save_path), img)

def _insert_between(l, item):
    new = [[element, item] for element in l]
    new = sum(new, [])[:-1]
    return new

import os
import numpy as np
import cv2
import torch
import torch.nn.functional as tnf


def compute_bpp(prob: torch.Tensor, num_pixels: int, batch_reduction='mean'):
    """ bits per pixel

    Args:
        prob (torch.Tensor): probabilities
        num_pixels (int): number of pixels
    """
    assert isinstance(prob, torch.Tensor) and prob.dim() == 4
    p_min = prob.detach().min()
    if p_min < 0: 
        print(f'Error: prob: {prob.shape}, min={p_min} is less than 0.')
    elif p_min == 0:
        print(f'Warning: prob: {prob.shape}, min={p_min} equals to 0.')
    elif torch.isnan(p_min):
        num = torch.isnan(prob).sum()
        print(f'Error: prob: {prob.shape}, {num} of it is nan.')

    nB = prob.shape[0]
    bpp = - torch.log2(prob).sum() / num_pixels
    if batch_reduction == 'mean':
        bpp = bpp / nB
    elif batch_reduction == 'sum':
        pass
    else:
        raise ValueError()
    return bpp


def _gaussian(window_size, sigma):
    gauss = torch.Tensor(
        [np.exp(-(x - window_size//2)**2 / float(2*sigma**2)) for x in range(window_size)]
    )
    return gauss/gauss.sum()


def _create_window(window_size, sigma, channel):
    _1D_window = _gaussian(window_size, sigma).unsqueeze(1)
    _2D_window = _1D_window.mm(
        _1D_window.t()).float().unsqueeze(0).unsqueeze(0)
    window = _2D_window.expand(channel, 1, window_size, window_size).contiguous()
    return window


class MS_SSIM(torch.nn.Module):
    ''' Adapted from https://github.com/lizhengwei1992/MS_SSIM_pytorch
    '''
    def __init__(self, max_val=1.0, reduction='mean'):
        super().__init__()
        self.channel = 3
        self.max_val = max_val
        self.weight = torch.Tensor([0.0448, 0.2856, 0.3001, 0.2363, 0.1333])
        assert reduction in {'mean'}, 'Invalid reduction'
        self.reduction = reduction

    def _ssim(self, img1: torch.Tensor, img2: torch.Tensor):
        _, c, w, h = img1.size()
        window_size = min(w, h, 11)
        sigma = 1.5 * window_size / 11

        window = _create_window(window_size, sigma, self.channel)
        window = window.to(device=img1.device, dtype=img1.dtype)

        mu1 = tnf.conv2d(img1, window, padding=window_size //
                       2, groups=self.channel)
        mu2 = tnf.conv2d(img2, window, padding=window_size //
                       2, groups=self.channel)

        mu1_sq = mu1.pow(2)
        mu2_sq = mu2.pow(2)
        mu1_mu2 = mu1*mu2

        sigma1_sq = tnf.conv2d(
            img1*img1, window, padding=window_size//2, groups=self.channel) - mu1_sq
        sigma2_sq = tnf.conv2d(
            img2*img2, window, padding=window_size//2, groups=self.channel) - mu2_sq
        sigma12 = tnf.conv2d(img1*img2, window, padding=window_size //
                           2, groups=self.channel) - mu1_mu2

        C1 = (0.01*self.max_val)**2
        C2 = (0.03*self.max_val)**2
        V1 = 2.0 * sigma12 + C2
        V2 = sigma1_sq + sigma2_sq + C2
        ssim_map = ((2*mu1_mu2 + C1)*V1)/((mu1_sq + mu2_sq + C1)*V2)
        mcs_map = V1 / V2
        if self.reduction == 'mean':
            ssim_map = ssim_map.mean()
            mcs_map = mcs_map.mean()
        return ssim_map, mcs_map

    def forward(self, img1, img2):
        assert img1.shape == img2.shape and img1.device == img2.device
        self.weight = self.weight.to(device=img1.device)
        levels = 5

        if min(img1.shape[2:4]) < 2**levels:
            return torch.zeros(1)

        msssim = []
        mcs = []
        for i in range(levels):
            ssim_map, mcs_map = self._ssim(img1, img2)
            msssim.append(ssim_map)
            mcs.append(mcs_map)
            filtered_im1 = tnf.avg_pool2d(img1, kernel_size=2, stride=2)
            filtered_im2 = tnf.avg_pool2d(img2, kernel_size=2, stride=2)
            img1 = filtered_im1
            img2 = filtered_im2
        msssim = torch.stack(msssim)
        mcs = torch.stack(mcs)
        value = torch.prod(mcs[0:levels-1] ** self.weight[0:levels-1]) \
                * (msssim[levels-1] ** self.weight[levels-1])
        value: torch.Tensor
        return value


@torch.no_grad()
def get_important_channels(model, topk=16):
    from mycv.paths import IMPROC_DIR
    device = next(model.parameters()).device
    img_dir = IMPROC_DIR / 'kodak'
    img_names = os.listdir(img_dir)
    # img_names = img_names[:1]

    entropy_sum = None
    value_max = None
    for imname in img_names:
        impath = img_dir / imname
        im = cv2.imread(str(impath))
        im = cv2.cvtColor(im, cv2.COLOR_BGR2RGB)

        input_ = torch.from_numpy(im).permute(2,0,1).float() / 255.0
        input_ = input_.unsqueeze(0)
        input_ = input_.to(device=device)
        ih, iw = input_.shape[2:4]

        # encode
        outputs = model(input_)
        likelihoods = outputs['likelihoods']
        p_main = likelihoods['y']
        z = model.g_a(input_)
        zhat = torch.round(z)
        # rec = rec.clamp_(min=0, max=1)
        # rec = (rec.squeeze(0).cpu().permute(1,2,0) * 255).to(dtype=torch.uint8).numpy()
        # plt.imshow(rec); plt.show()

        # entropys
        assert p_main.shape[2:4] == (ih//16, iw//16)
        Hs = -torch.log2(p_main)
        Hs = Hs.sum(dim=3).sum(dim=2).squeeze(0).cpu()
        # plt.figure(); plt.bar(np.arange(len(Hs)), Hs)
        # plt.figure(); plt.bar(np.arange(len(Es)), Es); plt.show()

        # max values within channels
        assert zhat.shape[2:4] == (ih//16, iw//16)
        zmax = torch.amax(zhat, dim=(2,3)).squeeze(0).cpu()

        entropy_sum = entropy_sum + Hs if entropy_sum is not None else Hs
        value_max = torch.maximum(value_max, zmax) if value_max is not None else zmax

    entropy_mean = entropy_sum / len(img_names)
    # plt.bar(np.arange(len(entropy_mean)), entropy_mean); plt.show()
    entropys, indexs = torch.sort(entropy_mean, descending=True)
    indexs = indexs[:topk]
    debug = 1
    # plt.bar(np.arange(len(entropys)), entropys)
    # plt.xlabel('Rank')
    # plt.ylabel('Entropy')
    # plt.show()
    # exit()
    return indexs, value_max, zhat.shape[1]


@torch.no_grad()
def plot_response(model: torch.nn.Module, topk, save_path):
    assert not model.training
    device = next(model.parameters()).device

    if hasattr(model, 'decoder'):
        decode_func = model.decoder
    elif hasattr(model, 'g_s'):
        decode_func = model.g_s
    else:
        raise NotImplementedError()

    channel_indices, value_max, nC = get_important_channels(model, topk)

    images = []
    for k, chi in enumerate(channel_indices):
        # print(f'running channel {chi}')

        x = torch.zeros(1, nC, 3, 3, device=device)
        a = value_max[chi] * 1.2
        x[0, chi, 1, 1] = a
        rec1 = decode_func(x)
        # x[0, chi, 1, 1] = -a
        # rec2 = decode_func(x)
        # rec = torch.cat([rec1, rec2], dim=2)
        rec = rec1
        rec = rec.clamp_(min=0, max=1)
        rec = (rec.squeeze(0).cpu().permute(1,2,0) * 255).to(dtype=torch.uint8).numpy()
        # assert rec.shape == (48,48,3)
        h,w = rec.shape[0:2]
        # rec[h//2,:,:] = 255
        # resize images
        rec = cv2.resize(rec, (w*2,h*2), interpolation=cv2.INTER_NEAREST)
        rec = rec.copy()
        # label the image
        cv2.putText(rec, f'{k+1}', (4,20),
                    cv2.FONT_HERSHEY_SIMPLEX, 0.8, (255,255,255), 2)
        # cv2.putText(rec, f'Rank {k}, channel {chi}', (0,16),
        #             cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255,255,255), 1)
        # plt.imshow(rec); plt.show()
        images.append(rec)

    if len(images) == 8:
        h,w = images[0].shape[0:2]
        assert h == w
        border = (np.ones((h//16, w, 3)) * 255).astype(np.uint8)
        column1 = _insert_between(images[:4], border)
        column1 = np.concatenate(column1, axis=0)
        column2 = _insert_between(images[4:], border)
        column2 = np.concatenate(column2, axis=0)
        ch = column1.shape[0]
        middle = (np.ones((ch, w//16, 3)) * 255).astype(np.uint8)
        img = np.concatenate([column1, middle, column2], axis=1)
        # plt.imshow(img); plt.show()
        img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
        cv2.imwrite(str(save_path), img)
    else:
        # add border
        border = (np.ones((h*2, w//8, 3)) * 255).astype(np.uint8)
        to_concat = _insert_between(images, border)
        # concat and save
        img = np.concatenate(to_concat, axis=1)
        # plt.imshow(img); plt.show()
        # convert color and save
        img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
        cv2.imwrite(str(save_path), img)

def _insert_between(l, item):
    new = [[element, item] for element in l]
    new = sum(new, [])[:-1]
    return new

bits per pixel      Args:         prob (torch.Tensor): probabilities         num_pixels (int): number of pixels Adapted from https://github.com/lizhengwei1992/MS_SSIM_pytorch # img_names = img_names[:1] # encode # rec = rec.clamp_(min=0, max=1) # rec = (rec.squeeze(0).cpu().permute(1,2,0) * 255).to(dtype=torch.uint8).numpy() # plt.imshow(rec); plt.show() # entropys # plt.figure(); plt.bar(np.arange(len(Hs)), Hs) # plt.figure(); plt.bar(np.arange(len(Es)), Es); plt.show() # max values within channels # plt.bar(np.arange(len(entropy_mean)), entropy_mean); plt.show() # plt.bar(np.arange(len(entropys)), entropys) # plt.xlabel('Rank') # plt.ylabel('Entropy') # plt.show() # exit() # print(f'running channel {chi}') # x[0, chi, 1, 1] = -a # rec2 = decode_func(x) # rec = torch.cat([rec1, rec2], dim=2) # assert rec.shape == (48,48,3) # rec[h//2,:,:] = 255 # resize images # label the image # cv2.putText(rec, f'Rank {k}, channel {chi}', (0,16), #             cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255,255,255), 1) # plt.imshow(rec); plt.show() # plt.imshow(img); plt.show() # add border # concat and save # plt.imshow(img); plt.show() # convert color and save