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

// ContactsUnblockRequest represents TL type `contacts.unblock#b550d328`.
// Deletes a peer from a blocklist, see here »¹ for more info.
//
// Links:
//  1. https://core.telegram.org/api/block
//
// See https://core.telegram.org/method/contacts.unblock for reference.
type ContactsUnblockRequest struct {
	// Flags, see TL conditional fields¹
	//
	// Links:
	//  1) https://core.telegram.org/mtproto/TL-combinators#conditional-fields
	Flags bin.Fields
	// Whether the peer should be removed from the story blocklist; if not set, the peer will
	// be removed from the main blocklist, see here »¹ for more info.
	//
	// Links:
	//  1) https://core.telegram.org/api/block
	MyStoriesFrom bool
	// Peer
	ID InputPeerClass
}

// ContactsUnblockRequestTypeID is TL type id of ContactsUnblockRequest.
const ContactsUnblockRequestTypeID = 0xb550d328

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

func (u *ContactsUnblockRequest) Zero() bool {
	if u == nil {
		return true
	}
	if !(u.Flags.Zero()) {
		return false
	}
	if !(u.MyStoriesFrom == false) {
		return false
	}
	if !(u.ID == nil) {
		return false
	}

	return true
}

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

// FillFrom fills ContactsUnblockRequest from given interface.
func (u *ContactsUnblockRequest) FillFrom(from interface {
	GetMyStoriesFrom() (value bool)
	GetID() (value InputPeerClass)
}) {
	u.MyStoriesFrom = from.GetMyStoriesFrom()
	u.ID = from.GetID()
}

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

// TypeName returns name of type in TL schema.
func (*ContactsUnblockRequest) TypeName() string {
	return "contacts.unblock"
}

// TypeInfo returns info about TL type.
func (u *ContactsUnblockRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "contacts.unblock",
		ID:   ContactsUnblockRequestTypeID,
	}
	if u == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "MyStoriesFrom",
			SchemaName: "my_stories_from",
			Null:       !u.Flags.Has(0),
		},
		{
			Name:       "ID",
			SchemaName: "id",
		},
	}
	return typ
}

// SetFlags sets flags for non-zero fields.
func (u *ContactsUnblockRequest) SetFlags() {
	if !(u.MyStoriesFrom == false) {
		u.Flags.Set(0)
	}
}

// Encode implements bin.Encoder.
func (u *ContactsUnblockRequest) Encode(b *bin.Buffer) error {
	if u == nil {
		return fmt.Errorf("can't encode contacts.unblock#b550d328 as nil")
	}
	b.PutID(ContactsUnblockRequestTypeID)
	return u.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (u *ContactsUnblockRequest) EncodeBare(b *bin.Buffer) error {
	if u == nil {
		return fmt.Errorf("can't encode contacts.unblock#b550d328 as nil")
	}
	u.SetFlags()
	if err := u.Flags.Encode(b); err != nil {
		return fmt.Errorf("unable to encode contacts.unblock#b550d328: field flags: %w", err)
	}
	if u.ID == nil {
		return fmt.Errorf("unable to encode contacts.unblock#b550d328: field id is nil")
	}
	if err := u.ID.Encode(b); err != nil {
		return fmt.Errorf("unable to encode contacts.unblock#b550d328: field id: %w", err)
	}
	return nil
}

// Decode implements bin.Decoder.
func (u *ContactsUnblockRequest) Decode(b *bin.Buffer) error {
	if u == nil {
		return fmt.Errorf("can't decode contacts.unblock#b550d328 to nil")
	}
	if err := b.ConsumeID(ContactsUnblockRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode contacts.unblock#b550d328: %w", err)
	}
	return u.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (u *ContactsUnblockRequest) DecodeBare(b *bin.Buffer) error {
	if u == nil {
		return fmt.Errorf("can't decode contacts.unblock#b550d328 to nil")
	}
	{
		if err := u.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode contacts.unblock#b550d328: field flags: %w", err)
		}
	}
	u.MyStoriesFrom = u.Flags.Has(0)
	{
		value, err := DecodeInputPeer(b)
		if err != nil {
			return fmt.Errorf("unable to decode contacts.unblock#b550d328: field id: %w", err)
		}
		u.ID = value
	}
	return nil
}

// SetMyStoriesFrom sets value of MyStoriesFrom conditional field.
func (u *ContactsUnblockRequest) SetMyStoriesFrom(value bool) {
	if value {
		u.Flags.Set(0)
		u.MyStoriesFrom = true
	} else {
		u.Flags.Unset(0)
		u.MyStoriesFrom = false
	}
}

// GetMyStoriesFrom returns value of MyStoriesFrom conditional field.
func (u *ContactsUnblockRequest) GetMyStoriesFrom() (value bool) {
	if u == nil {
		return
	}
	return u.Flags.Has(0)
}

// GetID returns value of ID field.
func (u *ContactsUnblockRequest) GetID() (value InputPeerClass) {
	if u == nil {
		return
	}
	return u.ID
}

// ContactsUnblock invokes method contacts.unblock#b550d328 returning error if any.
// Deletes a peer from a blocklist, see here »¹ for more info.
//
// Links:
//  1. https://core.telegram.org/api/block
//
// Possible errors:
//
//	400 CHANNEL_PRIVATE: You haven't joined this channel/supergroup.
//	400 CONTACT_ID_INVALID: The provided contact ID is invalid.
//	400 MSG_ID_INVALID: Invalid message ID provided.
//	400 PEER_ID_INVALID: The provided peer id is invalid.
//
// See https://core.telegram.org/method/contacts.unblock for reference.
func (c *Client) ContactsUnblock(ctx context.Context, request *ContactsUnblockRequest) (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
}
