include("WBInclusions")
include("w/DICT")
#using WAV
#y, fs = wavread(raw"C:\Windows\Media\Ring01.wav")
#wavplay(y, fs)


APFS = "0168" # WBZ:APFS
global versionNr = APFS

include("WBInclusions")

global hex = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f']

global xx = []
global usedDirectories = fill!(Array{Bool}(undef,256),false)
global assholes = 0
println("\n WBZ v" * versionNr * ".  ARGS ", ARGS, "    ", APtS_ET)
if length(ARGS) == 0       
    txt = RRead("w/WBKountOut.html", "asis")
    sall = findall(r"href=\"images[^\"]+\"", txt)
    for i = 1 : length(sall)
        sa = sall[i]
        push!(xx,txt[sa.start + 18 : sa.stop - 1])
    end
elseif length(ARGS) == 1 
    global xx = split(replace(ARGS[1]," " => ""), ',')
elseif length(ARGS) > 0
    global xx = ARGS
end
shit = []
for s = 1:256
    append!(shit,0)
end
global first = "" # "\n"
for x in xx
    #y = collect(x)
    #leny = length(y)
    #Ltot = 0         # leny
    #for i = 1 : leny
    #    Ltot += Int32(y[i])
    #end
    Ltot = sum(map(Int32, collect(x)))    # Fun!
    ttt = Ltot % 256
    shit[ttt + 1] += 1
    v = Int32(ttt % 16)
    w = Int32((ttt - v) / 16)
    if usedDirectories[ttt + 1] == false
        global assholes += 1;
        usedDirectories[ttt + 1] = true
    end

    if length(ARGS) > 0
        wplus1 = w + 1
        lenD = length(readdir("w/images/" * hex[wplus1] * "/" * hex[wplus1] * hex[v + 1] * "/"))
        println("\n", first * "    " * hex[wplus1] * "/" * hex[wplus1] * hex[v + 1] * "    " * x *
                        " (" , lenD, ")  " , 
                        (getD(x) == MISSING ? "" : HI_RED * "   Already used!" * HI_END))
        global first = ""
    end
end

global shi = []
global shid = []
for s = 1:20
    append!(shi,0)
end


global crap = ""
for i = 1 : length(shit)
    if i % 32 == 1
        global crap *= " "
        if i % 64 == 1
            global crap *= "\n   "
        end
    end
    shi[shit[i] + 1] += 1
    global crap *= HILITE_TOHEX[shit[i]]
end

shiORIG = copy(shi)
global mulch = 0
for i = 1: length(shi)
    global mulch += (i - 1) * shi[i]
    if shi[i] != 0
        global shi[i] = [i - 1, shi[i], mulch]
    end
end    
for i = 1 : length(shi)
    if shi[length(shi)] != 0
        break
    end
    global shi = copy(shi)[1:length(shi) - 1]
end
global shidt = 0
for i = 2 : length(shi)
    if shi[i] == 0
        append!(shid, 0)
        continue
    end
    sh = shi[i][2] # (i - 1)
    append!(shid, sh) #  - shi[i - 1][3])
    global shidt += sh
end
xxx = shi
qqqqq = shi[length(shi)][3]

#println("\n\n", shi, "\n")
ss1 = SubstitutionString("[\\1, " * HI_YELLOW * "\\2" * HI_END)   #       s" * "\\1" * t*)
ss2 = SubstitutionString(" " * HI_RED * "\\1" * HI_END * "]]")

println(crap * 
    "  " * string(sum(shiORIG[11 : length(shiORIG)])) * "\n\n" * 
    replace(replace(replace(replace(replace(replace(replace(string(shi), r"\[(\d), (\d+)" => ss1), r"1([01234]), (\d+), " => s"1\1, YY1\2YY2, "), r" (\d+)]]" => ss2), "YY1" => HI_AQUA), "YY2" => HI_END), "], [7," => "],\n    [7,"), "], [12," => "],\n    [12,") *
     "]\n\n    " *  
    replace(replace(replace(string(map(x -> x < 10 ? string(shid[x]) : ("X" * string(shid[x]) * "Y"), Array(1:length(shid)))), "\"" => ""), "X" => HI_AQUA), "Y" => HI_END) *
     " " * 
     string(shidt) * 
     " " * 
     string(assholes)
)

if length(ARGS) != 0
    return(0)
end

for i = 1:length(xxx)
    if typeof(xxx[i]) == Vector{Int64}
        xxx[i] = (i - 1) * xxx[i][2]
    end
end
println(" ",xxx, "  ", formatDecimal(qqqqq /  assholes, 3))
