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

from base.base_net import BaseNet


class SubTer_LeNet(BaseNet):
    def __init__(self, rep_dim=1024):
        super().__init__()

        self.input_dim = (1, 32, 2048)  # Input dimension for the network
        self.rep_dim = rep_dim
        self.pool = nn.MaxPool2d(2, 2)

        self.conv1 = nn.Conv2d(1, 8, 5, bias=False, padding=2)
        self.bn1 = nn.BatchNorm2d(8, eps=1e-04, affine=False)
        self.conv2 = nn.Conv2d(8, 4, 5, bias=False, padding=2)
        self.bn2 = nn.BatchNorm2d(4, eps=1e-04, affine=False)
        self.fc1 = nn.Linear(4 * 512 * 8, self.rep_dim, bias=False)

    def forward(self, x):
        x = x.view(-1, 1, 32, 2048)
        x = self.conv1(x)
        x = self.pool(F.leaky_relu(self.bn1(x)))
        x = self.conv2(x)
        x = self.pool(F.leaky_relu(self.bn2(x)))
        x = x.view(int(x.size(0)), -1)
        x = self.fc1(x)
        return x


class SubTer_LeNet_Decoder(BaseNet):
    def __init__(self, rep_dim=1024):
        super().__init__()

        self.rep_dim = rep_dim

        # Decoder network
        self.fc3 = nn.Linear(self.rep_dim, 4 * 512 * 8, bias=False)
        self.bn3 = nn.BatchNorm2d(4, eps=1e-04, affine=False)
        self.deconv1 = nn.ConvTranspose2d(4, 8, 5, bias=False, padding=2)
        self.bn4 = nn.BatchNorm2d(8, eps=1e-04, affine=False)
        self.deconv2 = nn.ConvTranspose2d(8, 1, 5, bias=False, padding=2)

    def forward(self, x):
        x = self.fc3(x)
        x = x.view(int(x.size(0)), 4, 8, 512)
        x = F.interpolate(F.leaky_relu(self.bn3(x)), scale_factor=2)
        x = self.deconv1(x)
        x = F.interpolate(F.leaky_relu(self.bn4(x)), scale_factor=2)
        x = self.deconv2(x)
        x = torch.sigmoid(x)
        return x


class SubTer_LeNet_Autoencoder(BaseNet):
    def __init__(self, rep_dim=1024):
        super().__init__()

        self.input_dim = (1, 32, 2048)  # Input dimension for the network
        self.rep_dim = rep_dim
        self.encoder = SubTer_LeNet(rep_dim=rep_dim)
        self.decoder = SubTer_LeNet_Decoder(rep_dim=rep_dim)

    def forward(self, x):
        x = self.encoder(x)
        x = self.decoder(x)
        return x
