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

namespace MyTelegram.Schema;

///<summary>
/// Info about a group.When updating the <a href="https://corefork.telegram.org/api/peers">local peer database</a>, all fields from the newly received constructor take priority over the old constructor cached locally (including by removing fields that aren't set in the new constructor).See <a href="https://github.com/tdlib/td/blob/a24af0992245f838f2b4b418a0a2d5fa9caa27b5/td/telegram/ChatManager.cpp#L5152">here »</a> for an implementation of the logic to use when updating the <a href="https://corefork.telegram.org/api/peers">local user peer database</a>.
/// See <a href="https://corefork.telegram.org/constructor/chat" />
///</summary>
[TlObject(0x41cbf256)]
public sealed class TChat : MyTelegram.Schema.IChat, ILayeredChat
{
    public uint ConstructorId => 0x41cbf256;
    ///<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 the current user is the creator of the group
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool Creator { get; set; }

    ///<summary>
    /// Whether the current user has left the group
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool Left { get; set; }

    ///<summary>
    /// Whether the group was <a href="https://corefork.telegram.org/api/channel">migrated</a>
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool Deactivated { get; set; }

    ///<summary>
    /// Whether a group call is currently active
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool CallActive { get; set; }

    ///<summary>
    /// Whether there's anyone in the group call
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool CallNotEmpty { get; set; }

    ///<summary>
    /// Whether this group is <a href="https://telegram.org/blog/protected-content-delete-by-date-and-more">protected</a>, thus does not allow forwarding messages from it
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool Noforwards { get; set; }

    ///<summary>
    /// ID of the group, see <a href="https://corefork.telegram.org/api/peers#peer-id">here »</a> for more info
    ///</summary>
    public long Id { get; set; }

    ///<summary>
    /// Title
    ///</summary>
    public string Title { get; set; }

    ///<summary>
    /// Chat photo
    /// See <a href="https://corefork.telegram.org/type/ChatPhoto" />
    ///</summary>
    public MyTelegram.Schema.IChatPhoto Photo { get; set; }

    ///<summary>
    /// Participant count
    ///</summary>
    public int ParticipantsCount { get; set; }

    ///<summary>
    /// Date of creation of the group
    ///</summary>
    public int Date { get; set; }

    ///<summary>
    /// Used in basic groups to reorder updates and make sure that all of them were received.
    ///</summary>
    public int Version { get; set; }

    ///<summary>
    /// Means this chat was <a href="https://corefork.telegram.org/api/channel">upgraded</a> to a supergroup
    /// See <a href="https://corefork.telegram.org/type/InputChannel" />
    ///</summary>
    public MyTelegram.Schema.IInputChannel? MigratedTo { get; set; }

    ///<summary>
    /// <a href="https://corefork.telegram.org/api/rights">Admin rights</a> of the user in the group
    /// See <a href="https://corefork.telegram.org/type/ChatAdminRights" />
    ///</summary>
    public MyTelegram.Schema.IChatAdminRights? AdminRights { get; set; }

    ///<summary>
    /// <a href="https://corefork.telegram.org/api/rights">Default banned rights</a> of all users in the group
    /// See <a href="https://corefork.telegram.org/type/ChatBannedRights" />
    ///</summary>
    public MyTelegram.Schema.IChatBannedRights? DefaultBannedRights { get; set; }

    public void ComputeFlag()
    {
        if (Creator) { Flags = Flags.SetBit(0); }
        if (Left) { Flags = Flags.SetBit(2); }
        if (Deactivated) { Flags = Flags.SetBit(5); }
        if (CallActive) { Flags = Flags.SetBit(23); }
        if (CallNotEmpty) { Flags = Flags.SetBit(24); }
        if (Noforwards) { Flags = Flags.SetBit(25); }
        if (MigratedTo != null) { Flags = Flags.SetBit(6); }
        if (AdminRights != null) { Flags = Flags.SetBit(14); }
        if (DefaultBannedRights != null) { Flags = Flags.SetBit(18); }
    }

    public void Serialize(IBufferWriter<byte> writer)
    {
        ComputeFlag();
        writer.Write(ConstructorId);
        writer.Write(Flags);
        writer.Write(Id);
        writer.Write(Title);
        writer.Write(Photo);
        writer.Write(ParticipantsCount);
        writer.Write(Date);
        writer.Write(Version);
        if (Flags.IsBitSet(6)) { writer.Write(MigratedTo); }
        if (Flags.IsBitSet(14)) { writer.Write(AdminRights); }
        if (Flags.IsBitSet(18)) { writer.Write(DefaultBannedRights); }
    }

    public void Deserialize(ref ReadOnlyMemory<byte> buffer)
    {
        Flags = buffer.ReadInt32();
        if (Flags.IsBitSet(0)) { Creator = true; }
        if (Flags.IsBitSet(2)) { Left = true; }
        if (Flags.IsBitSet(5)) { Deactivated = true; }
        if (Flags.IsBitSet(23)) { CallActive = true; }
        if (Flags.IsBitSet(24)) { CallNotEmpty = true; }
        if (Flags.IsBitSet(25)) { Noforwards = true; }
        Id = buffer.ReadInt64();
        Title = buffer.ReadString();
        Photo = buffer.Read<MyTelegram.Schema.IChatPhoto>();
        ParticipantsCount = buffer.ReadInt32();
        Date = buffer.ReadInt32();
        Version = buffer.ReadInt32();
        if (Flags.IsBitSet(6)) { MigratedTo = buffer.Read<MyTelegram.Schema.IInputChannel>(); }
        if (Flags.IsBitSet(14)) { AdminRights = buffer.Read<MyTelegram.Schema.IChatAdminRights>(); }
        if (Flags.IsBitSet(18)) { DefaultBannedRights = buffer.Read<MyTelegram.Schema.IChatBannedRights>(); }
    }
}