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

namespace MyTelegram.Schema;

///<summary>
/// A <a href="https://corefork.telegram.org/api/sponsored-messages">sponsored message</a>.
/// See <a href="https://corefork.telegram.org/constructor/sponsoredMessage" />
///</summary>
[TlObject(0x7dbf8673)]
public sealed class TSponsoredMessage : ISponsoredMessage
{
    public uint ConstructorId => 0x7dbf8673;
    ///<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 message needs to be labeled as "recommended" instead of "sponsored"
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool Recommended { get; set; }

    ///<summary>
    /// Whether this message can be <a href="https://corefork.telegram.org/api/sponsored-messages#reporting-sponsored-messages">reported as specified here »</a>.
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool CanReport { get; set; }

    ///<summary>
    /// Message ID
    ///</summary>
    public ReadOnlyMemory<byte> RandomId { get; set; }

    ///<summary>
    /// Contains the URL to open when the user clicks on the sponsored message.
    ///</summary>
    public string Url { get; set; }

    ///<summary>
    /// Contains the title of the sponsored message.
    ///</summary>
    public string Title { get; set; }

    ///<summary>
    /// Sponsored message
    ///</summary>
    public string Message { get; set; }

    ///<summary>
    /// <a href="https://corefork.telegram.org/api/entities">Message entities for styled text</a> in <code>message</code>.
    ///</summary>
    public TVector<MyTelegram.Schema.IMessageEntity>? Entities { get; set; }

    ///<summary>
    /// If set, contains a custom profile photo bubble that should be displayed for the sponsored message, like for messages sent in groups.
    /// See <a href="https://corefork.telegram.org/type/Photo" />
    ///</summary>
    public MyTelegram.Schema.IPhoto? Photo { get; set; }

    ///<summary>
    /// If set, contains some media.
    /// See <a href="https://corefork.telegram.org/type/MessageMedia" />
    ///</summary>
    public MyTelegram.Schema.IMessageMedia? Media { get; set; }

    ///<summary>
    /// If set, the sponsored message should use the <a href="https://corefork.telegram.org/api/colors">message accent color »</a> specified in <code>color</code>.
    /// See <a href="https://corefork.telegram.org/type/PeerColor" />
    ///</summary>
    public MyTelegram.Schema.IPeerColor? Color { get; set; }

    ///<summary>
    /// Label of the sponsored message button.
    ///</summary>
    public string ButtonText { get; set; }

    ///<summary>
    /// If set, contains additional information about the sponsor to be shown along with the message.
    ///</summary>
    public string? SponsorInfo { get; set; }

    ///<summary>
    /// If set, contains additional information about the sponsored message to be shown along with the message.
    ///</summary>
    public string? AdditionalInfo { get; set; }
    public int? MinDisplayDuration { get; set; }
    public int? MaxDisplayDuration { get; set; }

    public void ComputeFlag()
    {
        if (Recommended) { Flags = Flags.SetBit(5); }
        if (CanReport) { Flags = Flags.SetBit(12); }
        if (Entities?.Count > 0) { Flags = Flags.SetBit(1); }
        if (Photo != null) { Flags = Flags.SetBit(6); }
        if (Media != null) { Flags = Flags.SetBit(14); }
        if (Color != null) { Flags = Flags.SetBit(13); }
        if (SponsorInfo != null) { Flags = Flags.SetBit(7); }
        if (AdditionalInfo != null) { Flags = Flags.SetBit(8); }
        if (/*MinDisplayDuration != 0 && */MinDisplayDuration.HasValue) { Flags = Flags.SetBit(15); }
        if (/*MaxDisplayDuration != 0 && */MaxDisplayDuration.HasValue) { Flags = Flags.SetBit(15); }
    }

    public void Serialize(IBufferWriter<byte> writer)
    {
        ComputeFlag();
        writer.Write(ConstructorId);
        writer.Write(Flags);
        writer.Write(RandomId);
        writer.Write(Url);
        writer.Write(Title);
        writer.Write(Message);
        if (Flags.IsBitSet(1)) { writer.Write(Entities); }
        if (Flags.IsBitSet(6)) { writer.Write(Photo); }
        if (Flags.IsBitSet(14)) { writer.Write(Media); }
        if (Flags.IsBitSet(13)) { writer.Write(Color); }
        writer.Write(ButtonText);
        if (Flags.IsBitSet(7)) { writer.Write(SponsorInfo); }
        if (Flags.IsBitSet(8)) { writer.Write(AdditionalInfo); }
        if (Flags.IsBitSet(15)) { writer.Write(MinDisplayDuration.Value); }
        if (Flags.IsBitSet(15)) { writer.Write(MaxDisplayDuration.Value); }
    }

    public void Deserialize(ref ReadOnlyMemory<byte> buffer)
    {
        Flags = buffer.ReadInt32();
        if (Flags.IsBitSet(5)) { Recommended = true; }
        if (Flags.IsBitSet(12)) { CanReport = true; }
        RandomId = buffer.ReadBytes();
        Url = buffer.ReadString();
        Title = buffer.ReadString();
        Message = buffer.ReadString();
        if (Flags.IsBitSet(1)) { Entities = buffer.Read<TVector<MyTelegram.Schema.IMessageEntity>>(); }
        if (Flags.IsBitSet(6)) { Photo = buffer.Read<MyTelegram.Schema.IPhoto>(); }
        if (Flags.IsBitSet(14)) { Media = buffer.Read<MyTelegram.Schema.IMessageMedia>(); }
        if (Flags.IsBitSet(13)) { Color = buffer.Read<MyTelegram.Schema.IPeerColor>(); }
        ButtonText = buffer.ReadString();
        if (Flags.IsBitSet(7)) { SponsorInfo = buffer.ReadString(); }
        if (Flags.IsBitSet(8)) { AdditionalInfo = buffer.ReadString(); }
        if (Flags.IsBitSet(15)) { MinDisplayDuration = buffer.ReadInt32(); }
        if (Flags.IsBitSet(15)) { MaxDisplayDuration = buffer.ReadInt32(); }
    }
}