package forwarder

import (
	"context"
	"time"

	"helmet/pkg/client"
	"helmet/pkg/mlbctl"

	"github.com/spf13/cobra"
)

type DeleteForwarderParams struct {
	Hostname string
	Type     string
	Lport    uint32
}
type DeleteForwarderResult struct{}

func (tool *Tool) DeleteForwarder(cmd *cobra.Command, args []string) {
	tool.deleteForwarderParams.Hostname = args[0]
	res, err := tool.deleteForwarder(&tool.deleteForwarderParams)
	printResponse(res, err)
}

func (tool *Tool) deleteForwarder(params *DeleteForwarderParams) (*DeleteForwarderResult, error) {
	var err error
	res := &DeleteForwarderResult{}
	ref, err := client.NewReferer(params.Hostname)
	if err != nil {
		return res, err
	}
	authCred := client.NewAuthCredential(ref.Userinfo())
	conn, cli, err := client.NewClient(ref.Hostinfo(), authCred)
	if err != nil {
		return res, err
	}
	defer conn.Close()
	ctx, _ := context.WithTimeout(context.Background(), 1*time.Second)
	opReq := &mlbctl.DeleteForwarderParams{
		Type:  params.Type,
		Lport: params.Lport,
	}
	_, err = cli.DeleteForwarder(ctx, opReq)
	if err != nil {
		return res, err
	}
	return res, err
}
