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

namespace MyTelegram.Schema;

///<summary>
/// Channel/supergroup participant
/// See <a href="https://corefork.telegram.org/constructor/channelParticipant" />
///</summary>
[TlObject(0xcb397619)]
public sealed class TChannelParticipant : IChannelParticipant
{
    public uint ConstructorId => 0xcb397619;
    ///<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>
    /// Participant user ID
    ///</summary>
    public long UserId { get; set; }

    ///<summary>
    /// Date joined
    ///</summary>
    public int Date { get; set; }

    ///<summary>
    /// If set, contains the expiration date of the current <a href="https://corefork.telegram.org/api/stars#star-subscriptions">Telegram Star subscription period »</a> for the specified participant.
    ///</summary>
    public int? SubscriptionUntilDate { get; set; }

    public void ComputeFlag()
    {
        if (/*SubscriptionUntilDate != 0 && */SubscriptionUntilDate.HasValue) { Flags = Flags.SetBit(0); }
    }

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

    public void Deserialize(ref ReadOnlyMemory<byte> buffer)
    {
        Flags = buffer.ReadInt32();
        UserId = buffer.ReadInt64();
        Date = buffer.ReadInt32();
        if (Flags.IsBitSet(0)) { SubscriptionUntilDate = buffer.ReadInt32(); }
    }
}