package cli

import (
	"fmt"
	"strings"

	"github.com/jandedobbeleer/oh-my-posh/src/cache"
	"github.com/jandedobbeleer/oh-my-posh/src/cli/font"
	"github.com/jandedobbeleer/oh-my-posh/src/dsc"
	"github.com/jandedobbeleer/oh-my-posh/src/log"
	"github.com/jandedobbeleer/oh-my-posh/src/runtime"
	"github.com/jandedobbeleer/oh-my-posh/src/terminal"

	"github.com/spf13/cobra"
)

var (
	zipFolder string
	headless  bool

	fontCmd = &cobra.Command{
		Use:   "font [install|configure]",
		Short: "Manage fonts",
		Long: `Manage fonts.

This command is used to install fonts and configure the font in your terminal.

  - install: oh-my-posh font install 3270`,
		ValidArgs: []string{
			"install",
			"configure",
		},
		Run: func(cmd *cobra.Command, args []string) {
			if len(args) == 0 {
				_ = cmd.Help()
				return
			}
			switch args[0] {
			case "install":
				var fontName string
				if len(args) > 1 {
					fontName = args[1]
				}

				env := &runtime.Terminal{}
				env.Init(&runtime.Flags{})

				sh := env.Shell()

				cache.Init(sh, cache.Persist)

				defer func() {
					cache.Close()
				}()

				terminal.Init(sh)

				if !strings.HasPrefix(zipFolder, "/") {
					zipFolder += "/"
				}

				fontName, err := font.Run(fontName, zipFolder, headless)
				if err != nil {
					log.Error(err)
					exitcode = 70
					return
				}

				if env.Root() {
					// do not update the DSC cache if we are running as root
					return
				}

				fontDSC := font.DSC()
				fontDSC.Load()
				fontDSC.Add(fontName)
				fontDSC.Save()

				return
			case "configure":
				fmt.Println("not implemented")
			default:
				_ = cmd.Help()
			}
		},
	}
)

func init() {
	fontCmd.Flags().StringVar(&zipFolder, "zip-folder", "", "the folder inside the zip file to install fonts from")
	fontCmd.Flags().BoolVar(&headless, "headless", false, "install font without TUI")
	fontCmd.AddCommand(dsc.Command(font.DSC()))
	RootCmd.AddCommand(fontCmd)
}
