// Code generated by gotdgen, DO NOT EDIT.

package tdapi

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

// SetUserPersonalProfilePhotoRequest represents TL type `setUserPersonalProfilePhoto#1baa28f6`.
type SetUserPersonalProfilePhotoRequest struct {
	// User identifier
	UserID int64
	// Profile photo to set; pass null to delete the photo; inputChatPhotoPrevious isn't
	// supported in this function
	Photo InputChatPhotoClass
}

// SetUserPersonalProfilePhotoRequestTypeID is TL type id of SetUserPersonalProfilePhotoRequest.
const SetUserPersonalProfilePhotoRequestTypeID = 0x1baa28f6

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

func (s *SetUserPersonalProfilePhotoRequest) Zero() bool {
	if s == nil {
		return true
	}
	if !(s.UserID == 0) {
		return false
	}
	if !(s.Photo == nil) {
		return false
	}

	return true
}

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

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

// TypeName returns name of type in TL schema.
func (*SetUserPersonalProfilePhotoRequest) TypeName() string {
	return "setUserPersonalProfilePhoto"
}

// TypeInfo returns info about TL type.
func (s *SetUserPersonalProfilePhotoRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "setUserPersonalProfilePhoto",
		ID:   SetUserPersonalProfilePhotoRequestTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "UserID",
			SchemaName: "user_id",
		},
		{
			Name:       "Photo",
			SchemaName: "photo",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (s *SetUserPersonalProfilePhotoRequest) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode setUserPersonalProfilePhoto#1baa28f6 as nil")
	}
	b.PutID(SetUserPersonalProfilePhotoRequestTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *SetUserPersonalProfilePhotoRequest) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode setUserPersonalProfilePhoto#1baa28f6 as nil")
	}
	b.PutInt53(s.UserID)
	if s.Photo == nil {
		return fmt.Errorf("unable to encode setUserPersonalProfilePhoto#1baa28f6: field photo is nil")
	}
	if err := s.Photo.Encode(b); err != nil {
		return fmt.Errorf("unable to encode setUserPersonalProfilePhoto#1baa28f6: field photo: %w", err)
	}
	return nil
}

// Decode implements bin.Decoder.
func (s *SetUserPersonalProfilePhotoRequest) Decode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode setUserPersonalProfilePhoto#1baa28f6 to nil")
	}
	if err := b.ConsumeID(SetUserPersonalProfilePhotoRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode setUserPersonalProfilePhoto#1baa28f6: %w", err)
	}
	return s.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (s *SetUserPersonalProfilePhotoRequest) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode setUserPersonalProfilePhoto#1baa28f6 to nil")
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode setUserPersonalProfilePhoto#1baa28f6: field user_id: %w", err)
		}
		s.UserID = value
	}
	{
		value, err := DecodeInputChatPhoto(b)
		if err != nil {
			return fmt.Errorf("unable to decode setUserPersonalProfilePhoto#1baa28f6: field photo: %w", err)
		}
		s.Photo = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (s *SetUserPersonalProfilePhotoRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if s == nil {
		return fmt.Errorf("can't encode setUserPersonalProfilePhoto#1baa28f6 as nil")
	}
	b.ObjStart()
	b.PutID("setUserPersonalProfilePhoto")
	b.Comma()
	b.FieldStart("user_id")
	b.PutInt53(s.UserID)
	b.Comma()
	b.FieldStart("photo")
	if s.Photo == nil {
		return fmt.Errorf("unable to encode setUserPersonalProfilePhoto#1baa28f6: field photo is nil")
	}
	if err := s.Photo.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode setUserPersonalProfilePhoto#1baa28f6: field photo: %w", err)
	}
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (s *SetUserPersonalProfilePhotoRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if s == nil {
		return fmt.Errorf("can't decode setUserPersonalProfilePhoto#1baa28f6 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("setUserPersonalProfilePhoto"); err != nil {
				return fmt.Errorf("unable to decode setUserPersonalProfilePhoto#1baa28f6: %w", err)
			}
		case "user_id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode setUserPersonalProfilePhoto#1baa28f6: field user_id: %w", err)
			}
			s.UserID = value
		case "photo":
			value, err := DecodeTDLibJSONInputChatPhoto(b)
			if err != nil {
				return fmt.Errorf("unable to decode setUserPersonalProfilePhoto#1baa28f6: field photo: %w", err)
			}
			s.Photo = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetUserID returns value of UserID field.
func (s *SetUserPersonalProfilePhotoRequest) GetUserID() (value int64) {
	if s == nil {
		return
	}
	return s.UserID
}

// GetPhoto returns value of Photo field.
func (s *SetUserPersonalProfilePhotoRequest) GetPhoto() (value InputChatPhotoClass) {
	if s == nil {
		return
	}
	return s.Photo
}

// SetUserPersonalProfilePhoto invokes method setUserPersonalProfilePhoto#1baa28f6 returning error if any.
func (c *Client) SetUserPersonalProfilePhoto(ctx context.Context, request *SetUserPersonalProfilePhotoRequest) error {
	var ok Ok

	if err := c.rpc.Invoke(ctx, request, &ok); err != nil {
		return err
	}
	return nil
}
