import torch
import torch.nn as nn
import torch.nn.functional as F
from models.unet import UNet
import numpy as np
from torch.autograd import Variable
import matplotlib.pyplot as plt
class ConsModule(nn.Module):
        def __init__(self, nlabel, mindepth):
                super(ConsModule, self).__init__()
                self.cons_net = UNet(6, 4)
                self.nlabel = nlabel
                self.mindepth = mindepth
        def init_weights(self):
                for m in self.modules():
                        if isinstance(m, nn.Conv2d) or isinstance(m, nn.ConvTranspose2d) or isinstance(m, nn.Linear) or isinstance(m, nn.Conv3d):
                                nn.init.xavier_uniform_(m.weight.data)
                                if m.bias is not None:
                                        m.bias.data.zero_()
                        elif isinstance(m, nn.BatchNorm2d) or isinstance(m, nn.BatchNorm3d):
                                m.weight.data.fill_(1)
                                m.bias.data.zero_()

        def forward(self, depth, nmap, cons_loss):


                b,_,h,w = depth.size()
                if nmap.size(3) == 3:
                        nmap = nmap.permute(0,3,1,2)

                residual = torch.cat((depth, nmap), dim=1)
                input_features = torch.cat((depth, nmap, cons_loss), dim = 1) # [1, 6, H, W]

                output = self.cons_net(input_features) + residual
                depth = output[:,0].unsqueeze(1)
                nmap = F.normalize(output[:,1:], dim = 1)
                return torch.cat((depth, nmap), dim = 1) # [1, 4, H, W]
                

class ConsLoss(nn.Module):
        def __init__(self):
                super(ConsLoss, self).__init__()
                self.sobel_kernel = None

        def get_grad_1(self, depth):
                if self.sobel_kernel is None:
                        edge_kernel_x = torch.from_numpy(np.array([[1/8, 0, -1/8],[1/4,0,-1/4],[1/8,0,-1/8]])).type_as(depth)
                        edge_kernel_y = torch.from_numpy(np.array([[1/8, 1/4, 1/8],[0,0,0],[-1/8,-1/4,-1/8]])).type_as(depth)
                        self.sobel_kernel = torch.cat((edge_kernel_x.view(1,1,3,3), edge_kernel_y.view(1,1,3,3)), dim = 0)
                        self.sobel_kernel.requires_grad = False
                grad_depth = torch.nn.functional.conv2d(depth, self.sobel_kernel, padding = 1)
                
                return -1*grad_depth


        def get_grad_2(self, depth, nmap, intrinsics_var):                
                p_b,_,p_h,p_w = depth.size()
                c_x = p_w/2
                c_y = p_h/2
                p_y = torch.arange(0, p_h).view(1, p_h, 1).expand(p_b,p_h,p_w).type_as(depth) - c_y
                p_x = torch.arange(0, p_w).view(1, 1, p_w).expand(p_b,p_h,p_w).type_as(depth) - c_x

                nmap_z = nmap[:,2,:,:]
                nmap_z_mask = (nmap_z == 0)
                nmap_z[nmap_z_mask] = 1e-10
                nmap[:,2,:,:] = nmap_z
                n_grad = nmap[:,:2,:,:].clone()
                n_grad = n_grad/ (nmap[:,2,:,:].unsqueeze(1))

                grad_depth = -n_grad.clone()*depth.clone()
                
                fx = intrinsics_var[:,0,0].clone().view(-1,1,1)
                fy = intrinsics_var[:,1,1].clone().view(-1,1,1)
                f = torch.cat((fx.unsqueeze(1),fy.unsqueeze(1)), dim = 1)
                
                grad_depth = grad_depth/f
                
                denom = (1 + p_x*(n_grad[:,0,:,:])/fx + p_y*(n_grad[:,1,:,:])/fy )
                denom[denom == 0] = 1e-10
                grad_depth = grad_depth/denom.view(p_b,1,p_h,p_w)
                
                return grad_depth

        def forward(self, depth, nmap, intrinsics_var):

                grad_depth_1 = self.get_grad_1(depth)*100
                grad_depth_2 = self.get_grad_2(depth, nmap.clone(), intrinsics_var)*100

                grad_depth_1_np = grad_depth_1 #B,2,H,W
                grad_depth_1_np_min,_ = torch.min(grad_depth_1_np.reshape(grad_depth_1_np.size()[0],grad_depth_1_np.size()[1],-1),dim=2)
                grad_depth_1_np_min = grad_depth_1_np_min.view(grad_depth_1_np.size()[0],grad_depth_1_np.size()[1],1,1)
                grad_depth_1_np -= grad_depth_1_np_min
                grad_depth_1_np_max,_ = torch.max(grad_depth_1_np.reshape(grad_depth_1_np.size()[0],grad_depth_1_np.size()[1],-1),dim=2)
                grad_depth_1_np_max = grad_depth_1_np_max.view(grad_depth_1_np.size()[0],grad_depth_1_np.size()[1],1,1)
                grad_depth_1_np /= grad_depth_1_np_max - grad_depth_1_np_min

                grad_depth_2_np = grad_depth_2 #B,2,H,W
                grad_depth_2_np_min,_ = torch.min(grad_depth_2_np.reshape(grad_depth_2_np.size()[0],grad_depth_2_np.size()[1],-1),dim=2)
                grad_depth_2_np_min = grad_depth_2_np_min.view(grad_depth_2_np.size()[0],grad_depth_2_np.size()[1],1,1)
                grad_depth_2_np -= grad_depth_2_np_min
                grad_depth_2_np_max,_ = torch.max(grad_depth_2_np.reshape(grad_depth_2_np.size()[0],grad_depth_2_np.size()[1],-1),dim=2)
                grad_depth_2_np_max = grad_depth_2_np_max.view(grad_depth_2_np.size()[0],grad_depth_2_np.size()[1],1,1)
                grad_depth_2_np /= grad_depth_2_np_max - grad_depth_2_np_min

                cons_loss_input = torch.sub(grad_depth_1_np, grad_depth_2_np)
                cons_loss_input_min,_ = torch.min(cons_loss_input.reshape(cons_loss_input.size()[0],cons_loss_input.size()[1],-1),dim=2)
                cons_loss_input_min = cons_loss_input_min.view(cons_loss_input.size()[0],cons_loss_input.size()[1],1,1)
                cons_loss_input -= cons_loss_input_min
                cons_loss_input_max,_ = torch.max(cons_loss_input.reshape(cons_loss_input.size()[0],cons_loss_input.size()[1],-1),dim=2)
                cons_loss_input_max = cons_loss_input_max.view(cons_loss_input.size()[0],cons_loss_input.size()[1],1,1)
                cons_loss_input /= cons_loss_input_max - cons_loss_input_min

                return cons_loss_input
