using Flux, Statistics
using Flux.Tracker: update!, grad, back!
using Flux: onehotbatch, onecold, crossentropy, throttle
using Random
using Base.Iterators: repeated
using LinearAlgebra
using Images, ImageView, Plots

function ReadSEAVISION(filename)
    f = open(filename)
    A = map(row -> map(x -> parse(Int, x), split(row, ',')), eachline(f))
    close(f)

    T = [a[2:end] for a in A]
    L = [a[1] for a in A]
	N = length(A[1])

	T = reduce(hcat, T)	
    
    return length(A), N-1, T, L
end

function PlotDigits(row, k, w)
	R(X) = reshape(X, (71,21))[end:-1:1,:]

	A = zeros(71*w, 21*k)
	idx = 0
	for j = 1:w
		for i = 1:k
			idx = idx + 1
			A[(1+71*(j-1)):(71*j), (21*(i-1)+1):(21*(i))] = R(row[:,idx])
		end
	end

    p = heatmap(A, subplot=1, aspect_ratio=1, xticks = nothing, yticks = nothing, 
    	         colorbar=false, c=cgrad([:red, :blue, :white]))

    #png(p, "digits")
end

# Legge i dati di training
n, m, X, Y = ReadSEAVISION("seavision_training.csv")

# Stampa le prime 20 digits
PlotDigits(X, 10, 2)


# DA COMPLETARE ...
# DA COMPLETARE ...
# DA COMPLETARE ...