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

// MainWebApp represents TL type `mainWebApp#73a7ac7a`.
type MainWebApp struct {
	// URL of the Web App to open
	URL string
	// The mode in which the Web App must be opened
	Mode WebAppOpenModeClass
}

// MainWebAppTypeID is TL type id of MainWebApp.
const MainWebAppTypeID = 0x73a7ac7a

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

func (m *MainWebApp) Zero() bool {
	if m == nil {
		return true
	}
	if !(m.URL == "") {
		return false
	}
	if !(m.Mode == nil) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (m *MainWebApp) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "mainWebApp",
		ID:   MainWebAppTypeID,
	}
	if m == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "URL",
			SchemaName: "url",
		},
		{
			Name:       "Mode",
			SchemaName: "mode",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (m *MainWebApp) Encode(b *bin.Buffer) error {
	if m == nil {
		return fmt.Errorf("can't encode mainWebApp#73a7ac7a as nil")
	}
	b.PutID(MainWebAppTypeID)
	return m.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (m *MainWebApp) EncodeBare(b *bin.Buffer) error {
	if m == nil {
		return fmt.Errorf("can't encode mainWebApp#73a7ac7a as nil")
	}
	b.PutString(m.URL)
	if m.Mode == nil {
		return fmt.Errorf("unable to encode mainWebApp#73a7ac7a: field mode is nil")
	}
	if err := m.Mode.Encode(b); err != nil {
		return fmt.Errorf("unable to encode mainWebApp#73a7ac7a: field mode: %w", err)
	}
	return nil
}

// Decode implements bin.Decoder.
func (m *MainWebApp) Decode(b *bin.Buffer) error {
	if m == nil {
		return fmt.Errorf("can't decode mainWebApp#73a7ac7a to nil")
	}
	if err := b.ConsumeID(MainWebAppTypeID); err != nil {
		return fmt.Errorf("unable to decode mainWebApp#73a7ac7a: %w", err)
	}
	return m.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (m *MainWebApp) DecodeBare(b *bin.Buffer) error {
	if m == nil {
		return fmt.Errorf("can't decode mainWebApp#73a7ac7a to nil")
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode mainWebApp#73a7ac7a: field url: %w", err)
		}
		m.URL = value
	}
	{
		value, err := DecodeWebAppOpenMode(b)
		if err != nil {
			return fmt.Errorf("unable to decode mainWebApp#73a7ac7a: field mode: %w", err)
		}
		m.Mode = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (m *MainWebApp) EncodeTDLibJSON(b tdjson.Encoder) error {
	if m == nil {
		return fmt.Errorf("can't encode mainWebApp#73a7ac7a as nil")
	}
	b.ObjStart()
	b.PutID("mainWebApp")
	b.Comma()
	b.FieldStart("url")
	b.PutString(m.URL)
	b.Comma()
	b.FieldStart("mode")
	if m.Mode == nil {
		return fmt.Errorf("unable to encode mainWebApp#73a7ac7a: field mode is nil")
	}
	if err := m.Mode.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode mainWebApp#73a7ac7a: field mode: %w", err)
	}
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (m *MainWebApp) DecodeTDLibJSON(b tdjson.Decoder) error {
	if m == nil {
		return fmt.Errorf("can't decode mainWebApp#73a7ac7a to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("mainWebApp"); err != nil {
				return fmt.Errorf("unable to decode mainWebApp#73a7ac7a: %w", err)
			}
		case "url":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode mainWebApp#73a7ac7a: field url: %w", err)
			}
			m.URL = value
		case "mode":
			value, err := DecodeTDLibJSONWebAppOpenMode(b)
			if err != nil {
				return fmt.Errorf("unable to decode mainWebApp#73a7ac7a: field mode: %w", err)
			}
			m.Mode = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetURL returns value of URL field.
func (m *MainWebApp) GetURL() (value string) {
	if m == nil {
		return
	}
	return m.URL
}

// GetMode returns value of Mode field.
func (m *MainWebApp) GetMode() (value WebAppOpenModeClass) {
	if m == nil {
		return
	}
	return m.Mode
}
