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

// StoryArea represents TL type `storyArea#c9ff075e`.
type StoryArea struct {
	// Position of the area
	Position StoryAreaPosition
	// Type of the area
	Type StoryAreaTypeClass
}

// StoryAreaTypeID is TL type id of StoryArea.
const StoryAreaTypeID = 0xc9ff075e

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

func (s *StoryArea) Zero() bool {
	if s == nil {
		return true
	}
	if !(s.Position.Zero()) {
		return false
	}
	if !(s.Type == nil) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (s *StoryArea) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "storyArea",
		ID:   StoryAreaTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Position",
			SchemaName: "position",
		},
		{
			Name:       "Type",
			SchemaName: "type",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (s *StoryArea) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode storyArea#c9ff075e as nil")
	}
	b.PutID(StoryAreaTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *StoryArea) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode storyArea#c9ff075e as nil")
	}
	if err := s.Position.Encode(b); err != nil {
		return fmt.Errorf("unable to encode storyArea#c9ff075e: field position: %w", err)
	}
	if s.Type == nil {
		return fmt.Errorf("unable to encode storyArea#c9ff075e: field type is nil")
	}
	if err := s.Type.Encode(b); err != nil {
		return fmt.Errorf("unable to encode storyArea#c9ff075e: field type: %w", err)
	}
	return nil
}

// Decode implements bin.Decoder.
func (s *StoryArea) Decode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode storyArea#c9ff075e to nil")
	}
	if err := b.ConsumeID(StoryAreaTypeID); err != nil {
		return fmt.Errorf("unable to decode storyArea#c9ff075e: %w", err)
	}
	return s.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (s *StoryArea) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode storyArea#c9ff075e to nil")
	}
	{
		if err := s.Position.Decode(b); err != nil {
			return fmt.Errorf("unable to decode storyArea#c9ff075e: field position: %w", err)
		}
	}
	{
		value, err := DecodeStoryAreaType(b)
		if err != nil {
			return fmt.Errorf("unable to decode storyArea#c9ff075e: field type: %w", err)
		}
		s.Type = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (s *StoryArea) EncodeTDLibJSON(b tdjson.Encoder) error {
	if s == nil {
		return fmt.Errorf("can't encode storyArea#c9ff075e as nil")
	}
	b.ObjStart()
	b.PutID("storyArea")
	b.Comma()
	b.FieldStart("position")
	if err := s.Position.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode storyArea#c9ff075e: field position: %w", err)
	}
	b.Comma()
	b.FieldStart("type")
	if s.Type == nil {
		return fmt.Errorf("unable to encode storyArea#c9ff075e: field type is nil")
	}
	if err := s.Type.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode storyArea#c9ff075e: field type: %w", err)
	}
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (s *StoryArea) DecodeTDLibJSON(b tdjson.Decoder) error {
	if s == nil {
		return fmt.Errorf("can't decode storyArea#c9ff075e to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("storyArea"); err != nil {
				return fmt.Errorf("unable to decode storyArea#c9ff075e: %w", err)
			}
		case "position":
			if err := s.Position.DecodeTDLibJSON(b); err != nil {
				return fmt.Errorf("unable to decode storyArea#c9ff075e: field position: %w", err)
			}
		case "type":
			value, err := DecodeTDLibJSONStoryAreaType(b)
			if err != nil {
				return fmt.Errorf("unable to decode storyArea#c9ff075e: field type: %w", err)
			}
			s.Type = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetPosition returns value of Position field.
func (s *StoryArea) GetPosition() (value StoryAreaPosition) {
	if s == nil {
		return
	}
	return s.Position
}

// GetType returns value of Type field.
func (s *StoryArea) GetType() (value StoryAreaTypeClass) {
	if s == nil {
		return
	}
	return s.Type
}
