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

namespace MyTelegram.Schema;

///<summary>
/// Specifies the private chats that a <a href="https://corefork.telegram.org/api/business#connected-bots">connected business bot »</a> may receive messages and interact with.
/// See <a href="https://corefork.telegram.org/constructor/businessBotRecipients" />
///</summary>
[TlObject(0xb88cf373)]
public sealed class TBusinessBotRecipients : IBusinessBotRecipients
{
    public uint ConstructorId => 0xb88cf373;
    ///<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>
    /// Selects all existing private chats.
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool ExistingChats { get; set; }

    ///<summary>
    /// Selects all new private chats.
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool NewChats { get; set; }

    ///<summary>
    /// Selects all private chats with contacts.
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool Contacts { get; set; }

    ///<summary>
    /// Selects all private chats with non-contacts.
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool NonContacts { get; set; }

    ///<summary>
    /// If set, then all private chats <em>except</em> the ones selected by <code>existing_chats</code>, <code>new_chats</code>, <code>contacts</code>, <code>non_contacts</code> and <code>users</code> are chosen. <br>Note that if this flag is set, any values passed in <code>exclude_users</code> will be merged and moved into <code>users</code> by the server, thus <code>exclude_users</code> will always be empty.
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool ExcludeSelected { get; set; }

    ///<summary>
    /// Explicitly selected private chats.
    ///</summary>
    public TVector<long>? Users { get; set; }

    ///<summary>
    /// Identifiers of private chats that are always excluded.
    ///</summary>
    public TVector<long>? ExcludeUsers { get; set; }

    public void ComputeFlag()
    {
        if (ExistingChats) { Flags = Flags.SetBit(0); }
        if (NewChats) { Flags = Flags.SetBit(1); }
        if (Contacts) { Flags = Flags.SetBit(2); }
        if (NonContacts) { Flags = Flags.SetBit(3); }
        if (ExcludeSelected) { Flags = Flags.SetBit(5); }
        if (Users?.Count > 0) { Flags = Flags.SetBit(4); }
        if (ExcludeUsers?.Count > 0) { Flags = Flags.SetBit(6); }
    }

    public void Serialize(IBufferWriter<byte> writer)
    {
        ComputeFlag();
        writer.Write(ConstructorId);
        writer.Write(Flags);
        if (Flags.IsBitSet(4)) { writer.Write(Users); }
        if (Flags.IsBitSet(6)) { writer.Write(ExcludeUsers); }
    }

    public void Deserialize(ref ReadOnlyMemory<byte> buffer)
    {
        Flags = buffer.ReadInt32();
        if (Flags.IsBitSet(0)) { ExistingChats = true; }
        if (Flags.IsBitSet(1)) { NewChats = true; }
        if (Flags.IsBitSet(2)) { Contacts = true; }
        if (Flags.IsBitSet(3)) { NonContacts = true; }
        if (Flags.IsBitSet(5)) { ExcludeSelected = true; }
        if (Flags.IsBitSet(4)) { Users = buffer.Read<TVector<long>>(); }
        if (Flags.IsBitSet(6)) { ExcludeUsers = buffer.Read<TVector<long>>(); }
    }
}