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

namespace MyTelegram.Schema.Messages;

///<summary>
/// Edit an exported chat invite
/// <para>Possible errors</para>
/// Code Type Description
/// 400 CHANNEL_PRIVATE You haven't joined this channel/supergroup.
/// 400 CHAT_ADMIN_REQUIRED You must be an admin in this chat to do this.
/// 400 CHAT_INVITE_PERMANENT You can't set an expiration date on permanent invite links.
/// 403 CHAT_WRITE_FORBIDDEN You can't write in this chat.
/// 403 EDIT_BOT_INVITE_FORBIDDEN Normal users can't edit invites that were created by bots.
/// 400 INVITE_HASH_EXPIRED The invite link has expired.
/// 400 PEER_ID_INVALID The provided peer id is invalid.
/// 400 USAGE_LIMIT_INVALID The specified usage limit is invalid.
/// See <a href="https://corefork.telegram.org/method/messages.editExportedChatInvite" />
///</summary>
[TlObject(0xbdca2f75)]
public sealed class RequestEditExportedChatInvite : IRequest<MyTelegram.Schema.Messages.IExportedChatInvite>
{
    public uint ConstructorId => 0xbdca2f75;
    ///<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 to revoke the chat invite
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool Revoked { get; set; }

    ///<summary>
    /// Chat
    /// See <a href="https://corefork.telegram.org/type/InputPeer" />
    ///</summary>
    public MyTelegram.Schema.IInputPeer Peer { get; set; }

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

    ///<summary>
    /// New expiration date
    ///</summary>
    public int? ExpireDate { get; set; }

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

    ///<summary>
    /// Whether admin confirmation is required before admitting each separate user into the chat
    /// See <a href="https://corefork.telegram.org/type/Bool" />
    ///</summary>
    public bool? RequestNeeded { get; set; }

    ///<summary>
    /// Description of the invite link, visible only to administrators
    ///</summary>
    public string? Title { get; set; }

    public void ComputeFlag()
    {
        if (Revoked) { Flags = Flags.SetBit(2); }
        if (/*ExpireDate != 0 && */ExpireDate.HasValue) { Flags = Flags.SetBit(0); }
        if (/*UsageLimit != 0 && */UsageLimit.HasValue) { Flags = Flags.SetBit(1); }
        if (RequestNeeded !=null) { Flags = Flags.SetBit(3); }
        if (Title != null) { Flags = Flags.SetBit(4); }
    }

    public void Serialize(IBufferWriter<byte> writer)
    {
        ComputeFlag();
        writer.Write(ConstructorId);
        writer.Write(Flags);
        writer.Write(Peer);
        writer.Write(Link);
        if (Flags.IsBitSet(0)) { writer.Write(ExpireDate.Value); }
        if (Flags.IsBitSet(1)) { writer.Write(UsageLimit.Value); }
        if (Flags.IsBitSet(3)) { writer.Write(RequestNeeded.Value); }
        if (Flags.IsBitSet(4)) { writer.Write(Title); }
    }

    public void Deserialize(ref ReadOnlyMemory<byte> buffer)
    {
        Flags = buffer.ReadInt32();
        if (Flags.IsBitSet(2)) { Revoked = true; }
        Peer = buffer.Read<MyTelegram.Schema.IInputPeer>();
        Link = buffer.ReadString();
        if (Flags.IsBitSet(0)) { ExpireDate = buffer.ReadInt32(); }
        if (Flags.IsBitSet(1)) { UsageLimit = buffer.ReadInt32(); }
        if (Flags.IsBitSet(3)) { RequestNeeded = buffer.Read(); }
        if (Flags.IsBitSet(4)) { Title = buffer.ReadString(); }
    }
}
