# using SuperResolution
include("clusSR.jl")

mutable struct clusGAP
    modus::Int64
    wKL_Distance::SuperResolution.DistanceSR
    KL_Distance::SuperResolution.DistanceSR
    W_K::Array{Float64,1}
    W_K_star::Array{Float64,1}
    diff_gap::Array{Float64,1}
    diff_gap_max_idx::Array{Int64,1}
    diff_WK::Array{Float64,1}
    diff_WK_ratio::Array{Float64,1}
    diff_WK_max_idx::Array{Int64,1}
    Gap_n::Array{Float64,1}
    Gap::Array{Float64,1}
    s_k::Array{Float64,1}
    sd_k::Array{Float64,1}
    k_hat::Int64
    function clusGAP(ModelWKLD_SR::SuperResolution.ModelSR,ModelRef_SR::SuperResolution.ModelSR,modINPUT=1)
        obj=new()
        obj.modus=modINPUT
        obj.wKL_Distance=SuperResolution.DistanceSR(ModelWKLD_SR,1) #function DistanceSR in struct DistanceSR in clusSR.jl
        obj.W_K=map(i->obj.wKL_Distance.Distance[i].W_K,1:length(obj.wKL_Distance.Distance))
        if modINPUT==1 # weighted clustering
            obj.KL_Distance=SuperResolution.DistanceSR(ModelWKLD_SR,2)
            obj.W_K_star=map(i->obj.KL_Distance.Distance[i].W_K,1:length(obj.KL_Distance.Distance))
            obj.Gap_n=map(i->log(obj.KL_Distance.Distance[i].W_K)-log(obj.wKL_Distance.Distance[i].W_K),1:(length(obj.wKL_Distance.Distance)-1))
        elseif modINPUT==2 # UNweighted clustering (resp. reference model)
            obj.KL_Distance=SuperResolution.DistanceSR(ModelRef_SR,2)
            obj.W_K_star=map(i->obj.KL_Distance.Distance[i].W_K,1:length(obj.KL_Distance.Distance))
            obj.Gap_n=map(i->log(obj.KL_Distance.Distance[i].W_K)-log(obj.wKL_Distance.Distance[i].W_K),1:(length(obj.wKL_Distance.Distance)-1))
        end
        obj.diff_WK=obj.W_K[1:(end-1)]-obj.W_K[2:end]
        obj.diff_WK[obj.diff_WK.==0].=-10
        obj.diff_WK_ratio=map(i->obj.diff_WK[i]/obj.diff_WK[i+1],1:(length(obj.diff_WK)-1))
        obj.diff_WK_max_idx=findall(i->i==maximum(obj.diff_WK_ratio),obj.diff_WK_ratio)
        #obj.diff_WK_max_idx=findall(i->i==maximum(obj.diff_WK),obj.diff_WK)
        obj.diff_gap=obj.Gap_n[1:(end-1)]-obj.Gap_n[2:end]
        obj.diff_gap_max_idx=findall(i->i==maximum(obj.diff_gap),obj.diff_gap)
        obj
    end
end
export clusGAP
