using CVChannel
using Test
using DelimitedFiles

function noisyWHnonMult()
    print("\n\nThis script explores the Werner-Holevo channel's non-multiplicativity's robustness to noise.")
    print("\nTo do this we just take a linear combination of the Werner-Holevo channel with the maximally")
    print("depolarizing channel.")

    println("First we initialize the solver (One moment please...)")
    identChan(X) = X
    eaCVDual(choi(identChan,2,2),2,2)

    println("Now we actually get our data.")
    println("We consider d=3 and λ in [0,0.025,...,0.3] and mixing parameter α in [0,0.5,...,0.2].")
    println("NOTE: This will take a few minutes.")
    dim = 3
    depolChan(X) = 1/dim * [1 0 0 ; 0 1 0 ; 0 0 1]
    depol_choi = Choi(depolChan,dim,dim)
    λ_vals = 0:0.025:0.3
    α_vals = 0:0.05:0.2
    len, wid = length(λ_vals), length(α_vals)
    mult_table, cv_table = zeros(len,wid), zeros(len,wid)
    λ_ctr, α_ctr = 0, 1
    for λ in λ_vals
        println("Now evaluating for λ = ", λ)
        wern_choi = Choi(dim*wernerState(dim,λ),dim,dim)
        λ_ctr = λ_ctr + 1
        for α_amt in α_vals
            if α_amt == α_vals[Int(round(length(α_vals)/2))]
                println("Now halfway through this value of p.")
            end
            mix_choi = Choi((1-α_amt)*wern_choi.JN + α_amt*depol_choi.JN,dim,dim) #This uses linearity of Choi mapping
            cv_1, cv_2, par_cv, mult_val = pptCVMultiplicativity(mix_choi, mix_choi)
            mult_table[λ_ctr,α_ctr] = mult_val
            cv_table[λ_ctr,α_ctr] = cv_1
            α_ctr = α_ctr + 1
        end
        α_ctr = 1
    end

    info_vec = Vector{Union{Nothing,String}}(nothing, 2*len+3)
    info_vec[1] = "INFO:"
    info_vec[2] = "Generated by werner-holevo-non-multiplicativity-noise-robustness.jl"
    info_vec[3] = "dim = " * string(dim)
    info_vec[4] = "lambda_vals range: " * string(λ_vals)
    info_vec[5] = "alpha_vals range: " * string(α_vals)
    info_vec[6] = "Fixed lambda each row"
    info_vec[7] = "Fixed alpha each column"
    info_vec[8] = "First table is multiplicativity"
    info_vec[9] = "Second table is single copy cvPPT values"
    λ_label = vcat("", [λ_vals;])
    data_to_save_1 = hcat(λ_label,vcat([α_vals;]',mult_table))
    data_to_save_2 = hcat(λ_label,vcat([α_vals;]',cv_table))
    data_to_save = hcat(vcat(vcat(data_to_save_1,zeros(1,wid+1)),data_to_save_2),info_vec)
    println("Please name the file you'd like to write the results to: \n")
    file_name = readline()
    file_to_open = string(file_name,".csv")
    writedlm(file_to_open, data_to_save, ',')
    if all(mult_table -> mult_table < 1e-4 , mult_table)
        println("Something went awry. Please check your results.")
        return false
    else
        #We build printable multiplicativity table
        top_row = hcat(["λ↓  α:"],[α_vals;]')
        results = vcat(top_row,hcat([λ_vals;],mult_table))
        println("This is the table of multiplicativity values:")
        show(stdout, "text/plain", results)
        println("")
        println("Your results may also be found in your saved data file.")
        return true
    end
end

@testset "non-Multiplicativity with Noise" begin
    @test noisyWHnonMult()
end
println("Goodbye!")
