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

namespace MyTelegram.Schema.Payments;

///<summary>
/// A <a href="https://corefork.telegram.org/api/giveaways">giveaway</a> has ended.
/// See <a href="https://corefork.telegram.org/constructor/payments.giveawayInfoResults" />
///</summary>
[TlObject(0xe175e66f)]
public sealed class TGiveawayInfoResults : IGiveawayInfo
{
    public uint ConstructorId => 0xe175e66f;
    ///<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 we're one of the winners of this giveaway.
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool Winner { get; set; }

    ///<summary>
    /// Whether the giveaway was canceled and was fully refunded.
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool Refunded { get; set; }

    ///<summary>
    /// Start date of the giveaway
    ///</summary>
    public int StartDate { get; set; }

    ///<summary>
    /// If we're one of the winners of this giveaway, contains the <a href="https://corefork.telegram.org/api/links#premium-giftcode-links">Premium gift code</a>, see <a href="https://corefork.telegram.org/api/giveaways">here »</a> for more info on the full giveaway flow.
    ///</summary>
    public string? GiftCodeSlug { get; set; }

    ///<summary>
    /// If we're one of the winners of this <a href="https://corefork.telegram.org/api/giveaways#star-giveaways">Telegram Star giveaway</a>, the number <a href="https://corefork.telegram.org/api/stars">Telegram Stars</a> we won.
    ///</summary>
    public long? StarsPrize { get; set; }

    ///<summary>
    /// End date of the giveaway. May be bigger than the end date specified in parameters of the giveaway.
    ///</summary>
    public int FinishDate { get; set; }

    ///<summary>
    /// Number of winners in the giveaway
    ///</summary>
    public int WinnersCount { get; set; }

    ///<summary>
    /// Number of winners, which activated their <a href="https://corefork.telegram.org/api/links#premium-giftcode-links">gift codes</a>.
    ///</summary>
    public int? ActivatedCount { get; set; }

    public void ComputeFlag()
    {
        if (Winner) { Flags = Flags.SetBit(0); }
        if (Refunded) { Flags = Flags.SetBit(1); }
        if (GiftCodeSlug != null) { Flags = Flags.SetBit(3); }
        if (/*StarsPrize != 0 &&*/ StarsPrize.HasValue) { Flags = Flags.SetBit(4); }
        if (/*ActivatedCount != 0 && */ActivatedCount.HasValue) { Flags = Flags.SetBit(2); }
    }

    public void Serialize(IBufferWriter<byte> writer)
    {
        ComputeFlag();
        writer.Write(ConstructorId);
        writer.Write(Flags);
        writer.Write(StartDate);
        if (Flags.IsBitSet(3)) { writer.Write(GiftCodeSlug); }
        if (Flags.IsBitSet(4)) { writer.Write(StarsPrize.Value); }
        writer.Write(FinishDate);
        writer.Write(WinnersCount);
        if (Flags.IsBitSet(2)) { writer.Write(ActivatedCount.Value); }
    }

    public void Deserialize(ref ReadOnlyMemory<byte> buffer)
    {
        Flags = buffer.ReadInt32();
        if (Flags.IsBitSet(0)) { Winner = true; }
        if (Flags.IsBitSet(1)) { Refunded = true; }
        StartDate = buffer.ReadInt32();
        if (Flags.IsBitSet(3)) { GiftCodeSlug = buffer.ReadString(); }
        if (Flags.IsBitSet(4)) { StarsPrize = buffer.ReadInt64(); }
        FinishDate = buffer.ReadInt32();
        WinnersCount = buffer.ReadInt32();
        if (Flags.IsBitSet(2)) { ActivatedCount = buffer.ReadInt32(); }
    }
}