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

namespace MyTelegram.Schema;

///<summary>
/// Exported chat invite
/// See <a href="https://corefork.telegram.org/constructor/chatInviteExported" />
///</summary>
[TlObject(0xa22cbd96)]
public sealed class TChatInviteExported : IExportedChatInvite, ILayeredExportedChatInvite
{
    public uint ConstructorId => 0xa22cbd96;
    ///<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 chat invite was revoked
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool Revoked { get; set; }

    ///<summary>
    /// Whether this chat invite has no expiration
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool Permanent { get; set; }

    ///<summary>
    /// Whether users importing this invite link will have to be approved to join the channel or group
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool RequestNeeded { get; set; }

    ///<summary>
    /// Chat invitation link
    ///</summary>
    public string Link { get; set; }

    ///<summary>
    /// ID of the admin that created this chat invite
    ///</summary>
    public long AdminId { get; set; }

    ///<summary>
    /// When was this chat invite created
    ///</summary>
    public int Date { get; set; }

    ///<summary>
    /// When was this chat invite last modified
    ///</summary>
    public int? StartDate { get; set; }

    ///<summary>
    /// When does this chat invite expire
    ///</summary>
    public int? ExpireDate { get; set; }

    ///<summary>
    /// Maximum number of users that can join using this link
    ///</summary>
    public int? UsageLimit { get; set; }

    ///<summary>
    /// How many users joined using this link
    ///</summary>
    public int? Usage { get; set; }

    ///<summary>
    /// Number of users that have already used this link to join
    ///</summary>
    public int? Requested { get; set; }

    ///<summary>
    /// For <a href="https://corefork.telegram.org/api/stars#star-subscriptions">Telegram Star subscriptions »</a>, contains the number of chat members which have already joined the chat using the link, but have already left due to expiration of their subscription.
    ///</summary>
    public int? SubscriptionExpired { get; set; }

    ///<summary>
    /// Custom description for the invite link, visible only to admins
    ///</summary>
    public string? Title { get; set; }

    ///<summary>
    /// For <a href="https://corefork.telegram.org/api/stars#star-subscriptions">Telegram Star subscriptions »</a>, contains the pricing of the subscription the user must activate to join the private channel.
    /// See <a href="https://corefork.telegram.org/type/StarsSubscriptionPricing" />
    ///</summary>
    public MyTelegram.Schema.IStarsSubscriptionPricing? SubscriptionPricing { get; set; }

    public void ComputeFlag()
    {
        if (Revoked) { Flags = Flags.SetBit(0); }
        if (Permanent) { Flags = Flags.SetBit(5); }
        if (RequestNeeded) { Flags = Flags.SetBit(6); }
        if (/*StartDate != 0 && */StartDate.HasValue) { Flags = Flags.SetBit(4); }
        if (/*ExpireDate != 0 && */ExpireDate.HasValue) { Flags = Flags.SetBit(1); }
        if (/*UsageLimit != 0 && */UsageLimit.HasValue) { Flags = Flags.SetBit(2); }
        if (/*Usage != 0 && */Usage.HasValue) { Flags = Flags.SetBit(3); }
        if (/*Requested != 0 && */Requested.HasValue) { Flags = Flags.SetBit(7); }
        if (/*SubscriptionExpired != 0 && */SubscriptionExpired.HasValue) { Flags = Flags.SetBit(10); }
        if (Title != null) { Flags = Flags.SetBit(8); }
        if (SubscriptionPricing != null) { Flags = Flags.SetBit(9); }
    }

    public void Serialize(IBufferWriter<byte> writer)
    {
        ComputeFlag();
        writer.Write(ConstructorId);
        writer.Write(Flags);
        writer.Write(Link);
        writer.Write(AdminId);
        writer.Write(Date);
        if (Flags.IsBitSet(4)) { writer.Write(StartDate.Value); }
        if (Flags.IsBitSet(1)) { writer.Write(ExpireDate.Value); }
        if (Flags.IsBitSet(2)) { writer.Write(UsageLimit.Value); }
        if (Flags.IsBitSet(3)) { writer.Write(Usage.Value); }
        if (Flags.IsBitSet(7)) { writer.Write(Requested.Value); }
        if (Flags.IsBitSet(10)) { writer.Write(SubscriptionExpired.Value); }
        if (Flags.IsBitSet(8)) { writer.Write(Title); }
        if (Flags.IsBitSet(9)) { writer.Write(SubscriptionPricing); }
    }

    public void Deserialize(ref ReadOnlyMemory<byte> buffer)
    {
        Flags = buffer.ReadInt32();
        if (Flags.IsBitSet(0)) { Revoked = true; }
        if (Flags.IsBitSet(5)) { Permanent = true; }
        if (Flags.IsBitSet(6)) { RequestNeeded = true; }
        Link = buffer.ReadString();
        AdminId = buffer.ReadInt64();
        Date = buffer.ReadInt32();
        if (Flags.IsBitSet(4)) { StartDate = buffer.ReadInt32(); }
        if (Flags.IsBitSet(1)) { ExpireDate = buffer.ReadInt32(); }
        if (Flags.IsBitSet(2)) { UsageLimit = buffer.ReadInt32(); }
        if (Flags.IsBitSet(3)) { Usage = buffer.ReadInt32(); }
        if (Flags.IsBitSet(7)) { Requested = buffer.ReadInt32(); }
        if (Flags.IsBitSet(10)) { SubscriptionExpired = buffer.ReadInt32(); }
        if (Flags.IsBitSet(8)) { Title = buffer.ReadString(); }
        if (Flags.IsBitSet(9)) { SubscriptionPricing = buffer.Read<MyTelegram.Schema.IStarsSubscriptionPricing>(); }
    }
}