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

namespace MyTelegram.Schema.Messages;

///<summary>
/// A set of sponsored messages associated to a channel
/// See <a href="https://corefork.telegram.org/constructor/messages.sponsoredMessages" />
///</summary>
[TlObject(0xffda656d)]
public sealed class TSponsoredMessages : ISponsoredMessages
{
    public uint ConstructorId => 0xffda656d;
    ///<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, specifies the minimum number of messages between shown sponsored messages; otherwise, only one sponsored message must be shown after all ordinary messages.
    ///</summary>
    public int? PostsBetween { get; set; }
    public int? StartDelay { get; set; }
    public int? BetweenDelay { get; set; }

    ///<summary>
    /// Sponsored messages
    ///</summary>
    public TVector<MyTelegram.Schema.ISponsoredMessage> Messages { get; set; }

    ///<summary>
    /// Chats mentioned in the sponsored messages
    ///</summary>
    public TVector<MyTelegram.Schema.IChat> Chats { get; set; }

    ///<summary>
    /// Users mentioned in the sponsored messages
    ///</summary>
    public TVector<MyTelegram.Schema.IUser> Users { get; set; }

    public void ComputeFlag()
    {
        if (/*PostsBetween != 0 && */PostsBetween.HasValue) { Flags = Flags.SetBit(0); }
        if (/*StartDelay != 0 && */StartDelay.HasValue) { Flags = Flags.SetBit(1); }
        if (/*BetweenDelay != 0 && */BetweenDelay.HasValue) { Flags = Flags.SetBit(2); }

    }

    public void Serialize(IBufferWriter<byte> writer)
    {
        ComputeFlag();
        writer.Write(ConstructorId);
        writer.Write(Flags);
        if (Flags.IsBitSet(0)) { writer.Write(PostsBetween.Value); }
        if (Flags.IsBitSet(1)) { writer.Write(StartDelay.Value); }
        if (Flags.IsBitSet(2)) { writer.Write(BetweenDelay.Value); }
        writer.Write(Messages);
        writer.Write(Chats);
        writer.Write(Users);
    }

    public void Deserialize(ref ReadOnlyMemory<byte> buffer)
    {
        Flags = buffer.ReadInt32();
        if (Flags.IsBitSet(0)) { PostsBetween = buffer.ReadInt32(); }
        if (Flags.IsBitSet(1)) { StartDelay = buffer.ReadInt32(); }
        if (Flags.IsBitSet(2)) { BetweenDelay = buffer.ReadInt32(); }
        Messages = buffer.Read<TVector<MyTelegram.Schema.ISponsoredMessage>>();
        Chats = buffer.Read<TVector<MyTelegram.Schema.IChat>>();
        Users = buffer.Read<TVector<MyTelegram.Schema.IUser>>();
    }
}