module testVoltGridGen
    use testHelper
    use kdefs
    use XML_Input
    use volttypes
    use voltapp, only : genVoltShellGrid
    use shellGrid

    implicit none

    character(len=strLen) :: xmlName = 'voltGridTests.xml'
    integer, parameter :: gamRes = 64
        !! genVoltShellGrid expects a gamera resolution to determine defaults for its own

    contains

    @before
	subroutine setup()
	end subroutine setup

	@after
	subroutine teardown()
	end subroutine teardown

    @test
    subroutine testGenVoltGrid_uniform()
        type(voltApp_T) :: vApp
        type(XML_Input_T) :: xmlInp
        integer :: Nt, Np

        real(rp) :: OKerr = 1e-8_rp
        real(rp) :: err_sum
        character(len=strLen) :: checkMessage

        xmlInp = New_XML_Input(trim(xmlName),'Kaiju/Uniform',.true.)

        ! Get some xml info ourselves first
        call xmlInp%Set_Val(Nt, "grid/Nt", -1) ! -1 so things blow up if xml isn't set properly
        call xmlInp%Set_Val(Np, "grid/Np", -1) ! -1 so things blow up if xml isn't set properly

        call genVoltShellGrid(vApp, xmlInp, gamRes)

        @assertEqual(vApp%shGrid%Nt, 2*Nt, "Wrong amount of theta cells")
        @assertEqual(vApp%shGrid%Np, Np  , "Wrong amount of phi cells")

        call mirrorCheck(vApp, Nt, OKerr)

    end subroutine testGenVoltGrid_uniform


    @test
    subroutine testGenVoltGrid_warped()
        type(voltApp_T) :: vApp
        type(XML_Input_T) :: xmlInp
        integer :: Nt, Np

        real(rp) :: OKerr = 1e-8_rp
        real(rp) :: err_sum
        character(len=strLen) :: checkMessage

        xmlInp = New_XML_Input(trim(xmlName),'Kaiju/Warped',.true.)

        ! Get some xml info ourselves first
        call xmlInp%Set_Val(Nt, "grid/Nt", -1) ! -1 so things blow up if xml isn't set properly
        call xmlInp%Set_Val(Np, "grid/Np", -1) ! -1 so things blow up if xml isn't set properly

        call genVoltShellGrid(vApp, xmlInp, gamRes)

        @assertEqual(2*Nt, vApp%shGrid%Nt, "Wrong amount of theta cells")
        @assertEqual(  Np, vApp%shGrid%Np, "Wrong amount of phi cells")

        call mirrorCheck(vApp, Nt, OKerr)

        write(*,*)vApp%shGrid%th

    end subroutine testGenVoltGrid_warped



    subroutine mirrorCheck(vApp, Nt, Okerr)
        type(voltApp_T), intent(in) :: vApp
        integer, intent(in) :: Nt
        real(rp), intent(in) :: OKerr

        logical :: isMirror
        real(rp) :: err_sum, mirror_th
        integer :: i, i_mirror

        ! Cell center check
        err_sum = 0
        do i=1,Nt
            i_mirror = 2*Nt+1-i
            mirror_th = PI - vApp%shGrid%thc(i_mirror)
            err_sum = err_sum + abs(vApp%shGrid%thc(i) - mirror_th)
        enddo
        @assertLessThanOrEqual(err_sum, 1E-8_rp, "Theta grid error, cells not mirrored properly")
        
        ! Cell corner check
        err_sum = 0
        do i=1,Nt
            i_mirror = 2*Nt+2-i
            mirror_th = PI - vApp%shGrid%th(i_mirror)
            err_sum = err_sum + abs(vApp%shGrid%th(i) - mirror_th)
        enddo
        @assertLessThanOrEqual(err_sum, 1E-8_rp, "Theta grid error, corners not mirrored properly")
    end subroutine mirrorCheck
end module testVoltGridGen