using OrderedCollections

using REPL.TerminalMenus

"""
    line_count_dir(directory::String)

Count lines of code in directory `directory`.
"""
function line_count_dir(directory, extensions, sort_results=true)
    extensions isa AbstractString && (extensions = (extensions,))
    all_files = [joinpath(root, f) for (root, dirs, files) in Base.Filesystem.walkdir(directory) for f in files]
    all_files_no_root = [f for (root, dirs, files) in Base.Filesystem.walkdir(directory) for f in files]
    filter!(x->any([endswith(x, y) for y in extensions]), all_files)
    n_lines = Dict([f=>0 for f in all_files])

    for f in all_files
        if isfile(f)
            n_lines[f] += countlines(f)
        else
            @warn "missing file $f"
        end
    end

    if sort_results
        n_lines = OrderedDict(sort(collect(n_lines), by=x->x[2]))
    end

    println("----------------- Number of lines per file")
    for (f,v) in n_lines
        println("$v \t $f")
    end
    println("----------------- Total number of lines of code:")
    println("n_lines = $(sum(values(n_lines)))")
    return n_lines
end


"""
    remove_dropbox_conflicts(code_dir::String)

Recursively delete files that match with "... conflicted copy ..."
"""
function remove_dropbox_conflicts(code_dir::AbstractString)
    all_files = [joinpath([root,f]...) for (root, dirs, files) in Base.Filesystem.walkdir(code_dir) for f in files];

    names_to_match = (
        "(LAPTOP-HN9BG775's conflicted copy",
        "(dhcp-103-207.caltech.edu's conflicted copy",
    )

    file_with_conflicts = filter(x->any(occursin(y, x) for y in names_to_match), all_files);

    println("Matched files found:")
    for file in file_with_conflicts
        println("$file")
    end

    options = ["yes", "no"]
    menu = RadioMenu(options, pagesize=4)
    choice = request("Would you like to delete these files?", menu)
    choice == -1 && error("Menu canceled")
    @assert choice == 1 || choice == 2
    if choice == 1
        for file in file_with_conflicts
          println("Deleting file $file")
          rm(file, force=true)
        end
    end
end


using ArgParse

function parse_commandline()
    s = ArgParseSettings()
    @add_arg_table! s begin
        "--directory"
            help = "Directory"
            arg_type = String
        "--extensions"
            help = "Extensions to include"
            arg_type = String
        "--sort_results"
            help = "Sort results by number of lines"
            arg_type = Bool
            default = true
    end

    return parse_args(s)
end

function main()
    parsed_args = parse_commandline()
    line_count_dir(parsed_args["directory"],
                   parsed_args["extensions"],
                   parsed_args["sort_results"])
end

if isinteractive()
    main()
end
