package main

import (
	"os"
	"time"

	"github.com/champii/og/lib/common"

	"github.com/urfave/cli"
)

func parseArgs(done func(*common.OgConfig)) {
	cli_ := setupCli()

	cli_.Action = func(c *cli.Context) error {
		options := common.OgConfig{
			Blocks:      c.Bool("b"),
			Dirty:       c.Bool("d"),
			Print:       c.Bool("p"),
			Ast:         c.Bool("a"),
			SimpleAst:   c.Bool("s"),
			Quiet:       c.Bool("q"),
			Workers:     c.Int("w"),
			OutPath:     c.String("o"),
			Interpreter: c.Bool("i"),
			NoBuild:     c.Bool("n"),
			Run:         c.Bool("r"),
			Force:       c.Bool("f"),
			Paths:       []string(c.Args()),
		}

		done(&options)

		return nil
	}

	cli_.Run(os.Args)
}

func setupCli() *cli.App {
	cli.AppHelpTemplate = `NAME:
	{{.Name}} - {{.Usage}}

VERSION:
	{{.Version}}

USAGE:
	{{if .VisibleFlags}}{{.HelpName}} [options] [folders...|files...]

	If a Print argument (-p, -b, -d, -a) is given, no write on disk is done.

	If run without files, it will compile and build '.'{{end}}
	{{if len .Authors}}
AUTHOR:
	{{range .Authors}}{{ . }}{{end}}
	{{end}}{{if .Commands}}
OPTIONS:
	{{range .VisibleFlags}}{{.}}
	{{end}}{{end}}{{if .Copyright }}

COPYRIGHT:
	{{.Copyright}}
	{{end}}{{if .Version}}
	{{end}}
`

	cli.VersionFlag = cli.BoolFlag{
		Name:  "v, version",
		Usage: "Print version",
	}

	cli.HelpFlag = cli.BoolFlag{
		Name:  "h, help",
		Usage: "Print help",
	}

	app := cli.NewApp()

	app.Name = "Oglang"
	app.Version = "v0.7.2"
	app.Compiled = time.Now()

	app.Usage = "Golang on steroids"

	app.Flags = []cli.Flag{
		cli.BoolFlag{
			Name:  "r, run",
			Usage: "Run the binary",
		},
		cli.StringFlag{
			Name:  "o, out",
			Usage: "Output destination `directory`.",
			Value: "./",
		},
		cli.IntFlag{
			Name:  "w, workers",
			Usage: "Set the number of `jobs`",
			Value: 8,
		},
		cli.BoolFlag{
			Name:  "f, force",
			Usage: "Force the compilation even if no changes",
		},
		cli.BoolFlag{
			Name:  "p, print",
			Usage: "Print the file",
		},
		cli.BoolFlag{
			Name:  "d, dirty",
			Usage: "Print the file before going through 'go fmt'",
		},
		cli.BoolFlag{
			Name:  "b, blocks",
			Usage: "Print the file after it goes to preprocessor.",
		},
		cli.BoolFlag{
			Name:  "a, ast",
			Usage: "Print the generated AST",
		},
		cli.BoolFlag{
			Name:  "s, simple-ast",
			Usage: "Print a simple AST instead with only terminals",
		},
		cli.BoolFlag{
			Name:  "i, interpreter",
			Usage: "Run a small interpreter (ALPHA)",
		},
		cli.BoolFlag{
			Name:  "q, quiet",
			Usage: "Hide the progress output",
		},
		cli.BoolFlag{
			Name:  "n, no-build",
			Usage: "Don't run 'go build'",
		},
	}

	app.UsageText = "og [options] [folders|files]"

	return app
}
