package main

import (
	"log"
	"os"

	"github.com/urfave/cli/v2"
	"pdf/lib"
)

func main() {
	app := &cli.App{
		Name:                   "pdf_invoice",
		Usage:                  "Pdf xml invoice helper cli",
		UseShortOptionHandling: true,
		Commands: []*cli.Command{
			{
				Name:                   "extract",
				UseShortOptionHandling: true,
				Usage:                  "<input-pdf> [<output-file>] Extract xml invoice from pdf",
				Flags: []cli.Flag{
					&cli.BoolFlag{
						Name:    "debug",
						Usage:   "Debug flag",
						Aliases: []string{"d"},
					},
				},
				Action: func(context *cli.Context) error {
					inputFilePath := context.Args().Get(0)
					outputFilePath := context.Args().Get(1)
					debug := context.Bool("debug")
					lib.EnableDebug(debug)

					lib.ExtractFromPdf(inputFilePath, outputFilePath)

					return nil
				},
			},
			{
				Name:                   "append",
				Usage:                  "<input-pdf> Append xml invoice to pdf",
				UseShortOptionHandling: true,
				Flags: []cli.Flag{
					&cli.StringFlag{
						Name:     "xml-input",
						Value:    "",
						Usage:    "Optional path to xml input File",
						Required: false,
					},
					&cli.StringFlag{
						Name:    "out",
						Usage:   "Optional path to pdf output File",
						Aliases: []string{"o"},
					},
					&cli.BoolFlag{
						Name:    "debug",
						Usage:   "Debug flag",
						Aliases: []string{"d"},
					},
				},
				Action: func(context *cli.Context) error {
					inputPdfFilePath := context.Args().Get(0)
					inputXmlFilePath := context.String("xml-input")
					outputFilePath := context.String("out")
					debug := context.Bool("debug")

					lib.EnableDebug(debug)

					lib.AppendToPdf(inputPdfFilePath, inputXmlFilePath, outputFilePath)

					return nil
				},
			},
		},
	}
	var err error

	err = app.Run(os.Args)
	if err != nil {
		log.Fatal(err)
	}
}
