using TriangularShapedCloudInterpolation, Test
using TriangularShapedCloudInterpolation: find_dim_bounds


function get_test_data()

    resx = 20
    resy = 20
    resz = 20

    pos_disk = zeros(100,3)
    pos_disk[:,1] = [48.27596607669735, 82.56551527383029, 59.674992478017664, 88.72867763530981, 83.74416420754775, 56.553478366219004, 79.40224633281969, 12.247702269226423, 24.76307597016234, 20.65396882675379, 39.739858815215, 53.62475250308947, 42.8799333224674, 99.30427626525548, 40.53984344173627, 98.39331484600464, 27.03099629126937, 17.450630381018904, 92.79680354167115, 1.3200570823053148, 12.117913180641903, 4.562368102053593, 87.02080768524914, 90.12759717061596, 58.105501738661246, 71.30583576368103, 59.18517757543411, 92.4420383395431, 77.6513847683014, 51.83257648918249, 40.932574768040574, 90.29451960422166, 78.40612915836837, 30.234070232039233, 21.904668208057522, 14.710524970038552, 24.965015822432825, 40.531297139835765, 72.08415683854568, 24.951503702876686, 7.952055758881382, 16.384912539215456, 80.23816701271183, 79.59108924359609, 47.50244919274409, 32.58640303654241, 66.67881322926301, 0.834989797604968, 0.09027604518616617, 54.00736832044688, 34.59985432603396, 60.22545028761639, 55.76372588019114, 98.74980384232335, 8.82358921426103, 70.09520172800605, 58.188177320186774, 23.293616266310014, 21.926567293568876, 70.19206131232734, 77.90738195484646, 33.45197083046363, 64.2135701482184, 13.908627644484106, 72.55638246300246, 32.8753721630465, 79.73303856613852, 98.13226337220782, 47.400892329979015, 84.61160817807271, 61.49369466119585, 56.51100321352167, 48.74279436208817, 93.7849625641569, 41.54486173607101, 91.50174126560717, 58.55910247426601, 11.277403796384554, 13.158869661372364, 76.35282256347016, 50.96104199434386, 8.369362954907489, 87.48237787005928, 94.36562189307767, 82.2596843584407, 98.90190443747824, 80.64028033152142, 82.27811763093183, 75.57697393340146, 49.29841329183786, 85.86136846154764, 15.365603830297726, 60.82742244892172, 2.508203108543694, 15.467284525598156, 36.68837053798115, 22.497454082363454, 65.82019419526065, 80.90694344251779, 72.382143332465]
    pos_disk[:,2] = [65.39480109245994, 70.10083828746288, 37.63810246458843, 25.55702502497532, 63.170200867430836, 26.222181031275493, 28.40076500939672, 93.16071978860619, 79.76899457558429, 18.37157757524963, 97.409994245956, 66.22547557732312, 3.6863624270635054, 42.32714426313824, 45.41216920871203, 60.662063203193426, 30.5873288310859, 48.17651887002696, 38.66593628945645, 44.28967590964359, 94.43491548979898, 71.33594937236028, 21.144350285384306, 2.739521704932346, 90.40108553908934, 80.66450870163537, 16.09113302185228, 19.793397425606617, 15.79947110891069, 43.30712856710876, 96.67147150204183, 43.327007552430196, 30.369994647083097, 3.169248891373466, 96.47167676442805, 19.22937093600179, 25.347315843710703, 78.5344212126635, 74.89827103456696, 27.750437567607023, 28.346649837442726, 47.52703983050135, 16.158439001847924, 88.32954929358989, 22.124037524686525, 68.89950571552734, 3.2528438884913635, 83.81769547396011, 73.51371688881345, 59.94019789672696, 81.63970724493946, 39.50110321488998, 13.167313760356691, 58.54206489611129, 92.92978138162051, 28.435264098330993, 47.97945193870354, 18.753540397736003, 4.54053096664464, 58.35089307393031, 64.10459221086693, 40.98522210242628, 43.04009709233625, 58.57504138145766, 86.3711262194778, 96.95621651132566, 40.868012086183136, 11.167131365588245, 12.883082941326052, 37.50868423333902, 33.44071881336519, 87.22231976163893, 92.87972467286691, 98.44586948532299, 23.01710259802792, 0.2786152416903054, 56.98600725065006, 80.50585591815833, 56.05859837567551, 37.004823789584826, 39.19021763001827, 26.493495860890803, 3.6690342929007302, 39.23192946424188, 35.65034152738238, 8.789465141105325, 72.65440204677299, 12.948159607879294, 3.906178425985596, 92.9524301408695, 53.22825001613241, 18.668048147191186, 96.47990059586134, 81.85824145723363, 95.71227928404382, 54.6000896405596, 11.601377078221532, 75.16641020695212, 4.5258015836853405, 49.917360882191545]
    pos_disk[:,3] = [48.564038903676106, 89.45478918925743, 21.750574371868048, 72.97546394350488, 92.40722189668385, 85.8526409023191, 97.64575845692376, 2.3616146277549266, 2.473875222815569, 21.7409350502628, 61.53377198562045, 48.71643708431306, 39.88321122267286, 60.74695452872412, 54.261336771570214, 13.569269939102725, 65.4715854674037, 59.4045857370967, 35.29067595647404, 30.990050711665027, 44.93465265641872, 64.2652654038083, 75.26209525677577, 52.58054850704568, 60.15092623262757, 56.56333940040141, 24.153665440506032, 46.070819063637146, 46.12586347763767, 37.26019208330016, 55.58778846491006, 68.93309729986655, 10.49844710270207, 39.885619439377116, 66.65103605318905, 77.07418628223249, 94.70412849426084, 9.630658643810076, 63.37844491906821, 47.75782956745871, 30.926343723448756, 4.698265680967562, 64.31720472014317, 98.18501866100497, 48.61407936708528, 19.731912607843462, 20.53968076824184, 40.175271529079005, 42.16525702461031, 15.573081317480785, 6.442716102471757, 21.94390759166074, 81.93648367461665, 80.91076751935535, 56.18631620596546, 75.27576968153708, 54.155301361972306, 43.934661356094054, 52.56565635272361, 66.29340009917834, 45.190758753661406, 48.49803399085015, 59.43112288352774, 2.5568744649344888, 69.67714967149436, 57.405196871226295, 18.566925726488904, 80.95842528647228, 65.9537989711509, 23.57553222620201, 4.394211962138184, 27.33168997679849, 51.31394335916977, 33.949265899040086, 74.43815123611886, 81.32197253371727, 7.762577162417839, 16.82475348550152, 93.06333496704026, 19.988963390752023, 94.36145800449829, 99.78290742237421, 5.889244713358632, 41.636983224154456, 46.84819297367977, 34.915457592428226, 47.16026814249889, 29.99799561223737, 91.96293960767035, 76.98025584903725, 32.59174420164748, 9.254245000034311, 54.80050340794167, 12.20906137713631, 51.516614996863154, 67.7763905236716, 44.975328537419415, 37.03769828711438, 93.66992368193591, 85.64487076244889]

    pos_disk_tsc = zeros(100,3)

    minx = minimum(pos_disk[:,1])
    maxx = maximum(pos_disk[:,1]) .* (1.0+1.e-6)
    dx   = -(minx - maxx) / resx
    pos_disk_tsc[:,1] = ( pos_disk[:,1] .- minx ) ./ dx 

    minx = minimum(pos_disk[:,2])
    maxx = maximum(pos_disk[:,2]) .* (1.0+1.e-6)
    dx   = -(minx - maxx) / resy
    pos_disk_tsc[:,2] = ( pos_disk[:,2] .- minx ) ./ dx 

    minx = minimum(pos_disk[:,3])
    maxx = maximum(pos_disk[:,3]) .* (1.0+1.e-6)
    dx   = -(minx - maxx) / resz
    pos_disk_tsc[:,3] = ( pos_disk[:,3] .- minx ) ./ dx

    rho = [0.1246130632669793, 0.25603749180063606, 0.4104355802162478, 0.5931598708600541, 0.7359315218105704, 0.9689369460686097, 0.38135503989297215, 0.6098051354275797, 0.8966399232205338, 0.675299997710952, 0.8198456567622867, 0.3242744798898247, 0.95224138615869, 0.6453328808947691, 0.6499731928261774, 0.4560801179385685, 0.2612971821651784, 0.8321480898033555, 0.7039184050232261, 0.5742836118900867, 0.16190054048366975, 0.8396157492301837, 0.00495882480188814, 0.6626729098202218, 0.44384137850656913, 0.37192372788756467, 0.01656256851134086, 0.16297499417611583, 0.9276381381659411, 0.8168864360712718, 0.30341799416020554, 0.24432549815597615, 0.2688388653618805, 0.6030398523895424, 0.5972765756216902, 0.24097468402579625, 0.7765414700319739, 0.8530800659297886, 0.5174513300434507, 0.0884473508293464, 0.8708937420043863, 0.8102675598242022, 0.9003113337536319, 0.13741967135155697, 0.09364327246670578, 0.17586275769297766, 0.566290677990132, 0.6358595632217443, 0.1782401573015988, 0.9421812936110101, 0.39430832614614153, 0.9449762810442555, 0.8081868343342873, 0.5158987086852937, 0.9825967999353682, 0.07458178257485804, 0.531105293927749, 0.7644847775769983, 0.3363977184144482, 0.9856472002247592, 0.5497509310270898, 0.8404188015705514, 0.36011013391154734, 0.19751213986592764, 0.874313109870857, 0.7510510536602204, 0.8151290522574428, 0.6669419081022179, 0.3365491392426945, 0.4795433719694546, 0.04286941282476198, 0.4712729226925354, 0.21316914619587846, 0.16241915654940464, 0.39568415093468223, 0.32291797712076664, 0.6234497162571426, 0.3464479715219908, 0.33823090500874864, 0.8940854179779834, 0.016924314962348808, 0.22225644374345754, 0.19775635489355392, 0.3498267055644031, 0.8371335263231685, 0.5777647141166327, 0.7717583996200446, 0.5490746259789394, 0.6046389447280436, 0.4675402645092084, 0.04331278006207917, 0.9302632894778857, 0.8171806161499484, 0.7211135755250679, 0.3483940202513707, 0.005278043113299269, 0.308268002323842, 0.8770268369379157, 0.5503219680779388, 0.23706305557960938]

    return rho, pos_disk_tsc, resx, resy, resz

end

@testset "TSC" begin

    @testset "Dimension bounds" begin

        dimx, dimy, dimz = find_dim_bounds(1)

        @test dimx == 3
        @test dimy == 1
        @test dimz == 1 

        dimx, dimy, dimz = find_dim_bounds(2)

        @test dimx == 3
        @test dimy == 3
        @test dimz == 1 

        dimx, dimy, dimz = find_dim_bounds(3)

        @test dimx == 3
        @test dimy == 3
        @test dimz == 3 

    end

    @testset "TSC positions" begin

        pos = zeros(3,100)
        pos[1,:] = [48.27596607669735, 82.56551527383029, 59.674992478017664, 88.72867763530981, 83.74416420754775, 56.553478366219004, 79.40224633281969, 12.247702269226423, 24.76307597016234, 20.65396882675379, 39.739858815215, 53.62475250308947, 42.8799333224674, 99.30427626525548, 40.53984344173627, 98.39331484600464, 27.03099629126937, 17.450630381018904, 92.79680354167115, 1.3200570823053148, 12.117913180641903, 4.562368102053593, 87.02080768524914, 90.12759717061596, 58.105501738661246, 71.30583576368103, 59.18517757543411, 92.4420383395431, 77.6513847683014, 51.83257648918249, 40.932574768040574, 90.29451960422166, 78.40612915836837, 30.234070232039233, 21.904668208057522, 14.710524970038552, 24.965015822432825, 40.531297139835765, 72.08415683854568, 24.951503702876686, 7.952055758881382, 16.384912539215456, 80.23816701271183, 79.59108924359609, 47.50244919274409, 32.58640303654241, 66.67881322926301, 0.834989797604968, 0.09027604518616617, 54.00736832044688, 34.59985432603396, 60.22545028761639, 55.76372588019114, 98.74980384232335, 8.82358921426103, 70.09520172800605, 58.188177320186774, 23.293616266310014, 21.926567293568876, 70.19206131232734, 77.90738195484646, 33.45197083046363, 64.2135701482184, 13.908627644484106, 72.55638246300246, 32.8753721630465, 79.73303856613852, 98.13226337220782, 47.400892329979015, 84.61160817807271, 61.49369466119585, 56.51100321352167, 48.74279436208817, 93.7849625641569, 41.54486173607101, 91.50174126560717, 58.55910247426601, 11.277403796384554, 13.158869661372364, 76.35282256347016, 50.96104199434386, 8.369362954907489, 87.48237787005928, 94.36562189307767, 82.2596843584407, 98.90190443747824, 80.64028033152142, 82.27811763093183, 75.57697393340146, 49.29841329183786, 85.86136846154764, 15.365603830297726, 60.82742244892172, 2.508203108543694, 15.467284525598156, 36.68837053798115, 22.497454082363454, 65.82019419526065, 80.90694344251779, 72.382143332465]
        pos[2,:] = [65.39480109245994, 70.10083828746288, 37.63810246458843, 25.55702502497532, 63.170200867430836, 26.222181031275493, 28.40076500939672, 93.16071978860619, 79.76899457558429, 18.37157757524963, 97.409994245956, 66.22547557732312, 3.6863624270635054, 42.32714426313824, 45.41216920871203, 60.662063203193426, 30.5873288310859, 48.17651887002696, 38.66593628945645, 44.28967590964359, 94.43491548979898, 71.33594937236028, 21.144350285384306, 2.739521704932346, 90.40108553908934, 80.66450870163537, 16.09113302185228, 19.793397425606617, 15.79947110891069, 43.30712856710876, 96.67147150204183, 43.327007552430196, 30.369994647083097, 3.169248891373466, 96.47167676442805, 19.22937093600179, 25.347315843710703, 78.5344212126635, 74.89827103456696, 27.750437567607023, 28.346649837442726, 47.52703983050135, 16.158439001847924, 88.32954929358989, 22.124037524686525, 68.89950571552734, 3.2528438884913635, 83.81769547396011, 73.51371688881345, 59.94019789672696, 81.63970724493946, 39.50110321488998, 13.167313760356691, 58.54206489611129, 92.92978138162051, 28.435264098330993, 47.97945193870354, 18.753540397736003, 4.54053096664464, 58.35089307393031, 64.10459221086693, 40.98522210242628, 43.04009709233625, 58.57504138145766, 86.3711262194778, 96.95621651132566, 40.868012086183136, 11.167131365588245, 12.883082941326052, 37.50868423333902, 33.44071881336519, 87.22231976163893, 92.87972467286691, 98.44586948532299, 23.01710259802792, 0.2786152416903054, 56.98600725065006, 80.50585591815833, 56.05859837567551, 37.004823789584826, 39.19021763001827, 26.493495860890803, 3.6690342929007302, 39.23192946424188, 35.65034152738238, 8.789465141105325, 72.65440204677299, 12.948159607879294, 3.906178425985596, 92.9524301408695, 53.22825001613241, 18.668048147191186, 96.47990059586134, 81.85824145723363, 95.71227928404382, 54.6000896405596, 11.601377078221532, 75.16641020695212, 4.5258015836853405, 49.917360882191545]
        pos[3,:] = [48.564038903676106, 89.45478918925743, 21.750574371868048, 72.97546394350488, 92.40722189668385, 85.8526409023191, 97.64575845692376, 2.3616146277549266, 2.473875222815569, 21.7409350502628, 61.53377198562045, 48.71643708431306, 39.88321122267286, 60.74695452872412, 54.261336771570214, 13.569269939102725, 65.4715854674037, 59.4045857370967, 35.29067595647404, 30.990050711665027, 44.93465265641872, 64.2652654038083, 75.26209525677577, 52.58054850704568, 60.15092623262757, 56.56333940040141, 24.153665440506032, 46.070819063637146, 46.12586347763767, 37.26019208330016, 55.58778846491006, 68.93309729986655, 10.49844710270207, 39.885619439377116, 66.65103605318905, 77.07418628223249, 94.70412849426084, 9.630658643810076, 63.37844491906821, 47.75782956745871, 30.926343723448756, 4.698265680967562, 64.31720472014317, 98.18501866100497, 48.61407936708528, 19.731912607843462, 20.53968076824184, 40.175271529079005, 42.16525702461031, 15.573081317480785, 6.442716102471757, 21.94390759166074, 81.93648367461665, 80.91076751935535, 56.18631620596546, 75.27576968153708, 54.155301361972306, 43.934661356094054, 52.56565635272361, 66.29340009917834, 45.190758753661406, 48.49803399085015, 59.43112288352774, 2.5568744649344888, 69.67714967149436, 57.405196871226295, 18.566925726488904, 80.95842528647228, 65.9537989711509, 23.57553222620201, 4.394211962138184, 27.33168997679849, 51.31394335916977, 33.949265899040086, 74.43815123611886, 81.32197253371727, 7.762577162417839, 16.82475348550152, 93.06333496704026, 19.988963390752023, 94.36145800449829, 99.78290742237421, 5.889244713358632, 41.636983224154456, 46.84819297367977, 34.915457592428226, 47.16026814249889, 29.99799561223737, 91.96293960767035, 76.98025584903725, 32.59174420164748, 9.254245000034311, 54.80050340794167, 12.20906137713631, 51.516614996863154, 67.7763905236716, 44.975328537419415, 37.03769828711438, 93.66992368193591, 85.64487076244889]

        pos_tsc = get_tsc_positions(pos, 20)

        rho, pos_disk_tsc, resx, resy, resz = get_test_data()

        @test pos_tsc ≈ pos_disk_tsc
    end

    @testset "TSC interpolation 1D" begin
        
        isolated = false
        wraparound = false

        rho, pos_disk_tsc, resx, resy, resz = get_test_data()

        @test_nowarn TSCInterpolation(rho, 
                                pos_disk_tsc[:,1], resx,
                                average=false, isolated=true)

    end

    @testset "TSC interpolation 2D" begin
        
        isolated = false
        wraparound = false

        rho, pos_disk_tsc, resx, resy, resz = get_test_data()


        @test_nowarn TSCInterpolation(rho, 
                                pos_disk_tsc[:,1:2], resx,
                                average=false, isolated=true)

    end

    @testset "TSC interpolation 3D" begin
        
        isolated = false
        wraparound = false

        rho, pos_disk_tsc, resx, resy, resz = get_test_data()

        tsc = TSCInterpolation(rho, 
                                pos_disk_tsc, [resx, resy, resz],
                                average=true)

        @test tsc[ 1,  1,  1] ≈ 0.0
        @test tsc[15, 12, 13] ≈ 0.9856472002247592
        @test tsc[20, 20, 20] ≈ 0.0

        @test_nowarn TSCInterpolation(rho, 
                                pos_disk_tsc, [resx, resy, resz],
                                average=false, wraparound=true)

        @test_nowarn TSCInterpolation(rho, 
                                pos_disk_tsc, [resx, resy, resz],
                                average=false, isolated=true)

        @test_nowarn TSCInterpolation(rho, 
                                pos_disk_tsc, resx,
                                average=false, isolated=true)

        @test_nowarn TSCInterpolation(rho, 
                                pos_disk_tsc[:,1], resx,
                                average=false, isolated=true)

    end

end