package extension

import (
	"errors"
	"fmt"
	"github.com/chainreactors/malice-network/client/assets"
	"github.com/chainreactors/malice-network/client/repl"
	"github.com/chainreactors/malice-network/helper/utils/fileutils"
	"github.com/chainreactors/tui"
	"github.com/spf13/cobra"
	"os"
	"path/filepath"
)

// ExtensionsRemoveCmd - Remove an extension
func ExtensionsRemoveCmd(cmd *cobra.Command, con *repl.Console) {
	name := cmd.Flags().Arg(0)
	if name == "" {
		con.Log.Errorf("Extension name is required\n")
		return
	}
	confirmModel := tui.NewConfirm(fmt.Sprintf("Remove '%s' extension?", name))
	err := confirmModel.Run()
	if err != nil {
		con.Log.Errorf("Error running confirm model: %s", err)
		return
	}
	if !confirmModel.GetConfirmed() {
		return
	}
	err = RemoveExtensionByCommandName(name, con)
	if err != nil {
		con.Log.Errorf("Error removing extension: %s\n", err)
		return
	} else {
		con.Log.Infof("Extension '%s' removed\n", name)
	}
}

// RemoveExtensionByCommandName - Remove an extension by command name
func RemoveExtensionByCommandName(commandName string, con *repl.Console) error {
	if commandName == "" {
		return errors.New("command name is required")
	}
	if _, ok := loadedExtensions[commandName]; !ok {
		return errors.New("extension not loaded")
	}
	delete(loadedExtensions, commandName)
	implantMenu := con.ImplantMenu()
	for _, cmd := range implantMenu.Commands() {
		if cmd.Name() == commandName {
			implantMenu.RemoveCommand(cmd)
		}
	}
	extPath := filepath.Join(assets.GetExtensionsDir(), filepath.Base(commandName))
	if _, err := os.Stat(extPath); os.IsNotExist(err) {
		return nil
	}
	fileutils.ForceRemoveAll(extPath)
	return nil
}
