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

namespace MyTelegram.Schema;

///<summary>
/// Contains info about a <a href="https://corefork.telegram.org/api/business#connected-bots">bot business connection</a>.
/// See <a href="https://corefork.telegram.org/constructor/botBusinessConnection" />
///</summary>
[TlObject(0x8f34b2f5)]
public sealed class TBotBusinessConnection : IBotBusinessConnection
{
    public uint ConstructorId => 0x8f34b2f5;
    ///<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>
    /// Whether this business connection is currently disabled
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool Disabled { get; set; }

    ///<summary>
    /// Business connection ID, used to identify messages coming from the connection and to reply to them as specified <a href="https://corefork.telegram.org/api/business#connected-bots">here »</a>.
    ///</summary>
    public string ConnectionId { get; set; }

    ///<summary>
    /// ID of the user that the bot is connected to via this connection.
    ///</summary>
    public long UserId { get; set; }

    ///<summary>
    /// ID of the datacenter where to send queries wrapped in a <a href="https://corefork.telegram.org/method/invokeWithBusinessConnection">invokeWithBusinessConnection</a> as specified <a href="https://corefork.telegram.org/api/business#connected-bots">here »</a>.
    ///</summary>
    public int DcId { get; set; }

    ///<summary>
    /// When was the connection created.
    ///</summary>
    public int Date { get; set; }
    public MyTelegram.Schema.IBusinessBotRights? Rights { get; set; }

    public void ComputeFlag()
    {
        if (Disabled) { Flags = Flags.SetBit(1); }
        if (Rights != null) { Flags = Flags.SetBit(2); }
    }

    public void Serialize(IBufferWriter<byte> writer)
    {
        ComputeFlag();
        writer.Write(ConstructorId);
        writer.Write(Flags);
        writer.Write(ConnectionId);
        writer.Write(UserId);
        writer.Write(DcId);
        writer.Write(Date);
        if (Flags.IsBitSet(2)) { writer.Write(Rights); }
    }

    public void Deserialize(ref ReadOnlyMemory<byte> buffer)
    {
        Flags = buffer.ReadInt32();
        if (Flags.IsBitSet(1)) { Disabled = true; }
        ConnectionId = buffer.ReadString();
        UserId = buffer.ReadInt64();
        DcId = buffer.ReadInt32();
        Date = buffer.ReadInt32();
        if (Flags.IsBitSet(2)) { Rights = buffer.Read<MyTelegram.Schema.IBusinessBotRights>(); }
    }
}