function! vaxe#flow#Targets(...)
    return s:flow_targets
endfunction

function! vaxe#flow#ProjectFlow(...)
    if exists('g:vaxe_flow')
        unlet g:vaxe_flow
    endif
    let g:vaxe_working_directory = getcwd()

    if a:0 > 0 && a:1 != ''
        let g:vaxe_flow = expand(a:1,':p')
    else
        let flows = split(glob("**/*.flow"),'\n')

        if len(flows) == 0
            echoerr "No flow/openfl project files found in current working directory"
            return
        endif

        let base_flow = vaxe#util#InputList("Select flow", flows)

        if base_flow !~ "^\([a-zA-Z]:\)\=[/\\]"
            let base_flow = getcwd() . '/' . base_flow
        endif

        echomsg 'Project flow selected: ' . base_flow

        let g:vaxe_flow = base_flow
    endif
    if !filereadable(g:vaxe_flow)
        echoerr "Project flow file not valid, please create one."
        return
    endif
    call vaxe#flow#BuildFlowHxml(g:vaxe_flow)
    call vaxe#SetCompiler()
    return g:vaxe_flow
endfunction

function! vaxe#flow#Clean(...)
    if (a:0 && a:1 != '')
        let target = split(a:1)[0]
    else
        let target = g:vaxe_flow_target
    endif
    let command = "flow clean ".target
    call vaxe#Log(command)
    call s:Sys(command)
endfunction

function! vaxe#flow#RebuildHxml()
   if exists("b:vaxe_flow")
      call vaxe#flow#BuildFlowHxml(b:vaxe_flow)
   endif
endfunction

"A simple system function that first changes directory to the current vaxe
"working directory
function! s:Sys(cmd)
    call system("cd ".g:vaxe_working_directory." && ".a:cmd)
endfunction

function! vaxe#flow#BuildFlowHxml(flow)
    let base_hxml = a:flow.".hxml"

    if !strlen(g:vaxe_flow_target)
        call vaxe#flow#Target(a:flow)
    endif

    let g:vaxe_working_directory = fnamemodify(a:flow, ":p:h")
    let cdcmd = 'cd "'.fnameescape(g:vaxe_working_directory).'" && '

    "create the flow.hxml if not present
    if !filereadable(base_hxml)
        " pipe flow display to an hxml for completions
        let escape_base = fnameescape(base_hxml)
        call s:Sys(" echo '# THIS FILE IS AUTOGENERATED BY VAXE, ANY EDITS ARE DISCARDED' " . " > " . escape_base)
        call s:Sys(" haxelib run flow info " . g:vaxe_flow_target . " --hxml "
                    \. " >> " . escape_base )
    endif

    let b:vaxe_hxml = base_hxml
    " let g:vaxe_hxml = b:vaxe_hxml " don't set a global projet var by default
endfunction

"Sets the target.  If target is missing it asks the user. Also updates the
"makeprg compiler command
function! vaxe#flow#Target(flow, ...)
    let g:vaxe_flow_target = ''
    if a:0 > 1 && a:2 != ''
        let g:vaxe_flow_target = a:2
    else
        let g:vaxe_flow_target = vaxe#util#InputList("Select flow Target", s:flow_targets)
        let g:vaxe_flow_target = split(g:vaxe_flow_target, ":")[0]
    endif
    call vaxe#flow#BuildFlowHxml(a:flow)
    call vaxe#SetCompiler()
endfunction

" A list of all the flow targets
let s:flow_targets = [ "android : Create Google Android applications"
            \, "web : Create HTML5 webgl applications"
            \, "ios : Create Apple iOS applications"
            \, "linux --arch 32 : Create Linux 32-bit applications"
            \, "linux --arch 64 : Create Linux 64-bit applications"
            \, "mac --arch 32 : Create Apple Mac OS X 32-bit applications"
            \, "mac --arch 64 : Create Apple Mac OS X 64-bit applications"
            \, "windows --arch 32 : Create Microsoft Windows 32-bit applications"
            \, "windows --arch 64 : Create Microsoft Windows 64-bit applications"]
