using Test
@testset "Tests for triangulation" begin
# import Triangle
@testset "Test function is_left_of" begin
    points = [
         0  0
         1  0
         1  1
         1 -1
        -1  1
        -1 -1
    ]
    @test is_left_of(points, 1, 2, 3) == true
    @test is_left_of(points, 1, 2, 4) == false
    @test is_left_of(points, 1, 2, 5) == true
    @test is_left_of(points, 1, 2, 6) == false
end
@testset "Test function is_right_of" begin
    points = [
         0  0
         1  0
         1  1
         1 -1
        -1  1
        -1 -1
    ]
    @test is_right_of(points, 1, 2, 3) == false
    @test is_right_of(points, 1, 2, 4) == true
    @test is_right_of(points, 1, 2, 5) == false
    @test is_right_of(points, 1, 2, 6) == true
end

@testset "Test of Triangle constructors" begin
    points = [
         0  0
         1  0
         1  1
    ]
    @testset "For two vertices" begin
        t = Triangle(1, 2)
        @test t.v3 == 0
        @test t.t1 == t.t2 == t.t3
        next = t.t3
        @test next.v3 == 0
        @test next.v1 == t.v2
        @test next.v2 == t.v1
        @test next.t1 == next.t2 == next.t3 == t
    end
    @testset "For three vertices" begin
        t = Triangle(points, 1, 2, 3)
        # v1 -> v2 -> v3 should be clockwise
        @test t.v1 == 1
        @test t.v2 == 3
        @test t.v3 == 2
        @test is_left_of(points, t.v1, t.v3, t.v2)
        @test t.v1 == 1

        @test t.t1.t3 == t.t2.t3 == t.t3.t3 == t
        @test t.t1.t1 == t.t3.t2 == t.t2
        @test t.t1.t2 == t.t2.t1 == t.t3
        @test t.t2.t2 == t.t3.t1 == t.t1

        @test t.t2.v2 == t.t3.v1 == t.v1
        @test t.t1.v1 == t.t3.v2 == t.v2
        @test t.t1.v2 == t.t2.v1 == t.v3
    end
end
@testset "Test is_hull" begin
    points = [
         0  0
         1  0
         1  1
    ]
    t = Triangle(points, 1, 2, 3)
    @test is_hull(t.t1)
    @test !is_hull(t)
end
@testset "Test hull navigation function" begin
    points = [
         0  0
         1  0
         1  1
    ]
    t = Triangle(points, 1, 2, 3)
    @test next(t.t1) == t.t2
    @test prev(t.t1) == t.t3
end
@testset "Test find_base_tangente" begin
    points = [
         0  0
         1  0
         1  1
         2  0
         3  0
         3  1
    ]
    t1 = Triangle(points, 1, 2, 3)
    t2 = Triangle(points, 4, 5, 6)
    t3 = Triangle(2, 3)
    t4 = Triangle(4, 5)

    t_left, t_right = find_base_tangente(points, t1.t1, t2.t2)
    @test (t_left.v1, t_right.v2) == (1, 4)
end
@testset "Test _is_above_base" begin
    @testset "Test _is_above_base_left" begin
        @testset "For two edges triangles" begin
            points = [
                0 0
                0 1
                1 0
            ]
            t = Triangle(1, 2)
            @test _is_above_base_left(t, points, 1, 3) == true
            points = [
                0 -1
                0 0
                1 0
            ]
            t = Triangle(2, 1)
            @test _is_above_base_left(t, points, 2, 3) == false
        end
        @testset "For classic triangles" begin
            points = [
                0 0
                1 2
                2 1
                3 0
            ]
            t = Triangle(points, 1, 2, 3)
            @test _is_above_base_left(t, points, 1, 4) == true
            points = [
                0 0
                1 -1
                1 -2
                3 0
            ]
            t = Triangle(points, 1, 2, 3)
            @test _is_above_base_left(t, points, 1, 4) == false
            points = [
                0 1
                1 0
                2 0
                3 0
            ]
            t = Triangle(points, 1, 2, 3)
            @test _is_above_base_left(t, points, 3, 4) == true
            points = [
                0 -1
                1 0
                2 0
                3 0
            ]
            t = Triangle(points, 1, 2, 3)
            @test _is_above_base_left(t, points, 3, 4) == false
            points = [
                0 1
                1 1
                2 0
                3 0
            ]
            t = Triangle(points, 1, 2, 3)
            @test _is_above_base_left(t, points, 3, 4) == true
            points = [
                0 -1
                1 1
                2 0
                3 0
            ]
            t = Triangle(points, 1, 2, 3)
            @test _is_above_base_left(t, points, 3, 4) == false
        end
    end
    @testset "Test _is_above_base_right" begin
        @testset "For two edges triangles" begin
            points = [
                0 0
                1 0
                1 1
            ]
            t = Triangle(2, 3)
            @test _is_above_base_right(t, points, 1, 2) == true
            points = [
                0 0
                1 0
                1 -1
            ]
            t = Triangle(3, 2)
            @test _is_above_base_right(t, points, 1, 2) == false
        end
        @testset "For classic triangles" begin
            points = [
                0 0
                1 3
                2 1
                3 0
            ]
            t = Triangle(points, 2, 3, 4)
            @test _is_above_base_right(t, points, 1, 4) == true
            points = [
                0 0
                1 -3
                2 -2
                3 0
            ]
            t = Triangle(points, 2, 3, 4)
            @test _is_above_base_right(t, points, 1, 4) == false
            points = [
                0 0
                1 1
                2 0
                3 1
            ]
            t = Triangle(points, 2, 3, 4)
            @test _is_above_base_right(t, points, 1, 3) == true
            points = [
                0 0
                1 -1
                2 0
                3 -1
            ]
            t = Triangle(points, 2, 3, 4)
            @test _is_above_base_right(t, points, 1, 3) == false
            points = [
                0 0
                1 0
                2 1
                3 1
            ]
            t = Triangle(points, 2, 3, 4)
            @test _is_above_base_right(t, points, 1, 2) == true
            points = [
                0 0
                1 0
                2 -1
                3 -1
            ]
            t = Triangle(points, 2, 3, 4)
            @test _is_above_base_right(t, points, 1, 2) == false
        end
    end
end
@testset "Test _next" begin
    @testset "For two edges triangles" begin
        t = Triangle(1, 2)
        @test _next_left(t, 1) == t.t3
        @test _next_left(_next_left(t, 1), 1) == t
        @test _next_right(t, 1) == t.t3
        @test _next_right(_next_right(t, 1), 1) == t
    end
    @testset "For classic triangles" begin
        points = [
            0 0
            1 0
            1 1
        ]
        t = Triangle(points, 1, 2, 3)
        @test _next_left(t, 1) == t.t3
        @test _next_left(t, 3) == t.t1
        @test _next_left(t, 2) == t.t2
        @test _next_right(t, 1) == t.t2
        @test _next_right(t, 3) == t.t3
        @test _next_right(t, 2) == t.t1
    end
end
end
