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

namespace MyTelegram.Schema;

///<summary>
/// Contains info about a <a href="https://corefork.telegram.org/api/giveaways">giveaway, see here »</a> for more info.
/// See <a href="https://corefork.telegram.org/constructor/messageMediaGiveaway" />
///</summary>
[TlObject(0xaa073beb)]
public sealed class TMessageMediaGiveaway : IMessageMedia
{
    public uint ConstructorId => 0xaa073beb;
    ///<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, only new subscribers starting from the giveaway creation date will be able to participate to the giveaway.
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool OnlyNewSubscribers { get; set; }

    ///<summary>
    /// If set, giveaway winners are public and will be listed in a <a href="https://corefork.telegram.org/constructor/messageMediaGiveawayResults">messageMediaGiveawayResults</a> message that will be automatically sent to the channel once the giveaway ends.
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool WinnersAreVisible { get; set; }

    ///<summary>
    /// The channels that the user must join to participate in the giveaway.
    ///</summary>
    public TVector<long> Channels { get; set; }

    ///<summary>
    /// If set, only users residing in these countries can participate in the giveaway, (specified as a list of two-letter ISO 3166-1 alpha-2 country codes); otherwise there are no country-based limitations.
    ///</summary>
    public TVector<string>? CountriesIso2 { get; set; }

    ///<summary>
    /// Can contain a textual description of additional giveaway prizes.
    ///</summary>
    public string? PrizeDescription { get; set; }

    ///<summary>
    /// Number of <a href="https://corefork.telegram.org/api/premium">Telegram Premium</a> subscriptions given away.
    ///</summary>
    public int Quantity { get; set; }

    ///<summary>
    /// Duration in months of each <a href="https://corefork.telegram.org/api/premium">Telegram Premium</a> subscription in the giveaway.
    ///</summary>
    public int? Months { get; set; }

    ///<summary>
    /// For <a href="https://corefork.telegram.org/api/stars#star-giveaways">Telegram Star giveaways</a>, the total number of Telegram Stars being given away.
    ///</summary>
    public long? Stars { get; set; }

    ///<summary>
    /// The end date of the giveaway.
    ///</summary>
    public int UntilDate { get; set; }

    public void ComputeFlag()
    {
        if (OnlyNewSubscribers) { Flags = Flags.SetBit(0); }
        if (WinnersAreVisible) { Flags = Flags.SetBit(2); }
        if (CountriesIso2?.Count > 0) { Flags = Flags.SetBit(1); }
        if (PrizeDescription != null) { Flags = Flags.SetBit(3); }
        if (/*Months != 0 && */Months.HasValue) { Flags = Flags.SetBit(4); }
        if (/*Stars != 0 &&*/ Stars.HasValue) { Flags = Flags.SetBit(5); }

    }

    public void Serialize(IBufferWriter<byte> writer)
    {
        ComputeFlag();
        writer.Write(ConstructorId);
        writer.Write(Flags);
        writer.Write(Channels);
        if (Flags.IsBitSet(1)) { writer.Write(CountriesIso2); }
        if (Flags.IsBitSet(3)) { writer.Write(PrizeDescription); }
        writer.Write(Quantity);
        if (Flags.IsBitSet(4)) { writer.Write(Months.Value); }
        if (Flags.IsBitSet(5)) { writer.Write(Stars.Value); }
        writer.Write(UntilDate);
    }

    public void Deserialize(ref ReadOnlyMemory<byte> buffer)
    {
        Flags = buffer.ReadInt32();
        if (Flags.IsBitSet(0)) { OnlyNewSubscribers = true; }
        if (Flags.IsBitSet(2)) { WinnersAreVisible = true; }
        Channels = buffer.Read<TVector<long>>();
        if (Flags.IsBitSet(1)) { CountriesIso2 = buffer.Read<TVector<string>>(); }
        if (Flags.IsBitSet(3)) { PrizeDescription = buffer.ReadString(); }
        Quantity = buffer.ReadInt32();
        if (Flags.IsBitSet(4)) { Months = buffer.ReadInt32(); }
        if (Flags.IsBitSet(5)) { Stars = buffer.ReadInt64(); }
        UntilDate = buffer.ReadInt32();
    }
}