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

// ReportOption represents TL type `reportOption#41f22c20`.
type ReportOption struct {
	// Unique identifier of the option
	ID []byte
	// Text of the option
	Text string
}

// ReportOptionTypeID is TL type id of ReportOption.
const ReportOptionTypeID = 0x41f22c20

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

func (r *ReportOption) Zero() bool {
	if r == nil {
		return true
	}
	if !(r.ID == nil) {
		return false
	}
	if !(r.Text == "") {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (r *ReportOption) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "reportOption",
		ID:   ReportOptionTypeID,
	}
	if r == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "ID",
			SchemaName: "id",
		},
		{
			Name:       "Text",
			SchemaName: "text",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (r *ReportOption) Encode(b *bin.Buffer) error {
	if r == nil {
		return fmt.Errorf("can't encode reportOption#41f22c20 as nil")
	}
	b.PutID(ReportOptionTypeID)
	return r.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (r *ReportOption) EncodeBare(b *bin.Buffer) error {
	if r == nil {
		return fmt.Errorf("can't encode reportOption#41f22c20 as nil")
	}
	b.PutBytes(r.ID)
	b.PutString(r.Text)
	return nil
}

// Decode implements bin.Decoder.
func (r *ReportOption) Decode(b *bin.Buffer) error {
	if r == nil {
		return fmt.Errorf("can't decode reportOption#41f22c20 to nil")
	}
	if err := b.ConsumeID(ReportOptionTypeID); err != nil {
		return fmt.Errorf("unable to decode reportOption#41f22c20: %w", err)
	}
	return r.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (r *ReportOption) DecodeBare(b *bin.Buffer) error {
	if r == nil {
		return fmt.Errorf("can't decode reportOption#41f22c20 to nil")
	}
	{
		value, err := b.Bytes()
		if err != nil {
			return fmt.Errorf("unable to decode reportOption#41f22c20: field id: %w", err)
		}
		r.ID = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode reportOption#41f22c20: field text: %w", err)
		}
		r.Text = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (r *ReportOption) EncodeTDLibJSON(b tdjson.Encoder) error {
	if r == nil {
		return fmt.Errorf("can't encode reportOption#41f22c20 as nil")
	}
	b.ObjStart()
	b.PutID("reportOption")
	b.Comma()
	b.FieldStart("id")
	b.PutBytes(r.ID)
	b.Comma()
	b.FieldStart("text")
	b.PutString(r.Text)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (r *ReportOption) DecodeTDLibJSON(b tdjson.Decoder) error {
	if r == nil {
		return fmt.Errorf("can't decode reportOption#41f22c20 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("reportOption"); err != nil {
				return fmt.Errorf("unable to decode reportOption#41f22c20: %w", err)
			}
		case "id":
			value, err := b.Bytes()
			if err != nil {
				return fmt.Errorf("unable to decode reportOption#41f22c20: field id: %w", err)
			}
			r.ID = value
		case "text":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode reportOption#41f22c20: field text: %w", err)
			}
			r.Text = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetID returns value of ID field.
func (r *ReportOption) GetID() (value []byte) {
	if r == nil {
		return
	}
	return r.ID
}

// GetText returns value of Text field.
func (r *ReportOption) GetText() (value string) {
	if r == nil {
		return
	}
	return r.Text
}
