// 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{}
)

// PhoneSaveDefaultSendAsRequest represents TL type `phone.saveDefaultSendAs#4167add1`.
//
// See https://core.telegram.org/method/phone.saveDefaultSendAs for reference.
type PhoneSaveDefaultSendAsRequest struct {
	// Call field of PhoneSaveDefaultSendAsRequest.
	Call InputGroupCallClass
	// SendAs field of PhoneSaveDefaultSendAsRequest.
	SendAs InputPeerClass
}

// PhoneSaveDefaultSendAsRequestTypeID is TL type id of PhoneSaveDefaultSendAsRequest.
const PhoneSaveDefaultSendAsRequestTypeID = 0x4167add1

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

func (s *PhoneSaveDefaultSendAsRequest) Zero() bool {
	if s == nil {
		return true
	}
	if !(s.Call == nil) {
		return false
	}
	if !(s.SendAs == nil) {
		return false
	}

	return true
}

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

// FillFrom fills PhoneSaveDefaultSendAsRequest from given interface.
func (s *PhoneSaveDefaultSendAsRequest) FillFrom(from interface {
	GetCall() (value InputGroupCallClass)
	GetSendAs() (value InputPeerClass)
}) {
	s.Call = from.GetCall()
	s.SendAs = from.GetSendAs()
}

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

// TypeName returns name of type in TL schema.
func (*PhoneSaveDefaultSendAsRequest) TypeName() string {
	return "phone.saveDefaultSendAs"
}

// TypeInfo returns info about TL type.
func (s *PhoneSaveDefaultSendAsRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "phone.saveDefaultSendAs",
		ID:   PhoneSaveDefaultSendAsRequestTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Call",
			SchemaName: "call",
		},
		{
			Name:       "SendAs",
			SchemaName: "send_as",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (s *PhoneSaveDefaultSendAsRequest) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode phone.saveDefaultSendAs#4167add1 as nil")
	}
	b.PutID(PhoneSaveDefaultSendAsRequestTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *PhoneSaveDefaultSendAsRequest) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode phone.saveDefaultSendAs#4167add1 as nil")
	}
	if s.Call == nil {
		return fmt.Errorf("unable to encode phone.saveDefaultSendAs#4167add1: field call is nil")
	}
	if err := s.Call.Encode(b); err != nil {
		return fmt.Errorf("unable to encode phone.saveDefaultSendAs#4167add1: field call: %w", err)
	}
	if s.SendAs == nil {
		return fmt.Errorf("unable to encode phone.saveDefaultSendAs#4167add1: field send_as is nil")
	}
	if err := s.SendAs.Encode(b); err != nil {
		return fmt.Errorf("unable to encode phone.saveDefaultSendAs#4167add1: field send_as: %w", err)
	}
	return nil
}

// Decode implements bin.Decoder.
func (s *PhoneSaveDefaultSendAsRequest) Decode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode phone.saveDefaultSendAs#4167add1 to nil")
	}
	if err := b.ConsumeID(PhoneSaveDefaultSendAsRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode phone.saveDefaultSendAs#4167add1: %w", err)
	}
	return s.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (s *PhoneSaveDefaultSendAsRequest) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode phone.saveDefaultSendAs#4167add1 to nil")
	}
	{
		value, err := DecodeInputGroupCall(b)
		if err != nil {
			return fmt.Errorf("unable to decode phone.saveDefaultSendAs#4167add1: field call: %w", err)
		}
		s.Call = value
	}
	{
		value, err := DecodeInputPeer(b)
		if err != nil {
			return fmt.Errorf("unable to decode phone.saveDefaultSendAs#4167add1: field send_as: %w", err)
		}
		s.SendAs = value
	}
	return nil
}

// GetCall returns value of Call field.
func (s *PhoneSaveDefaultSendAsRequest) GetCall() (value InputGroupCallClass) {
	if s == nil {
		return
	}
	return s.Call
}

// GetSendAs returns value of SendAs field.
func (s *PhoneSaveDefaultSendAsRequest) GetSendAs() (value InputPeerClass) {
	if s == nil {
		return
	}
	return s.SendAs
}

// PhoneSaveDefaultSendAs invokes method phone.saveDefaultSendAs#4167add1 returning error if any.
//
// See https://core.telegram.org/method/phone.saveDefaultSendAs for reference.
func (c *Client) PhoneSaveDefaultSendAs(ctx context.Context, request *PhoneSaveDefaultSendAsRequest) (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
}
