# using LatticeGPU
using ADerrors
using BDIO


## NEDS UPDATE
function read_ff(name::String)

    file = BDIO_open(name,"r","FerFlow correlators")

    while BDIO_get_uinfo(file) != 14
        BDIO_seek!(file)
    end
    ihdr = Vector{Int32}(undef,1)
    BDIO_read(file, ihdr)
    if ihdr[1] == 1730280201
        println("Reading file ",name)
    else
        error("Wrong IHDR in file ", name)
    end

    while BDIO_get_uinfo(file) != 1
        BDIO_seek!(file)
    end

    dims=Vector{Int32}(undef,1)
    iL=Vector{Int32}(undef,4)
    dpars=Vector{Float64}(undef,2) # m0,csw
    th=Vector{ComplexF64}(undef,4)
    N_noiseff=Vector{Int32}(undef,1)
    nsteps=Vector{Int32}(undef,1)
    N_noisebf=Vector{Int32}(undef,1)
    nflow=Vector{Int32}(undef,1)
    tzeroff=Vector{Float64}(undef,1)
    epsilon=Vector{Float64}(undef,1)
    tsourceff=Vector{Int32}(undef,1)
    tsourcebf=Vector{Int32}(undef,1)


    BDIO_read(file, dims)
    BDIO_read(file, iL)
    BDIO_read(file, dpars)
    BDIO_read(file, th)
    BDIO_read(file, N_noiseff)
    BDIO_read(file, nsteps)
    BDIO_read(file, N_noisebf)
    BDIO_read(file, nflow)
    BDIO_read(file, tzeroff)
    BDIO_read(file, epsilon)
    BDIO_read(file, tsourceff)
    BDIO_read(file, tsourcebf)
    flow_times=Vector{Float64}(undef,nflow[1])
    nflow[1] > 0 ? BDIO_read(file, flow_times) : nothing

    print("Reading data from a ",Int64.(iL), " lattice.\n")
    println("Frontflow: ",N_noiseff[1], " sources at euclidean time ", tsourceff[1] ,". Flowtime starting at ",tzeroff[1], " with ",nsteps[1]," steps of size ", epsilon[1])
    println("Backflow: ",N_noisebf[1]," sources at euclidean time ",tsourcebf[1],". Flow times are ", flow_times)

    N_noiseff = N_noiseff[1]
    N_noisebf = N_noisebf[1]
    nflow = nflow[1]
    nsteps = nsteps[1]

    TvecR = Vector{Float64}(undef,iL[4]);
    TvecC = Vector{ComplexF64}(undef,iL[4]);
    CN = Vector{ComplexF64}(undef,1);
    RN = Vector{Float64}(undef,1);
    ET = Vector{ComplexF64}(undef,2+nsteps);

    pp_corr = Array{Float64}(undef,1,iL[4],N_noiseff);
    ap_corr = Array{ComplexF64}(undef,1,iL[4],N_noiseff);
    pp_corr_t = Array{Float64}(undef,1,iL[4],N_noiseff,nsteps+1);
    ap_corr_t = Array{ComplexF64}(undef,1,iL[4],N_noiseff,nsteps+1);
    pphat_t = Array{Float64}(undef,1,iL[4],N_noiseff,nsteps+1);
    pptilde_t = Array{ComplexF64}(undef,1,iL[4],N_noiseff,nsteps+1);
    Eoft = Array{Complex{Float64}}(undef,1,2+nsteps,iL[4]);

    pp_corr_tfl = Array{Float64}(undef,1,iL[4],N_noisebf,nflow);
    ap_corr_tfl = Array{ComplexF64}(undef,1,iL[4],N_noisebf,nflow);
    Sigma = Array{ComplexF64}(undef,1,iL[4],N_noisebf,nflow);
    Sigma_cfl = Array{ComplexF64}(undef,1,iL[4],N_noisebf,nflow);
    Sigma2 = Array{ComplexF64}(undef,1,iL[4],N_noisebf,nflow);
    Sigma2_cfl = Array{ComplexF64}(undef,1,iL[4],N_noisebf,nflow);
    ChiDchi = Array{ComplexF64}(undef,1,iL[4],N_noisebf,nflow);
    ChiDchi_cfl = Array{ComplexF64}(undef,1,iL[4],N_noisebf,nflow);

    pp_corr_mc = Array{Float64}(undef,0,iL[4],N_noiseff);
    ap_corr_mc = Array{ComplexF64}(undef,0,iL[4],N_noiseff);
    pphat_t_mc = Array{Float64}(undef,0,iL[4],N_noiseff,nsteps+1);
    pptilde_t_mc = Array{ComplexF64}(undef,0,iL[4],N_noiseff,nsteps+1);
    Eoft_mc = Array{Complex{Float64}}(undef,0,2+nsteps,iL[4]);
    pp_corr_t_mc = Array{Float64}(undef,0,iL[4],N_noiseff,nsteps+1);
    ap_corr_t_mc = Array{ComplexF64}(undef,0,iL[4],N_noiseff,nsteps+1);

    pp_corr_tfl_mc = Array{Float64}(undef,0,iL[4],N_noisebf,nflow);
    ap_corr_tfl_mc = Array{ComplexF64}(undef,0,iL[4],N_noisebf,nflow);
    Sigma_mc = Array{ComplexF64}(undef,0,iL[4],N_noisebf,nflow);
    Sigma_cfl_mc = Array{ComplexF64}(undef,0,iL[4],N_noisebf,nflow);
    Sigma2_mc = Array{ComplexF64}(undef,0,iL[4],N_noisebf,nflow);
    Sigma2_cfl_mc = Array{ComplexF64}(undef,0,iL[4],N_noisebf,nflow);
    ChiDchi_mc = Array{ComplexF64}(undef,0,iL[4],N_noisebf,nflow);
    ChiDchi_cfl_mc = Array{ComplexF64}(undef,0,iL[4],N_noisebf,nflow);

    while BDIO_get_uinfo(file) != 8
        BDIO_seek!(file)
    end

    while BDIO_get_uinfo(file) == 8

        for noi in 1:N_noiseff
            BDIO_read(file,TvecR)
            pp_corr[1,:,noi] .= TvecR

            BDIO_read(file,TvecC)
            ap_corr[1,:,noi] .= TvecC

            for fl in 1:nsteps+1
                BDIO_read(file,TvecR)
                pp_corr_t[1,:,noi,fl].= TvecR

                BDIO_read(file,TvecC)
                ap_corr_t[1,:,noi,fl].= TvecC

                BDIO_read(file,TvecR)
                pphat_t[1,:,noi,fl].= TvecR

                BDIO_read(file,TvecC)
                pptilde_t[1,:,noi,fl].= TvecC
            end
        end
        for time in 1:iL[4]
            BDIO_read(file,ET)
            Eoft[1,:,time] .= ET
        end
        for noi in 1:N_noisebf
            for fl in 1:nflow
                BDIO_read(file,TvecR)
                pp_corr_tfl[1,:,noi,fl].= TvecR

                BDIO_read(file,TvecC)
                ap_corr_tfl[1,:,noi,fl].= TvecC

                BDIO_read(file,TvecC)
                Sigma[1,:,noi,fl] .= TvecC
                BDIO_read(file,TvecC)
                Sigma_cfl[1,:,noi,fl] .= TvecC

                BDIO_read(file,TvecC)
                Sigma2[1,:,noi,fl] .= TvecC

                BDIO_read(file,TvecC)
                Sigma2_cfl[1,:,noi,fl] .= TvecC

                BDIO_read(file,TvecC)
                ChiDchi[1,:,noi,fl] .= TvecC

                BDIO_read(file,TvecC)
                ChiDchi_cfl[1,:,noi,fl] .= TvecC
            end
        end

        Eoft_mc=[Eoft_mc;Eoft]

        pp_corr_mc=[pp_corr_mc;pp_corr]
        ap_corr_mc=[ap_corr_mc;ap_corr]
        pp_corr_t_mc=[pp_corr_t_mc;pp_corr_t]
        ap_corr_t_mc=[ap_corr_t_mc;ap_corr_t]
        pphat_t_mc=[pphat_t_mc;pphat_t]
        pptilde_t_mc=[pptilde_t_mc;pptilde_t]

        pp_corr_tfl_mc=[pp_corr_tfl_mc;pp_corr_tfl]
        ap_corr_tfl_mc=[ap_corr_tfl_mc;ap_corr_tfl]
        Sigma_mc=[Sigma_mc;Sigma]
        Sigma_cfl_mc=[Sigma_cfl_mc;Sigma_cfl]
        Sigma2_mc=[Sigma2_mc;Sigma2]
        Sigma2_cfl_mc=[Sigma2_cfl_mc;Sigma2_cfl]
        ChiDchi_mc=[ChiDchi_mc;ChiDchi]
        ChiDchi_cfl_mc=[ChiDchi_cfl_mc;ChiDchi_cfl]

        BDIO_seek!(file)
        BDIO_seek!(file)
    end

    BDIO_close!(file)

    return Eoft_mc,pp_corr_mc,ap_corr_mc,pp_corr_t_mc,ap_corr_t_mc,pp_corr_tfl_mc,ap_corr_tfl_mc,Sigma_mc,Sigma_cfl_mc,Sigma2_mc,Sigma2_cfl_mc,ChiDchi_mc,ChiDchi_cfl_mc,pphat_t_mc,pptilde_t_mc
end


"""
function uwff(file::String)

Loads in the variables 'Eoft', 'pp_corr', 'ap_corr', 'pp_corr_t','ap_corr_t', 'pphat_t', 'pptilde_t','pp_corr_tfl','ap_corr_tfl' 'Sigma'(v1 and v2), 'Sigma_cfl'(v1 and v2), 'ChiDchi' and 'ChiDchi_cfl' the corresponding
'uwreal' quantities. Noise average has been performed. The indices are, when corresponding, eucliden time and flow times
"""
function uwff(file::String)
    Eoft_mc,pp_corr_mc,ap_corr_mc,pp_corr_t_mc,ap_corr_t_mc,pp_corr_tfl_mc,ap_corr_tfl_mc,Sigma_mc,Sigma_cfl_mc,Sigma2_mc,Sigma2_cfl_mc,ChiDchi_mc,ChiDchi_cfl_mc,pphat_t_mc,pptilde_t_mc = read_ff(file)
    runame = String(split(split(file,"/")[end],".")[1])

    T = size(pp_corr_mc)[2]
    Nsff = size(pp_corr_mc)[3]
    nsteps = size(pp_corr_t_mc)[4]-1
    Nsbf = size(pp_corr_tfl_mc)[3]
    Nfl = size(pp_corr_tfl_mc)[4]

    global Eoft = [uwreal(real.(Eoft_mc[:,i,j]),runame) for j in 1:T, i in 1:nsteps+2]

    global pp_corr = [uwreal(sum(pp_corr_mc,dims = 3)[:,i,1]./Nsff,runame) for i in 1:T]
    global ap_corr = [uwreal(real.(sum(ap_corr_mc,dims = 3)[:,i,1]./Nsff),runame) for i in 1:T]

    global pp_corr_t = [uwreal(sum(pp_corr_t_mc,dims = 3)[:,i,1,k]./Nsff,runame) for i in 1:T, k in 1:nsteps+1]
    global ap_corr_t = [uwreal(real.(sum(ap_corr_t_mc,dims = 3)[:,i,1,k]./Nsff),runame) for i in 1:T,k in 1:nsteps+1]

    global pphat_t = [uwreal(sum(pphat_t_mc,dims = 3)[:,i,1,k]./Nsff,runame) for i in 1:T, k in 1:nsteps+1]
    global pptilde_t = [uwreal(real.(sum(pptilde_t_mc,dims = 3)[:,i,1,k]./Nsff),runame) for i in 1:T,k in 1:nsteps+1]

    global pp_corr_tfl = [uwreal(sum(pp_corr_tfl_mc,dims = 3)[:,i,1,k]./Nsbf,runame) for i in 1:T, k in 1:Nfl]
    global ap_corr_tfl = [uwreal(real.(sum(ap_corr_tfl_mc,dims = 3)[:,i,1,k]./Nsbf),runame) for i in 1:T,k in 1:Nfl]

    global Sigma = [uwreal(real.(sum(Sigma_mc,dims = 3)[:,i,1,k]./Nsbf),runame) for i in 1:T, k in 1:Nfl]
    global Sigma_cfl = [uwreal(real.(sum(Sigma_cfl_mc,dims = 3)[:,i,1,k]./Nsbf),runame) for i in 1:T, k in 1:Nfl]

    global Sigma2 = [uwreal(real.(sum(Sigma2_mc,dims = 3)[:,i,1,k]./Nsbf),runame) for i in 1:T, k in 1:Nfl]
    global Sigma2_cfl = [uwreal(real.(sum(Sigma2_cfl_mc,dims = 3)[:,i,1,k]./Nsbf),runame) for i in 1:T, k in 1:Nfl]

    global ChiDchi = [uwreal(real.(sum(ChiDchi_mc,dims = 3)[:,i,1,k]./Nsbf),runame) for i in 1:T, k in 1:Nfl]
    global ChiDchi_cfl = [uwreal(real.(sum(ChiDchi_cfl_mc,dims = 3)[:,i,1,k]./Nsbf),runame) for i in 1:T, k in 1:Nfl]
    return nothing
end