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

// MessagesDiscardEncryptionRequest represents TL type `messages.discardEncryption#f393aea0`.
// Cancels a request for creation and/or delete info on secret chat.
//
// See https://core.telegram.org/method/messages.discardEncryption for reference.
type MessagesDiscardEncryptionRequest struct {
	// Flags, see TL conditional fields¹
	//
	// Links:
	//  1) https://core.telegram.org/mtproto/TL-combinators#conditional-fields
	Flags bin.Fields
	// Whether to delete the entire chat history for the other user as well
	DeleteHistory bool
	// Secret chat ID
	ChatID int
}

// MessagesDiscardEncryptionRequestTypeID is TL type id of MessagesDiscardEncryptionRequest.
const MessagesDiscardEncryptionRequestTypeID = 0xf393aea0

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

func (d *MessagesDiscardEncryptionRequest) Zero() bool {
	if d == nil {
		return true
	}
	if !(d.Flags.Zero()) {
		return false
	}
	if !(d.DeleteHistory == false) {
		return false
	}
	if !(d.ChatID == 0) {
		return false
	}

	return true
}

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

// FillFrom fills MessagesDiscardEncryptionRequest from given interface.
func (d *MessagesDiscardEncryptionRequest) FillFrom(from interface {
	GetDeleteHistory() (value bool)
	GetChatID() (value int)
}) {
	d.DeleteHistory = from.GetDeleteHistory()
	d.ChatID = from.GetChatID()
}

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

// TypeName returns name of type in TL schema.
func (*MessagesDiscardEncryptionRequest) TypeName() string {
	return "messages.discardEncryption"
}

// TypeInfo returns info about TL type.
func (d *MessagesDiscardEncryptionRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "messages.discardEncryption",
		ID:   MessagesDiscardEncryptionRequestTypeID,
	}
	if d == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "DeleteHistory",
			SchemaName: "delete_history",
			Null:       !d.Flags.Has(0),
		},
		{
			Name:       "ChatID",
			SchemaName: "chat_id",
		},
	}
	return typ
}

// SetFlags sets flags for non-zero fields.
func (d *MessagesDiscardEncryptionRequest) SetFlags() {
	if !(d.DeleteHistory == false) {
		d.Flags.Set(0)
	}
}

// Encode implements bin.Encoder.
func (d *MessagesDiscardEncryptionRequest) Encode(b *bin.Buffer) error {
	if d == nil {
		return fmt.Errorf("can't encode messages.discardEncryption#f393aea0 as nil")
	}
	b.PutID(MessagesDiscardEncryptionRequestTypeID)
	return d.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (d *MessagesDiscardEncryptionRequest) EncodeBare(b *bin.Buffer) error {
	if d == nil {
		return fmt.Errorf("can't encode messages.discardEncryption#f393aea0 as nil")
	}
	d.SetFlags()
	if err := d.Flags.Encode(b); err != nil {
		return fmt.Errorf("unable to encode messages.discardEncryption#f393aea0: field flags: %w", err)
	}
	b.PutInt(d.ChatID)
	return nil
}

// Decode implements bin.Decoder.
func (d *MessagesDiscardEncryptionRequest) Decode(b *bin.Buffer) error {
	if d == nil {
		return fmt.Errorf("can't decode messages.discardEncryption#f393aea0 to nil")
	}
	if err := b.ConsumeID(MessagesDiscardEncryptionRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode messages.discardEncryption#f393aea0: %w", err)
	}
	return d.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (d *MessagesDiscardEncryptionRequest) DecodeBare(b *bin.Buffer) error {
	if d == nil {
		return fmt.Errorf("can't decode messages.discardEncryption#f393aea0 to nil")
	}
	{
		if err := d.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode messages.discardEncryption#f393aea0: field flags: %w", err)
		}
	}
	d.DeleteHistory = d.Flags.Has(0)
	{
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode messages.discardEncryption#f393aea0: field chat_id: %w", err)
		}
		d.ChatID = value
	}
	return nil
}

// SetDeleteHistory sets value of DeleteHistory conditional field.
func (d *MessagesDiscardEncryptionRequest) SetDeleteHistory(value bool) {
	if value {
		d.Flags.Set(0)
		d.DeleteHistory = true
	} else {
		d.Flags.Unset(0)
		d.DeleteHistory = false
	}
}

// GetDeleteHistory returns value of DeleteHistory conditional field.
func (d *MessagesDiscardEncryptionRequest) GetDeleteHistory() (value bool) {
	if d == nil {
		return
	}
	return d.Flags.Has(0)
}

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

// MessagesDiscardEncryption invokes method messages.discardEncryption#f393aea0 returning error if any.
// Cancels a request for creation and/or delete info on secret chat.
//
// Possible errors:
//
//	400 CHAT_ID_EMPTY: The provided chat ID is empty.
//	400 ENCRYPTION_ALREADY_ACCEPTED: Secret chat already accepted.
//	400 ENCRYPTION_ALREADY_DECLINED: The secret chat was already declined.
//	400 ENCRYPTION_ID_INVALID: The provided secret chat ID is invalid.
//
// See https://core.telegram.org/method/messages.discardEncryption for reference.
func (c *Client) MessagesDiscardEncryption(ctx context.Context, request *MessagesDiscardEncryptionRequest) (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
}
