// <auto-generated/>
// ReSharper disable All

namespace MyTelegram.Schema;

///<summary>
/// Contains the webview URL with appropriate theme and user info parameters added
/// See <a href="https://corefork.telegram.org/constructor/webViewResultUrl" />
///</summary>
[TlObject(0x4d22ff98)]
public sealed class TWebViewResultUrl : IWebViewResult
{
    public uint ConstructorId => 0x4d22ff98;
    ///<summary>
    /// Flags, see <a href="https://corefork.telegram.org/mtproto/TL-combinators#conditional-fields">TL conditional fields</a>
    ///</summary>
    public int Flags { get; set; }

    ///<summary>
    /// If set, the app must be opened in fullsize mode instead of compact mode.
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool Fullsize { get; set; }

    ///<summary>
    /// &nbsp;
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool Fullscreen { get; set; }

    ///<summary>
    /// Webview session ID (only returned by <a href="https://corefork.telegram.org/api/bots/webapps#inline-button-mini-apps">inline button mini apps</a>, <a href="https://corefork.telegram.org/api/bots/webapps#menu-button-mini-apps">menu button mini apps</a>, <a href="https://corefork.telegram.org/api/bots/webapps#attachment-menu-mini-apps">attachment menu mini apps</a>).
    ///</summary>
    public long? QueryId { get; set; }

    ///<summary>
    /// Webview URL to open
    ///</summary>
    public string Url { get; set; }

    public void ComputeFlag()
    {
        if (Fullsize) { Flags = Flags.SetBit(1); }
        if (Fullscreen) { Flags = Flags.SetBit(2); }
        if (/*QueryId != 0 &&*/ QueryId.HasValue) { Flags = Flags.SetBit(0); }

    }

    public void Serialize(IBufferWriter<byte> writer)
    {
        ComputeFlag();
        writer.Write(ConstructorId);
        writer.Write(Flags);
        if (Flags.IsBitSet(0)) { writer.Write(QueryId.Value); }
        writer.Write(Url);
    }

    public void Deserialize(ref ReadOnlyMemory<byte> buffer)
    {
        Flags = buffer.ReadInt32();
        if (Flags.IsBitSet(1)) { Fullsize = true; }
        if (Flags.IsBitSet(2)) { Fullscreen = true; }
        if (Flags.IsBitSet(0)) { QueryId = buffer.ReadInt64(); }
        Url = buffer.ReadString();
    }
}