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

namespace MyTelegram.Schema;

///<summary>
/// Results of poll
/// See <a href="https://corefork.telegram.org/constructor/pollResults" />
///</summary>
[TlObject(0x7adf2420)]
public sealed class TPollResults : IPollResults
{
    public uint ConstructorId => 0x7adf2420;
    ///<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>
    /// Similar to <a href="https://corefork.telegram.org/api/min">min</a> objects, used for poll constructors that are the same for all users so they don't have the option chosen by the current user (you can use <a href="https://corefork.telegram.org/method/messages.getPollResults">messages.getPollResults</a> to get the full poll results).
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool Min { get; set; }

    ///<summary>
    /// Poll results
    ///</summary>
    public TVector<MyTelegram.Schema.IPollAnswerVoters>? Results { get; set; }

    ///<summary>
    /// Total number of people that voted in the poll
    ///</summary>
    public int? TotalVoters { get; set; }

    ///<summary>
    /// IDs of the last users that recently voted in the poll
    ///</summary>
    public TVector<MyTelegram.Schema.IPeer>? RecentVoters { get; set; }

    ///<summary>
    /// Explanation of quiz solution
    ///</summary>
    public string? Solution { get; set; }

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

    public void ComputeFlag()
    {
        if (Min) { Flags = Flags.SetBit(0); }
        if (Results?.Count > 0) { Flags = Flags.SetBit(1); }
        if (/*TotalVoters != 0 && */TotalVoters.HasValue) { Flags = Flags.SetBit(2); }
        if (RecentVoters?.Count > 0) { Flags = Flags.SetBit(3); }
        if (Solution != null) { Flags = Flags.SetBit(4); }
        if (SolutionEntities?.Count > 0) { Flags = Flags.SetBit(4); }
    }

    public void Serialize(IBufferWriter<byte> writer)
    {
        ComputeFlag();
        writer.Write(ConstructorId);
        writer.Write(Flags);
        if (Flags.IsBitSet(1)) { writer.Write(Results); }
        if (Flags.IsBitSet(2)) { writer.Write(TotalVoters.Value); }
        if (Flags.IsBitSet(3)) { writer.Write(RecentVoters); }
        if (Flags.IsBitSet(4)) { writer.Write(Solution); }
        if (Flags.IsBitSet(4)) { writer.Write(SolutionEntities); }
    }

    public void Deserialize(ref ReadOnlyMemory<byte> buffer)
    {
        Flags = buffer.ReadInt32();
        if (Flags.IsBitSet(0)) { Min = true; }
        if (Flags.IsBitSet(1)) { Results = buffer.Read<TVector<MyTelegram.Schema.IPollAnswerVoters>>(); }
        if (Flags.IsBitSet(2)) { TotalVoters = buffer.ReadInt32(); }
        if (Flags.IsBitSet(3)) { RecentVoters = buffer.Read<TVector<MyTelegram.Schema.IPeer>>(); }
        if (Flags.IsBitSet(4)) { Solution = buffer.ReadString(); }
        if (Flags.IsBitSet(4)) { SolutionEntities = buffer.Read<TVector<MyTelegram.Schema.IMessageEntity>>(); }
    }
}