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

// DeleteChatBackgroundRequest represents TL type `deleteChatBackground#1316e678`.
type DeleteChatBackgroundRequest struct {
	// Chat identifier
	ChatID int64
	// Pass true to restore previously set background. Can be used only in private and secret
	// chats with non-deleted users if userFullInfo.set_chat_background == true.
	RestorePrevious bool
}

// DeleteChatBackgroundRequestTypeID is TL type id of DeleteChatBackgroundRequest.
const DeleteChatBackgroundRequestTypeID = 0x1316e678

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

func (d *DeleteChatBackgroundRequest) Zero() bool {
	if d == nil {
		return true
	}
	if !(d.ChatID == 0) {
		return false
	}
	if !(d.RestorePrevious == false) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (d *DeleteChatBackgroundRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "deleteChatBackground",
		ID:   DeleteChatBackgroundRequestTypeID,
	}
	if d == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "ChatID",
			SchemaName: "chat_id",
		},
		{
			Name:       "RestorePrevious",
			SchemaName: "restore_previous",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (d *DeleteChatBackgroundRequest) Encode(b *bin.Buffer) error {
	if d == nil {
		return fmt.Errorf("can't encode deleteChatBackground#1316e678 as nil")
	}
	b.PutID(DeleteChatBackgroundRequestTypeID)
	return d.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (d *DeleteChatBackgroundRequest) EncodeBare(b *bin.Buffer) error {
	if d == nil {
		return fmt.Errorf("can't encode deleteChatBackground#1316e678 as nil")
	}
	b.PutInt53(d.ChatID)
	b.PutBool(d.RestorePrevious)
	return nil
}

// Decode implements bin.Decoder.
func (d *DeleteChatBackgroundRequest) Decode(b *bin.Buffer) error {
	if d == nil {
		return fmt.Errorf("can't decode deleteChatBackground#1316e678 to nil")
	}
	if err := b.ConsumeID(DeleteChatBackgroundRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode deleteChatBackground#1316e678: %w", err)
	}
	return d.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (d *DeleteChatBackgroundRequest) DecodeBare(b *bin.Buffer) error {
	if d == nil {
		return fmt.Errorf("can't decode deleteChatBackground#1316e678 to nil")
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode deleteChatBackground#1316e678: field chat_id: %w", err)
		}
		d.ChatID = value
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode deleteChatBackground#1316e678: field restore_previous: %w", err)
		}
		d.RestorePrevious = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (d *DeleteChatBackgroundRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if d == nil {
		return fmt.Errorf("can't encode deleteChatBackground#1316e678 as nil")
	}
	b.ObjStart()
	b.PutID("deleteChatBackground")
	b.Comma()
	b.FieldStart("chat_id")
	b.PutInt53(d.ChatID)
	b.Comma()
	b.FieldStart("restore_previous")
	b.PutBool(d.RestorePrevious)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (d *DeleteChatBackgroundRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if d == nil {
		return fmt.Errorf("can't decode deleteChatBackground#1316e678 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("deleteChatBackground"); err != nil {
				return fmt.Errorf("unable to decode deleteChatBackground#1316e678: %w", err)
			}
		case "chat_id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode deleteChatBackground#1316e678: field chat_id: %w", err)
			}
			d.ChatID = value
		case "restore_previous":
			value, err := b.Bool()
			if err != nil {
				return fmt.Errorf("unable to decode deleteChatBackground#1316e678: field restore_previous: %w", err)
			}
			d.RestorePrevious = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetChatID returns value of ChatID field.
func (d *DeleteChatBackgroundRequest) GetChatID() (value int64) {
	if d == nil {
		return
	}
	return d.ChatID
}

// GetRestorePrevious returns value of RestorePrevious field.
func (d *DeleteChatBackgroundRequest) GetRestorePrevious() (value bool) {
	if d == nil {
		return
	}
	return d.RestorePrevious
}

// DeleteChatBackground invokes method deleteChatBackground#1316e678 returning error if any.
func (c *Client) DeleteChatBackground(ctx context.Context, request *DeleteChatBackgroundRequest) error {
	var ok Ok

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