import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.nn import Sequential, Linear, ReLU
from torch_geometric.nn import GINConv, global_add_pool
from torch_geometric.nn import global_mean_pool as gap, global_max_pool as gmp


class GINConvNet(torch.nn.Module):
    def __init__(self, num_features_xd=78, num_features_xt=25,
                 n_filters=32, embed_dim=128, output_dim=128, dropout=0.2):
        super(GINConvNet, self).__init__()

        self.interpret_drug_gradients = 0
        self.interpret_embedded_target_gradients = 0

        self.interpret_conv_drug_gradients = 0
        self.interpret_conv_target_gradients = 0

        self.interpret_conv_drug_activation = 0
        self.interpret_conv_target_activation = 0

        dim = 32
        self.dropout = nn.Dropout(dropout)
        self.relu = nn.ReLU()
        # convolution layers
        nn1 = Sequential(Linear(num_features_xd, dim), ReLU(), Linear(dim, dim))
        self.conv1 = GINConv(nn1)
        self.bn1 = torch.nn.BatchNorm1d(dim)

        nn2 = Sequential(Linear(dim, dim), ReLU(), Linear(dim, dim))
        self.conv2 = GINConv(nn2)
        self.bn2 = torch.nn.BatchNorm1d(dim)

        nn3 = Sequential(Linear(dim, dim), ReLU(), Linear(dim, dim))
        self.conv3 = GINConv(nn3)
        self.bn3 = torch.nn.BatchNorm1d(dim)

        nn4 = Sequential(Linear(dim, dim), ReLU(), Linear(dim, dim))
        self.conv4 = GINConv(nn4)
        self.bn4 = torch.nn.BatchNorm1d(dim)

        nn5 = Sequential(Linear(dim, dim), ReLU(), Linear(dim, dim))
        self.conv5 = GINConv(nn5)
        self.bn5 = torch.nn.BatchNorm1d(dim)

        self.fc1_xd = Linear(dim, output_dim)

        # 1D convolution on protein sequence
        self.embedding_xt = nn.Embedding(num_features_xt + 1, embed_dim)
        self.embedding_xt.weight.requires_grad = True
        self.conv_xt_1 = nn.Conv1d(in_channels=1000, out_channels=n_filters, kernel_size=8)
        self.fc1_xt = nn.Linear(32 * 121, output_dim)

        # combined layers
        self.fc1 = nn.Linear(256, 1024)
        self.fc2 = nn.Linear(1024, 256)

    def forward(self, data):

        if 'batch' in str(type(data)):
            x, edge_index, batch = data.x, data.edge_index, data.batch
        else:
            device = data[1]
            data = data[0]
            x, edge_index = data.x, data.edge_index
            batch = torch.Tensor([0]).long().to(device)
        self.interpret_drug_gradients = x
        target = data.target
        x = F.relu(self.conv1(x, edge_index))
        x = self.bn1(x)
        x = F.relu(self.conv2(x, edge_index))
        x = self.bn2(x)
        x = F.relu(self.conv3(x, edge_index))
        x = self.bn3(x)
        x = F.relu(self.conv4(x, edge_index))
        x = self.bn4(x)
        x = F.relu(self.conv5(x, edge_index))
        x = self.bn5(x)
        x = global_add_pool(x, batch)
        x = F.relu(self.fc1_xd(x))
        x = F.dropout(x, p=0.2, training=self.training)

        self.interpret_conv_drug_activation = x
        self.interpret_conv_drug_gradients = x

        embedded_xt = self.embedding_xt(target)
        embedded_xt.requires_grad = True
        self.interpret_embedded_target_gradients = embedded_xt
        conv_xt = self.conv_xt_1(embedded_xt)
        self.interpret_conv_target_activation = conv_xt
        self.interpret_embedded_target_gradients = conv_xt
        # flatten
        xt = conv_xt.view(-1, 32 * 121)
        xt = self.fc1_xt(xt)

        # concat
        xc = torch.cat((x, xt), 1)
        # add some dense layers
        xc = self.fc1(xc)
        xc = self.relu(xc)
        xc = self.dropout(xc)
        xc = self.fc2(xc)
        xc = self.relu(xc)
        xc = self.dropout(xc)
        return xc


class GINConvNet_Regression(GINConvNet):
    def __init__(self, n_output=1, num_features_xd=78, num_features_xt=25,
                 n_filters=32, embed_dim=128, output_dim=128, dropout=0.2):
        super(GINConvNet_Regression, self).__init__(num_features_xd=78, num_features_xt=25,
                                                    n_filters=32, embed_dim=128, output_dim=128, dropout=0.2)

        self.n_output = n_output
        self.out = nn.Linear(256, self.n_output)  # n_output = 1 for regression task

    def forward(self, data):
        xc = super().forward(data)
        out = self.out(xc)
        return out


class GINConvNet_Classification(GINConvNet):
    def __init__(self, n_output=2, num_features_xd=78, num_features_xt=25,
                 n_filters=32, embed_dim=128, output_dim=128, dropout=0.2):
        super(GINConvNet_Classification, self).__init__(num_features_xd=78, num_features_xt=25,
                                                    n_filters=32, embed_dim=128, output_dim=128, dropout=0.2)

        self.n_output = n_output
        self.out = nn.Linear(256, self.n_output)  # n_output = 1 for regression task

    def forward(self, data):
        xc = super().forward(data)
        out = self.out(xc)
        return out



