// Code generated by gotdgen, DO NOT EDIT.

package tg

import (
	"context"
	"errors"
	"fmt"
	"sort"
	"strings"

	"go.uber.org/multierr"

	"go.mau.fi/mautrix-telegram/pkg/gotd/bin"
	"go.mau.fi/mautrix-telegram/pkg/gotd/tdjson"
	"go.mau.fi/mautrix-telegram/pkg/gotd/tdp"
	"go.mau.fi/mautrix-telegram/pkg/gotd/tgerr"
)

// No-op definition for keeping imports.
var (
	_ = bin.Buffer{}
	_ = context.Background()
	_ = fmt.Stringer(nil)
	_ = strings.Builder{}
	_ = errors.Is
	_ = multierr.AppendInto
	_ = sort.Ints
	_ = tdp.Format
	_ = tgerr.Error{}
	_ = tdjson.Encoder{}
)

// AicomposeSaveToneRequest represents TL type `aicompose.saveTone#1782cbb1`.
//
// See https://core.telegram.org/method/aicompose.saveTone for reference.
type AicomposeSaveToneRequest struct {
	// Tone field of AicomposeSaveToneRequest.
	Tone InputAiComposeToneClass
	// Unsave field of AicomposeSaveToneRequest.
	Unsave bool
}

// AicomposeSaveToneRequestTypeID is TL type id of AicomposeSaveToneRequest.
const AicomposeSaveToneRequestTypeID = 0x1782cbb1

// Ensuring interfaces in compile-time for AicomposeSaveToneRequest.
var (
	_ bin.Encoder     = &AicomposeSaveToneRequest{}
	_ bin.Decoder     = &AicomposeSaveToneRequest{}
	_ bin.BareEncoder = &AicomposeSaveToneRequest{}
	_ bin.BareDecoder = &AicomposeSaveToneRequest{}
)

func (s *AicomposeSaveToneRequest) Zero() bool {
	if s == nil {
		return true
	}
	if !(s.Tone == nil) {
		return false
	}
	if !(s.Unsave == false) {
		return false
	}

	return true
}

// String implements fmt.Stringer.
func (s *AicomposeSaveToneRequest) String() string {
	if s == nil {
		return "AicomposeSaveToneRequest(nil)"
	}
	type Alias AicomposeSaveToneRequest
	return fmt.Sprintf("AicomposeSaveToneRequest%+v", Alias(*s))
}

// FillFrom fills AicomposeSaveToneRequest from given interface.
func (s *AicomposeSaveToneRequest) FillFrom(from interface {
	GetTone() (value InputAiComposeToneClass)
	GetUnsave() (value bool)
}) {
	s.Tone = from.GetTone()
	s.Unsave = from.GetUnsave()
}

// TypeID returns type id in TL schema.
//
// See https://core.telegram.org/mtproto/TL-tl#remarks.
func (*AicomposeSaveToneRequest) TypeID() uint32 {
	return AicomposeSaveToneRequestTypeID
}

// TypeName returns name of type in TL schema.
func (*AicomposeSaveToneRequest) TypeName() string {
	return "aicompose.saveTone"
}

// TypeInfo returns info about TL type.
func (s *AicomposeSaveToneRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "aicompose.saveTone",
		ID:   AicomposeSaveToneRequestTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Tone",
			SchemaName: "tone",
		},
		{
			Name:       "Unsave",
			SchemaName: "unsave",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (s *AicomposeSaveToneRequest) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode aicompose.saveTone#1782cbb1 as nil")
	}
	b.PutID(AicomposeSaveToneRequestTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *AicomposeSaveToneRequest) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode aicompose.saveTone#1782cbb1 as nil")
	}
	if s.Tone == nil {
		return fmt.Errorf("unable to encode aicompose.saveTone#1782cbb1: field tone is nil")
	}
	if err := s.Tone.Encode(b); err != nil {
		return fmt.Errorf("unable to encode aicompose.saveTone#1782cbb1: field tone: %w", err)
	}
	b.PutBool(s.Unsave)
	return nil
}

// Decode implements bin.Decoder.
func (s *AicomposeSaveToneRequest) Decode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode aicompose.saveTone#1782cbb1 to nil")
	}
	if err := b.ConsumeID(AicomposeSaveToneRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode aicompose.saveTone#1782cbb1: %w", err)
	}
	return s.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (s *AicomposeSaveToneRequest) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode aicompose.saveTone#1782cbb1 to nil")
	}
	{
		value, err := DecodeInputAiComposeTone(b)
		if err != nil {
			return fmt.Errorf("unable to decode aicompose.saveTone#1782cbb1: field tone: %w", err)
		}
		s.Tone = value
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode aicompose.saveTone#1782cbb1: field unsave: %w", err)
		}
		s.Unsave = value
	}
	return nil
}

// GetTone returns value of Tone field.
func (s *AicomposeSaveToneRequest) GetTone() (value InputAiComposeToneClass) {
	if s == nil {
		return
	}
	return s.Tone
}

// GetUnsave returns value of Unsave field.
func (s *AicomposeSaveToneRequest) GetUnsave() (value bool) {
	if s == nil {
		return
	}
	return s.Unsave
}

// AicomposeSaveTone invokes method aicompose.saveTone#1782cbb1 returning error if any.
//
// See https://core.telegram.org/method/aicompose.saveTone for reference.
func (c *Client) AicomposeSaveTone(ctx context.Context, request *AicomposeSaveToneRequest) (bool, error) {
	var result BoolBox

	if err := c.rpc.Invoke(ctx, request, &result); err != nil {
		return false, err
	}
	_, ok := result.Bool.(*BoolTrue)
	return ok, nil
}
