#!/usr/bin/env python

import os
from cmdprogress.bar import ProgBar

# ------------------------------------------------ util ------------------------------------------------ #

def parse_years(arg):
    years = set()
    for a in arg.split(','):
        if '-' in a:
            y0,y1 = map(int,a.split('-'))
            years |= set(range(y0,y1+1))
        else:
            years |= {int(a)}
    years = list(years)
    years.sort()
    return years

def verify_dir(path):
    if len(path) and not os.path.exists(path):
        verify_dir(os.path.dirname(path))
        os.mkdir(path)


# ------------------------------------------------ compiler ------------------------------------------------ #

def file_compiler(fn):
    def wrapper(years,outdir):
        comp = fn()
        for year in years:
            with open(f'{outdir}/{year}.txt','w') as f:
                for l in comp(year):
                    print(l,file=f)
    return wrapper

@file_compiler
def eve():
    ##  bbsrc eve 1990-2016 ~/bbsrc/files
    from retro.eve import simEVE
    return simEVE

@file_compiler
def ros():
    ##  bbsrc ros 1990-2016 ~/bbsrc/files
    from retro.misc import simROS
    return simROS

@file_compiler
def ctx():
    ##  bbsrc ctx 1990-2016 ~/bbsrc/files
    from retro.misc import simCTX
    return simCTX

@file_compiler
def team():
    ##  bbsrc team 1990-2016 ~/bbsrc/files
    from retro.misc import simTEAM
    return simTEAM

@file_compiler
def gid():
    ## bbsrc gid 1990-2016 ~/bbsrc/files
    from retro.misc import simGID
    return simGID


### Main
def main():
    import argparse
    parser = argparse.ArgumentParser(description="Baseball Source File Compiler")
    subparsers = parser.add_subparsers(title="type of file to compile",dest='type',metavar='command')

    compiler_parser = argparse.ArgumentParser(add_help=False)
    compiler_parser.add_argument('years',type=parse_years,help='years to compile')
    compiler_parser.add_argument('outdir',type=str,help='folder to put compiled files into')

    parser_gid = subparsers.add_parser('gid',parents=[compiler_parser],help='compile gid files')
    parser_gid.set_defaults(run=gid)
    parser_eve = subparsers.add_parser('eve',parents=[compiler_parser],help='compile eve files')
    parser_eve.set_defaults(run=eve)
    parser_ros = subparsers.add_parser('ros',parents=[compiler_parser],help='compile ros files')
    parser_ros.set_defaults(run=ros)
    parser_ctx = subparsers.add_parser('ctx',parents=[compiler_parser],help='compile ctx files')
    parser_ctx.set_defaults(run=ctx)
    parser_team = subparsers.add_parser('team',parents=[compiler_parser],help='compile team files')
    parser_team.set_defaults(run=team)

    args = parser.parse_args()
    outdir = os.path.join(args.outdir,args.type)
    if os.path.exists(outdir) and not os.path.isdir(outdir):
        print(f"error: '{outdir}' exists and is not a directory")
        return
    verify_dir(outdir)
    args.run(ProgBar(prefix=args.type).iter(args.years),outdir)

if __name__=='__main__':
    main()
