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

namespace MyTelegram.Schema;

///<summary>
/// A poll
/// See <a href="https://corefork.telegram.org/constructor/inputMediaPoll" />
///</summary>
[TlObject(0xf94e5f1)]
public sealed class TInputMediaPoll : IInputMedia
{
    public uint ConstructorId => 0xf94e5f1;
    ///<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>
    /// The poll to send
    /// See <a href="https://corefork.telegram.org/type/Poll" />
    ///</summary>
    public MyTelegram.Schema.IPoll Poll { get; set; }

    ///<summary>
    /// Correct answer IDs (for quiz polls)
    ///</summary>
    public TVector<string>? CorrectAnswers { 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</a>
    ///</summary>
    public TVector<MyTelegram.Schema.IMessageEntity>? SolutionEntities { get; set; }

    public void ComputeFlag()
    {
        if (CorrectAnswers?.Count > 0) { Flags = Flags.SetBit(0); }
        if (Solution != null) { Flags = Flags.SetBit(1); }
        if (SolutionEntities?.Count > 0) { Flags = Flags.SetBit(1); }
    }

    public void Serialize(IBufferWriter<byte> writer)
    {
        ComputeFlag();
        writer.Write(ConstructorId);
        writer.Write(Flags);
        writer.Write(Poll);
        if (Flags.IsBitSet(0)) { writer.Write(CorrectAnswers); }
        if (Flags.IsBitSet(1)) { writer.Write(Solution); }
        if (Flags.IsBitSet(1)) { writer.Write(SolutionEntities); }
    }

    public void Deserialize(ref ReadOnlyMemory<byte> buffer)
    {
        Flags = buffer.ReadInt32();
        Poll = buffer.Read<MyTelegram.Schema.IPoll>();
        if (Flags.IsBitSet(0)) { CorrectAnswers = buffer.Read<TVector<string>>(); }
        if (Flags.IsBitSet(1)) { Solution = buffer.ReadString(); }
        if (Flags.IsBitSet(1)) { SolutionEntities = buffer.Read<TVector<MyTelegram.Schema.IMessageEntity>>(); }
    }
}