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

namespace MyTelegram.Schema;

///<summary>
/// You received a <a href="https://corefork.telegram.org/api/gifts">gift, see here »</a> for more info.
/// See <a href="https://corefork.telegram.org/constructor/messageActionStarGift" />
///</summary>
[TlObject(0x4717e8a4)]
public sealed class TMessageActionStarGift : IMessageAction
{
    public uint ConstructorId => 0x4717e8a4;
    ///<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>
    /// If set, the name of the sender of the gift will be hidden if the destination user decides to display the gift on their profile
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool NameHidden { get; set; }

    ///<summary>
    /// Whether this gift was added to the destination user's profile (may be toggled using <a href="https://corefork.telegram.org/method/payments.saveStarGift">payments.saveStarGift</a> and fetched using <a href="https://corefork.telegram.org/method/payments.getUserStarGifts">payments.getUserStarGifts</a>)
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool Saved { get; set; }

    ///<summary>
    /// Whether this gift was converted to <a href="https://corefork.telegram.org/api/stars">Telegram Stars</a> and cannot be displayed on the profile anymore.
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool Converted { get; set; }
    public bool Upgraded { get; set; }
    public bool Refunded { get; set; }
    public bool CanUpgrade { get; set; }

    ///<summary>
    /// Info about the gift
    /// See <a href="https://corefork.telegram.org/type/StarGift" />
    ///</summary>
    public MyTelegram.Schema.IStarGift Gift { get; set; }

    ///<summary>
    /// Additional message from the sender of the gift
    /// See <a href="https://corefork.telegram.org/type/TextWithEntities" />
    ///</summary>
    public MyTelegram.Schema.ITextWithEntities? Message { get; set; }

    ///<summary>
    /// The receiver of this gift may convert it to this many Telegram Stars, instead of displaying it on their profile page.<br><code>convert_stars</code> will be equal to <code>stars</code> only if the gift was bought using recently bought Telegram Stars, otherwise it will be less than <code>stars</code>.
    ///</summary>
    public long? ConvertStars { get; set; }
    public int? UpgradeMsgId { get; set; }
    public long? UpgradeStars { get; set; }
    public MyTelegram.Schema.IPeer? FromId { get; set; }
    public MyTelegram.Schema.IPeer? Peer { get; set; }
    public long? SavedId { get; set; }

    public void ComputeFlag()
    {
        if (NameHidden) { Flags = Flags.SetBit(0); }
        if (Saved) { Flags = Flags.SetBit(2); }
        if (Converted) { Flags = Flags.SetBit(3); }
        if (Upgraded) { Flags = Flags.SetBit(5); }
        if (Refunded) { Flags = Flags.SetBit(9); }
        if (CanUpgrade) { Flags = Flags.SetBit(10); }
        if (Message != null) { Flags = Flags.SetBit(1); }
        if (/*ConvertStars != 0 &&*/ ConvertStars.HasValue) { Flags = Flags.SetBit(4); }
        if (/*UpgradeMsgId != 0 && */UpgradeMsgId.HasValue) { Flags = Flags.SetBit(5); }
        if (/*UpgradeStars != 0 &&*/ UpgradeStars.HasValue) { Flags = Flags.SetBit(8); }
        if (FromId != null) { Flags = Flags.SetBit(11); }
        if (Peer != null) { Flags = Flags.SetBit(12); }
        if (/*SavedId != 0 &&*/ SavedId.HasValue) { Flags = Flags.SetBit(12); }
    }

    public void Serialize(IBufferWriter<byte> writer)
    {
        ComputeFlag();
        writer.Write(ConstructorId);
        writer.Write(Flags);
        writer.Write(Gift);
        if (Flags.IsBitSet(1)) { writer.Write(Message); }
        if (Flags.IsBitSet(4)) { writer.Write(ConvertStars ?? 0); }
        if (Flags.IsBitSet(5)) { writer.Write(UpgradeMsgId ?? 0); }
        if (Flags.IsBitSet(8)) { writer.Write(UpgradeStars ?? 0); }
        if (Flags.IsBitSet(11)) { writer.Write(FromId); }
        if (Flags.IsBitSet(12)) { writer.Write(Peer); }
        if (Flags.IsBitSet(12)) { writer.Write(SavedId ?? 0); }
    }

    public void Deserialize(ref ReadOnlyMemory<byte> buffer)
    {
        Flags = buffer.ReadInt32();
        if (Flags.IsBitSet(0)) { NameHidden = true; }
        if (Flags.IsBitSet(2)) { Saved = true; }
        if (Flags.IsBitSet(3)) { Converted = true; }
        if (Flags.IsBitSet(5)) { Upgraded = true; }
        if (Flags.IsBitSet(9)) { Refunded = true; }
        if (Flags.IsBitSet(10)) { CanUpgrade = true; }
        Gift = buffer.Read<MyTelegram.Schema.IStarGift>();
        if (Flags.IsBitSet(1)) { Message = buffer.Read<MyTelegram.Schema.ITextWithEntities>(); }
        if (Flags.IsBitSet(4)) { ConvertStars = buffer.ReadInt64(); }
        if (Flags.IsBitSet(5)) { UpgradeMsgId = buffer.ReadInt32(); }
        if (Flags.IsBitSet(8)) { UpgradeStars = buffer.ReadInt64(); }
        if (Flags.IsBitSet(11)) { FromId = buffer.Read<MyTelegram.Schema.IPeer>(); }
        if (Flags.IsBitSet(12)) { Peer = buffer.Read<MyTelegram.Schema.IPeer>(); }
        if (Flags.IsBitSet(12)) { SavedId = buffer.ReadInt64(); }
    }
}