// Code generated by go-swagger; DO NOT EDIT.

package cli

import (
	"encoding/json"
	"fmt"

	"github.com/go-swagger/go-swagger/examples/cli/client/todos"

	"github.com/spf13/cobra"

	"github.com/go-openapi/swag"
)

// makeOperationTodosDestroyOneCmd returns a command to handle operation destroyOne
func makeOperationTodosDestroyOneCmd() (*cobra.Command, error) {
	cmd := &cobra.Command{
		Use:   "destroyOne",
		Short: ``,
		RunE:  runOperationTodosDestroyOne,
	}

	if err := registerOperationTodosDestroyOneParamFlags(cmd); err != nil {
		return nil, err
	}

	return cmd, nil
}

// runOperationTodosDestroyOne uses cmd flags to call endpoint api
func runOperationTodosDestroyOne(cmd *cobra.Command, args []string) error {
	appCli, err := makeClient(cmd, args)
	if err != nil {
		return err
	}
	// retrieve flag values from cmd and fill params
	params := todos.NewDestroyOneParams()
	if err, _ = retrieveOperationTodosDestroyOneIDFlag(params, "", cmd); err != nil {
		return err
	}
	if dryRun {
		logDebugf("dry-run flag specified. Skip sending request.")
		return nil
	}
	// make request and then print result
	msgStr, err := parseOperationTodosDestroyOneResult(appCli.Todos.DestroyOne(params, nil))
	if err != nil {
		return err
	}

	if !debug {
		fmt.Println(msgStr)
	}

	return nil
}

// registerOperationTodosDestroyOneParamFlags registers all flags needed to fill params
func registerOperationTodosDestroyOneParamFlags(cmd *cobra.Command) error {
	if err := registerOperationTodosDestroyOneIDParamFlags("", cmd); err != nil {
		return err
	}
	return nil
}

func registerOperationTodosDestroyOneIDParamFlags(cmdPrefix string, cmd *cobra.Command) error {

	flagIDDescription := `Required. `

	var flagIDName string
	if cmdPrefix == "" {
		flagIDName = "id"
	} else {
		flagIDName = fmt.Sprintf("%v.id", cmdPrefix)
	}

	var flagIDDefault int64

	_ = cmd.PersistentFlags().Int64(flagIDName, flagIDDefault, flagIDDescription)

	return nil
}

func retrieveOperationTodosDestroyOneIDFlag(m *todos.DestroyOneParams, cmdPrefix string, cmd *cobra.Command) (error, bool) {
	retAdded := false
	if cmd.Flags().Changed("id") {

		var flagIDName string
		if cmdPrefix == "" {
			flagIDName = "id"
		} else {
			flagIDName = fmt.Sprintf("%v.id", cmdPrefix)
		}

		flagIDValue, err := cmd.Flags().GetInt64(flagIDName)
		if err != nil {
			return err, false
		}
		m.ID = flagIDValue

	}

	return nil, retAdded
}

// parseOperationTodosDestroyOneResult parses request result and return the string content
func parseOperationTodosDestroyOneResult(resp0 *todos.DestroyOneNoContent, respErr error) (string, error) {
	if respErr != nil {
		// default response
		var iRespD any = respErr
		respD, ok := iRespD.(*todos.DestroyOneDefault)
		if ok {
			if !swag.IsZero(respD) && !swag.IsZero(respD.Payload) {
				msgStr, err := json.Marshal(respD.Payload)
				if err != nil {
					return "", err
				}
				return string(msgStr), nil
			}
		}

		// responses
		// Non schema case: warning destroyOneNoContent is not supported
		return "", respErr
	}

	// success responses
	// warning: non schema response destroyOneNoContent is not supported by go-swagger cli yet.
	return "", nil
}
