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

// DisconnectWebsiteRequest represents TL type `disconnectWebsite#d194f3dd`.
type DisconnectWebsiteRequest struct {
	// Website identifier
	WebsiteID int64
}

// DisconnectWebsiteRequestTypeID is TL type id of DisconnectWebsiteRequest.
const DisconnectWebsiteRequestTypeID = 0xd194f3dd

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

func (d *DisconnectWebsiteRequest) Zero() bool {
	if d == nil {
		return true
	}
	if !(d.WebsiteID == 0) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (d *DisconnectWebsiteRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "disconnectWebsite",
		ID:   DisconnectWebsiteRequestTypeID,
	}
	if d == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "WebsiteID",
			SchemaName: "website_id",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (d *DisconnectWebsiteRequest) Encode(b *bin.Buffer) error {
	if d == nil {
		return fmt.Errorf("can't encode disconnectWebsite#d194f3dd as nil")
	}
	b.PutID(DisconnectWebsiteRequestTypeID)
	return d.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (d *DisconnectWebsiteRequest) EncodeBare(b *bin.Buffer) error {
	if d == nil {
		return fmt.Errorf("can't encode disconnectWebsite#d194f3dd as nil")
	}
	b.PutLong(d.WebsiteID)
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (d *DisconnectWebsiteRequest) DecodeBare(b *bin.Buffer) error {
	if d == nil {
		return fmt.Errorf("can't decode disconnectWebsite#d194f3dd to nil")
	}
	{
		value, err := b.Long()
		if err != nil {
			return fmt.Errorf("unable to decode disconnectWebsite#d194f3dd: field website_id: %w", err)
		}
		d.WebsiteID = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (d *DisconnectWebsiteRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if d == nil {
		return fmt.Errorf("can't encode disconnectWebsite#d194f3dd as nil")
	}
	b.ObjStart()
	b.PutID("disconnectWebsite")
	b.Comma()
	b.FieldStart("website_id")
	b.PutLong(d.WebsiteID)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

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

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("disconnectWebsite"); err != nil {
				return fmt.Errorf("unable to decode disconnectWebsite#d194f3dd: %w", err)
			}
		case "website_id":
			value, err := b.Long()
			if err != nil {
				return fmt.Errorf("unable to decode disconnectWebsite#d194f3dd: field website_id: %w", err)
			}
			d.WebsiteID = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetWebsiteID returns value of WebsiteID field.
func (d *DisconnectWebsiteRequest) GetWebsiteID() (value int64) {
	if d == nil {
		return
	}
	return d.WebsiteID
}

// DisconnectWebsite invokes method disconnectWebsite#d194f3dd returning error if any.
func (c *Client) DisconnectWebsite(ctx context.Context, websiteid int64) error {
	var ok Ok

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