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

namespace MyTelegram.Schema;

///<summary>
/// A poll answer, and how users voted on it
/// See <a href="https://corefork.telegram.org/constructor/pollAnswerVoters" />
///</summary>
[TlObject(0x3b6ddad2)]
public sealed class TPollAnswerVoters : IPollAnswerVoters
{
    public uint ConstructorId => 0x3b6ddad2;
    ///<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 have chosen this answer
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool Chosen { get; set; }

    ///<summary>
    /// For quizzes, whether the option we have chosen is correct
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool Correct { get; set; }

    ///<summary>
    /// The param that has to be passed to <a href="https://corefork.telegram.org/method/messages.sendVote">messages.sendVote</a>.
    ///</summary>
    public ReadOnlyMemory<byte> Option { get; set; }

    ///<summary>
    /// How many users voted for this option
    ///</summary>
    public int Voters { get; set; }

    public void ComputeFlag()
    {
        if (Chosen) { Flags = Flags.SetBit(0); }
        if (Correct) { Flags = Flags.SetBit(1); }

    }

    public void Serialize(IBufferWriter<byte> writer)
    {
        ComputeFlag();
        writer.Write(ConstructorId);
        writer.Write(Flags);
        writer.Write(Option);
        writer.Write(Voters);
    }

    public void Deserialize(ref ReadOnlyMemory<byte> buffer)
    {
        Flags = buffer.ReadInt32();
        if (Flags.IsBitSet(0)) { Chosen = true; }
        if (Flags.IsBitSet(1)) { Correct = true; }
        Option = buffer.ReadBytes();
        Voters = buffer.ReadInt32();
    }
}