// Code generated by gotdgen, DO NOT EDIT.

package tg

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

	"go.uber.org/multierr"

	"github.com/gotd/td/bin"
	"github.com/gotd/td/tdjson"
	"github.com/gotd/td/tdp"
	"github.com/gotd/td/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{}
)

// PhoneSaveDefaultGroupCallJoinAsRequest represents TL type `phone.saveDefaultGroupCallJoinAs#575e1f8c`.
// Set the default peer that will be used to join a group call in a specific dialog.
//
// See https://core.telegram.org/method/phone.saveDefaultGroupCallJoinAs for reference.
type PhoneSaveDefaultGroupCallJoinAsRequest struct {
	// The dialog
	Peer InputPeerClass
	// The default peer that will be used to join group calls in this dialog, presenting
	// yourself as a specific user/channel.
	JoinAs InputPeerClass
}

// PhoneSaveDefaultGroupCallJoinAsRequestTypeID is TL type id of PhoneSaveDefaultGroupCallJoinAsRequest.
const PhoneSaveDefaultGroupCallJoinAsRequestTypeID = 0x575e1f8c

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

func (s *PhoneSaveDefaultGroupCallJoinAsRequest) Zero() bool {
	if s == nil {
		return true
	}
	if !(s.Peer == nil) {
		return false
	}
	if !(s.JoinAs == nil) {
		return false
	}

	return true
}

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

// FillFrom fills PhoneSaveDefaultGroupCallJoinAsRequest from given interface.
func (s *PhoneSaveDefaultGroupCallJoinAsRequest) FillFrom(from interface {
	GetPeer() (value InputPeerClass)
	GetJoinAs() (value InputPeerClass)
}) {
	s.Peer = from.GetPeer()
	s.JoinAs = from.GetJoinAs()
}

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

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

// TypeInfo returns info about TL type.
func (s *PhoneSaveDefaultGroupCallJoinAsRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "phone.saveDefaultGroupCallJoinAs",
		ID:   PhoneSaveDefaultGroupCallJoinAsRequestTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Peer",
			SchemaName: "peer",
		},
		{
			Name:       "JoinAs",
			SchemaName: "join_as",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (s *PhoneSaveDefaultGroupCallJoinAsRequest) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode phone.saveDefaultGroupCallJoinAs#575e1f8c as nil")
	}
	b.PutID(PhoneSaveDefaultGroupCallJoinAsRequestTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *PhoneSaveDefaultGroupCallJoinAsRequest) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode phone.saveDefaultGroupCallJoinAs#575e1f8c as nil")
	}
	if s.Peer == nil {
		return fmt.Errorf("unable to encode phone.saveDefaultGroupCallJoinAs#575e1f8c: field peer is nil")
	}
	if err := s.Peer.Encode(b); err != nil {
		return fmt.Errorf("unable to encode phone.saveDefaultGroupCallJoinAs#575e1f8c: field peer: %w", err)
	}
	if s.JoinAs == nil {
		return fmt.Errorf("unable to encode phone.saveDefaultGroupCallJoinAs#575e1f8c: field join_as is nil")
	}
	if err := s.JoinAs.Encode(b); err != nil {
		return fmt.Errorf("unable to encode phone.saveDefaultGroupCallJoinAs#575e1f8c: field join_as: %w", err)
	}
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (s *PhoneSaveDefaultGroupCallJoinAsRequest) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode phone.saveDefaultGroupCallJoinAs#575e1f8c to nil")
	}
	{
		value, err := DecodeInputPeer(b)
		if err != nil {
			return fmt.Errorf("unable to decode phone.saveDefaultGroupCallJoinAs#575e1f8c: field peer: %w", err)
		}
		s.Peer = value
	}
	{
		value, err := DecodeInputPeer(b)
		if err != nil {
			return fmt.Errorf("unable to decode phone.saveDefaultGroupCallJoinAs#575e1f8c: field join_as: %w", err)
		}
		s.JoinAs = value
	}
	return nil
}

// GetPeer returns value of Peer field.
func (s *PhoneSaveDefaultGroupCallJoinAsRequest) GetPeer() (value InputPeerClass) {
	if s == nil {
		return
	}
	return s.Peer
}

// GetJoinAs returns value of JoinAs field.
func (s *PhoneSaveDefaultGroupCallJoinAsRequest) GetJoinAs() (value InputPeerClass) {
	if s == nil {
		return
	}
	return s.JoinAs
}

// PhoneSaveDefaultGroupCallJoinAs invokes method phone.saveDefaultGroupCallJoinAs#575e1f8c returning error if any.
// Set the default peer that will be used to join a group call in a specific dialog.
//
// Possible errors:
//
//	400 JOIN_AS_PEER_INVALID: The specified peer cannot be used to join a group call.
//
// See https://core.telegram.org/method/phone.saveDefaultGroupCallJoinAs for reference.
func (c *Client) PhoneSaveDefaultGroupCallJoinAs(ctx context.Context, request *PhoneSaveDefaultGroupCallJoinAsRequest) (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
}
