const _ARFF_SPACE       = UInt8(' ')
const _ARFF_COMMENT     = UInt8('%')
const _ARFF_AT          = UInt8('@')
const _ARFF_SEP         = UInt8(',')
const _ARFF_NEWLINE     = UInt8('\n')
const _ARFF_NOMSTART    = UInt8('{')
const _ARFF_NOMEND      = UInt8('}')
const _ARFF_ESC         = UInt8('\\')
const _ARFF_MISSING     = UInt8('?')
const _ARFF_RELMARK     = UInt8('\'')

function readARFF(p::String)
    open(p, "r") do io
        df = DataFrame()
        classes = String[]
        lines = readlines(io)
        for i in 1:length(lines)
            line = lines[i]
            # If not empty line or comment
            if !isempty(line)
                if UInt8(line[1]) != _ARFF_COMMENT
                    sline = split(line, " ")
                    # println(sline[1][1])
                    # If the first symbol is @
                    if UInt8(sline[1][1]) == _ARFF_AT
                        # If @relation
                        if sline[1][2:end] == "relation"
                            # println("Relation: " * sline[2])
                        end

                        # if sline[1][2:end] == "attribute" && sline[2] == "class"
                        #     classes = sline[3][2:end-1]
                        #     println(classes)
                        # end
                    # data, first char is '
                    elseif UInt8(sline[1][1]) == _ARFF_RELMARK
                        sline[1] = sline[1][2:end]
                        data_and_class = split(sline[1],"\'")
                        string_data = split(data_and_class[1], "\\n")
                        class = data_and_class[2][2:end]
                        
                        if isempty(names(df))
                            for i in 1:length(string_data)
                                insertcols!(df, Symbol("A" * string(i)) => Array{Float64, 1}[]) # add the attributes as 1,2,3,ecc.
                            end
                        end

                        float_data = Dict{Int,Vector{Float64}}()

                        for i in 1:length(string_data)
                            float_data[i] = map(x->parse(Float64,x), split(string_data[i], ","))
                        end

                        # @show float_data

                        
                        push!(df, [float_data[i] for i in 1:length(string_data)])
                        push!(classes, class)
                        # @show data
                        # @show class
                    end
                end
            end
            #error(1)
        end
        return ClassificationDataset([ModalFrame(df)], CategoricalArray(classes))
    end
end

train = readARFF("data/ARFF/Libras_TRAIN-10.txt")

# for (i, x) in enumerate([1,2,3,4])
#     println(i, " ", x)
#     i=3
# end



# @grammar begin
#     S   = UnivGlobal(F) | ExistGlobal(F)
#     F   = UnivL(implies(F,F))
#     F   = ExistL(and(F,F))
#     F   = implies(F,F)
#     F   = and(F,F)
#     F   = f(A)
# end
#
# @grammar begin
#     S   = ExistentialIntervalRelation{:L}(and(F,F))
#     F   = f(A)
# end
