package mnemonic

import (
	"fmt"

	"github.com/0xPolygon/polygon-cli/hdwallet"
	"github.com/spf13/cobra"
)

var (
	inputMnemonicWords int
	inputMnemonicLang  string
)

// mnemonicCmd represents the mnemonic command
var MnemonicCmd = &cobra.Command{
	Use:   "mnemonic",
	Short: "Generate a BIP39 mnemonic seed.",
	Long:  "",
	RunE: func(cmd *cobra.Command, args []string) error {
		mnemonic, err := hdwallet.NewMnemonic(inputMnemonicWords, inputMnemonicLang)
		if err != nil {
			return err
		}
		cmd.Println(mnemonic)
		return nil
	},
	PreRunE: func(cmd *cobra.Command, args []string) error {
		if inputMnemonicWords < 12 {
			return fmt.Errorf("the number of words in the mnemonic must be 12 or more. Given: %d", inputMnemonicWords)
		}
		if inputMnemonicWords > 24 {
			return fmt.Errorf("the number of words in the mnemonic must be 24 or less. Given: %d", inputMnemonicWords)
		}
		if inputMnemonicWords%3 != 0 {
			return fmt.Errorf("the number of words in the mnemonic must be a multiple of 3")
		}
		return nil

	},
}

func init() {
	f := MnemonicCmd.Flags()
	f.IntVar(&inputMnemonicWords, "words", 24, "number of words to use in mnemonic")
	f.StringVar(&inputMnemonicLang, "language", "english", "which language to use [ChineseSimplified, ChineseTraditional, Czech, English, French, Italian, Japanese, Korean, Spanish]")
}
