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

// FactCheck represents TL type `factCheck#b89bfccf`.
// Represents a fact-check »¹ created by an independent fact-checker.
//
// Links:
//  1. https://core.telegram.org/api/factcheck
//
// See https://core.telegram.org/constructor/factCheck for reference.
type FactCheck struct {
	// Flags, see TL conditional fields¹
	//
	// Links:
	//  1) https://core.telegram.org/mtproto/TL-combinators#conditional-fields
	Flags bin.Fields
	// If set, the country/text fields will not be set, and the fact check must be fetched
	// manually by the client (if it isn't already cached with the key specified in hash)
	// using bundled messages.getFactCheck¹ requests, when the message with the factcheck
	// scrolls into view.
	//
	// Links:
	//  1) https://core.telegram.org/method/messages.getFactCheck
	NeedCheck bool
	// A two-letter ISO 3166-1 alpha-2 country code of the country for which the fact-check
	// should be shown.
	//
	// Use SetCountry and GetCountry helpers.
	Country string
	// The fact-check.
	//
	// Use SetText and GetText helpers.
	Text TextWithEntities
	// Hash used for caching, for more info click here¹
	//
	// Links:
	//  1) https://core.telegram.org/api/offsets#hash-generation
	Hash int64
}

// FactCheckTypeID is TL type id of FactCheck.
const FactCheckTypeID = 0xb89bfccf

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

func (f *FactCheck) Zero() bool {
	if f == nil {
		return true
	}
	if !(f.Flags.Zero()) {
		return false
	}
	if !(f.NeedCheck == false) {
		return false
	}
	if !(f.Country == "") {
		return false
	}
	if !(f.Text.Zero()) {
		return false
	}
	if !(f.Hash == 0) {
		return false
	}

	return true
}

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

// FillFrom fills FactCheck from given interface.
func (f *FactCheck) FillFrom(from interface {
	GetNeedCheck() (value bool)
	GetCountry() (value string, ok bool)
	GetText() (value TextWithEntities, ok bool)
	GetHash() (value int64)
}) {
	f.NeedCheck = from.GetNeedCheck()
	if val, ok := from.GetCountry(); ok {
		f.Country = val
	}

	if val, ok := from.GetText(); ok {
		f.Text = val
	}

	f.Hash = from.GetHash()
}

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

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

// TypeInfo returns info about TL type.
func (f *FactCheck) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "factCheck",
		ID:   FactCheckTypeID,
	}
	if f == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "NeedCheck",
			SchemaName: "need_check",
			Null:       !f.Flags.Has(0),
		},
		{
			Name:       "Country",
			SchemaName: "country",
			Null:       !f.Flags.Has(1),
		},
		{
			Name:       "Text",
			SchemaName: "text",
			Null:       !f.Flags.Has(1),
		},
		{
			Name:       "Hash",
			SchemaName: "hash",
		},
	}
	return typ
}

// SetFlags sets flags for non-zero fields.
func (f *FactCheck) SetFlags() {
	if !(f.NeedCheck == false) {
		f.Flags.Set(0)
	}
	if !(f.Country == "") {
		f.Flags.Set(1)
	}
	if !(f.Text.Zero()) {
		f.Flags.Set(1)
	}
}

// Encode implements bin.Encoder.
func (f *FactCheck) Encode(b *bin.Buffer) error {
	if f == nil {
		return fmt.Errorf("can't encode factCheck#b89bfccf as nil")
	}
	b.PutID(FactCheckTypeID)
	return f.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (f *FactCheck) EncodeBare(b *bin.Buffer) error {
	if f == nil {
		return fmt.Errorf("can't encode factCheck#b89bfccf as nil")
	}
	f.SetFlags()
	if err := f.Flags.Encode(b); err != nil {
		return fmt.Errorf("unable to encode factCheck#b89bfccf: field flags: %w", err)
	}
	if f.Flags.Has(1) {
		b.PutString(f.Country)
	}
	if f.Flags.Has(1) {
		if err := f.Text.Encode(b); err != nil {
			return fmt.Errorf("unable to encode factCheck#b89bfccf: field text: %w", err)
		}
	}
	b.PutLong(f.Hash)
	return nil
}

// Decode implements bin.Decoder.
func (f *FactCheck) Decode(b *bin.Buffer) error {
	if f == nil {
		return fmt.Errorf("can't decode factCheck#b89bfccf to nil")
	}
	if err := b.ConsumeID(FactCheckTypeID); err != nil {
		return fmt.Errorf("unable to decode factCheck#b89bfccf: %w", err)
	}
	return f.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (f *FactCheck) DecodeBare(b *bin.Buffer) error {
	if f == nil {
		return fmt.Errorf("can't decode factCheck#b89bfccf to nil")
	}
	{
		if err := f.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode factCheck#b89bfccf: field flags: %w", err)
		}
	}
	f.NeedCheck = f.Flags.Has(0)
	if f.Flags.Has(1) {
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode factCheck#b89bfccf: field country: %w", err)
		}
		f.Country = value
	}
	if f.Flags.Has(1) {
		if err := f.Text.Decode(b); err != nil {
			return fmt.Errorf("unable to decode factCheck#b89bfccf: field text: %w", err)
		}
	}
	{
		value, err := b.Long()
		if err != nil {
			return fmt.Errorf("unable to decode factCheck#b89bfccf: field hash: %w", err)
		}
		f.Hash = value
	}
	return nil
}

// SetNeedCheck sets value of NeedCheck conditional field.
func (f *FactCheck) SetNeedCheck(value bool) {
	if value {
		f.Flags.Set(0)
		f.NeedCheck = true
	} else {
		f.Flags.Unset(0)
		f.NeedCheck = false
	}
}

// GetNeedCheck returns value of NeedCheck conditional field.
func (f *FactCheck) GetNeedCheck() (value bool) {
	if f == nil {
		return
	}
	return f.Flags.Has(0)
}

// SetCountry sets value of Country conditional field.
func (f *FactCheck) SetCountry(value string) {
	f.Flags.Set(1)
	f.Country = value
}

// GetCountry returns value of Country conditional field and
// boolean which is true if field was set.
func (f *FactCheck) GetCountry() (value string, ok bool) {
	if f == nil {
		return
	}
	if !f.Flags.Has(1) {
		return value, false
	}
	return f.Country, true
}

// SetText sets value of Text conditional field.
func (f *FactCheck) SetText(value TextWithEntities) {
	f.Flags.Set(1)
	f.Text = value
}

// GetText returns value of Text conditional field and
// boolean which is true if field was set.
func (f *FactCheck) GetText() (value TextWithEntities, ok bool) {
	if f == nil {
		return
	}
	if !f.Flags.Has(1) {
		return value, false
	}
	return f.Text, true
}

// GetHash returns value of Hash field.
func (f *FactCheck) GetHash() (value int64) {
	if f == nil {
		return
	}
	return f.Hash
}
