import os.path as osp

import torch
import torch.nn.functional as F
from torch.nn import Sequential as Seq, Linear as Lin, ReLU
from torch_geometric.datasets import ModelNet
import torch_geometric.transforms as T
from torch_geometric.data import DataLoader
from torch_geometric.nn import PointConv, fps, radius

path = osp.join(osp.dirname(osp.realpath(__file__)), '..', 'data/ModelNet10')
pre_transform, transform = T.NormalizeScale(), T.SamplePoints(1024)
train_dataset = ModelNet(path, '10', True, transform, pre_transform)
test_dataset = ModelNet(path, '10', False, transform, pre_transform)
train_loader = DataLoader(train_dataset, batch_size=32, shuffle=True)
test_loader = DataLoader(test_dataset, batch_size=32, shuffle=False)


class Net(torch.nn.Module):
    def __init__(self):
        super(Net, self).__init__()

        self.local_sa1 = PointConv(
            Seq(Lin(3, 64), ReLU(), Lin(64, 64), ReLU(), Lin(64, 128)))

        self.local_sa2 = PointConv(
            Seq(Lin(131, 128), ReLU(), Lin(128, 128), ReLU(), Lin(128, 256)))

        self.global_sa = Seq(
            Lin(259, 256), ReLU(), Lin(256, 512), ReLU(), Lin(512, 1024))

        self.lin1 = Lin(1024, 512)
        self.lin2 = Lin(512, 256)
        self.lin3 = Lin(256, 10)

    def forward(self, data):
        pos, batch = data.pos, data.batch

        idx = fps(pos, batch, ratio=0.5)  # 512 points
        edge_index = radius(pos[idx], pos, 0.1, batch[idx], batch, 48)
        x = F.relu(self.local_sa1(None, pos, edge_index))
        pos, batch = pos[idx], batch[idx]

        idx = fps(pos, batch, ratio=0.25)  # 128 points
        edge_index = radius(pos[idx], pos, 0.2, batch[idx], batch, 48)
        x = F.relu(self.local_sa2(x, pos, edge_index))
        pos, batch = pos[idx], batch[idx]

        x = self.global_sa(torch.cat([x, pos], dim=1))
        x = x.view(-1, 128, 1024).max(dim=1)[0]

        x = F.relu(self.lin1(x))
        x = F.dropout(x, p=0.5, training=self.training)
        x = F.relu(self.lin2(x))
        x = F.dropout(x, p=0.5, training=self.training)
        x = self.lin3(x)
        return F.log_softmax(x, dim=-1)


device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model = Net().to(device)
optimizer = torch.optim.Adam(model.parameters(), lr=0.001)


def train(epoch):
    model.train()

    for data in train_loader:
        data = data.to(device)
        optimizer.zero_grad()
        loss = F.nll_loss(model(data), data.y)
        loss.backward()
        optimizer.step()


def test(loader):
    model.eval()
    correct = 0

    for data in loader:
        data = data.to(device)
        with torch.no_grad():
            pred = model(data).max(1)[1]
        correct += pred.eq(data.y).sum().item()
    return correct / len(loader.dataset)


for epoch in range(1, 201):
    train(epoch)
    test_acc = test(test_loader)
    print('Epoch: {:02d}, Test: {:.4f}'.format(epoch, test_acc))
