#using SymmetrySnap
using Test
include("../src/SymmetrySnap.jl")

@testset "SymmetrySnap.jl" begin
    # Make sure that we get the same number of lattice points no matter what
    @test length(SymmetrySnap.getLatticePoints([1.234324,3.654,6.213], [5.12436,4.712346,9.2778], [1.23436346,2.678323,9.7])) == length(SymmetrySnap.getLatticePoints([2.435,6.54,6.213], [5.12436,7.12346,2.778], [2.3436346,6.78323,97]))

    # Test cubic symmetry, we should get 48 symmetry operations
    a = [1,2,3]
    b = sqrt(14/3)*[-1,-1,1]
    c = [5,-4,1]/sqrt(3)
    newLattice = Array{Float64, 2}(undef, 3, 3)
    newLattice[1:end, 1] = a
    newLattice[1:end, 2] = b
    newLattice[1:end, 3] = c
    inverse = inv(newLattice)
    @test length(SymmetrySnap.getSymmetryOps(a,b,c,inverse)) == 48

    # Test orthorhombic symmetry, we should get 8 symmetry operations
    a = [1,2,3]
    b = [-1,-1,1]
    c = [5,-4,1]
    newLattice = Array{Float64, 2}(undef, 3, 3)
    newLattice[1:end, 1] = a
    newLattice[1:end, 2] = b
    newLattice[1:end, 3] = c
    inverse = inv(newLattice)
    @test length(SymmetrySnap.getSymmetryOps(a,b,c,inverse)) == 8

    # Test hexagonal symmetry, we should get 24 symmetry operations
    a = [1,0,0]
    b = [-1/2,sqrt(3)/2,0]
    c = [0,0,1.212624]
    newLattice = Array{Float64, 2}(undef, 3, 3)
    newLattice[1:end, 1] = a
    newLattice[1:end, 2] = b
    newLattice[1:end, 3] = c
    inverse = inv(newLattice)
    @test length(SymmetrySnap.getSymmetryOps(a,b,c,inverse)) == 24

    # Make sure we can map atoms to a cell, we should get [0.478362, 0.76541, 0.0030137]
    a = [1,0,0]
    b = [0,1,0]
    c = [0,0,1]
    newLattice = Array{Float64, 2}(undef, 3, 3)
    newLattice[1:end, 1] = a
    newLattice[1:end, 2] = b
    newLattice[1:end, 3] = c
    inverse = inv(newLattice)
    atomPosition = [113.478362, -2093.234590, 1.0030137]
    @test isapprox(SymmetrySnap.mapAtomsToCell(a, b, c, inverse, [atomPosition]), [[0.478362, 0.76541, 0.0030137]])
    """
    a = [1,0,0]
    b = [0,1,0]
    c = [0,0,1]
    newLattice = Array{Float64, 2}(undef, 3, 3)
    newLattice[1:end, 1] = a
    newLattice[1:end, 2] = b
    newLattice[1:end, 3] = c
    inverse = inv(newLattice)
    atom1 = [0.25,0.25,0.25]
    atom2 = [0.75,0.25,0.25]
    operator = [-1 0 0; 0 1 0; 0 0 1]
    @test isapprox(SymmetrySnap.testAtom(a, b, c, inverse, operator, atom1, 1, atom2, 2, 2), [-1 0 0 -1 0 0 -1; 0 1 0 0 -1 0 0; 0 0 1 0 0 -1 0], [0,0,0])
    """
    a = [1,0,0]
    b = [0,1,0]
    c = [0,0,1]
    atomPos = [[0.25001,0.25003,0.24998], [0.2499992,0.25,0.751], [0.2519203,0.749210,0.25777], [0.751359,0.252468,0.248642], [0.75,0.74867530999,0.2525600], [0.753141592653,0.2510293,0.74987654321], [0.2500012387593,0.75130958,0.75], [0.74989132,0.750019284,0.751837592985409287374987810293892834750917823984723894574012983975]]
    newPos = vcat(atomPos...)
    solutionSpace = [0.25,0.25,0.25, 0.25,0.25,0.75, 0.25,0.75,0.25, 0.75,0.25,0.25, 0.75,0.75,0.25, 0.75,0.25,0.75, 0.25,0.75,0.75, 0.75,0.75,0.75]
    answer = SymmetrySnap.proj(solutionSpace, newPos)
    display(answer)
    display(SymmetrySnap.symmetrize(a, b, c, atomPos, [8]))
end
