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

namespace MyTelegram.Schema.Messages;

///<summary>
/// <a href="https://corefork.telegram.org/api/transcribe">Transcribed text from a voice message »</a>
/// See <a href="https://corefork.telegram.org/constructor/messages.transcribedAudio" />
///</summary>
[TlObject(0xcfb9d957)]
public sealed class TTranscribedAudio : ITranscribedAudio
{
    public uint ConstructorId => 0xcfb9d957;
    ///<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 the transcription is partial because audio transcription is still in progress, if set the user may receive further <a href="https://corefork.telegram.org/constructor/updateTranscribedAudio">updateTranscribedAudio</a> updates with the updated transcription.
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool Pending { get; set; }

    ///<summary>
    /// Transcription ID
    ///</summary>
    public long TranscriptionId { get; set; }

    ///<summary>
    /// Transcripted text
    ///</summary>
    public string Text { get; set; }

    ///<summary>
    /// For non-<a href="https://corefork.telegram.org/api/premium">Premium</a> users, this flag will be set, indicating the remaining transcriptions in the free trial period.
    ///</summary>
    public int? TrialRemainsNum { get; set; }

    ///<summary>
    /// For non-<a href="https://corefork.telegram.org/api/premium">Premium</a> users, this flag will be set, indicating the date when the <code>trial_remains_num</code> counter will be reset to the maximum value of <a href="https://corefork.telegram.org/api/config#transcribe-audio-trial-weekly-number">transcribe_audio_trial_weekly_number</a>.
    ///</summary>
    public int? TrialRemainsUntilDate { get; set; }

    public void ComputeFlag()
    {
        if (Pending) { Flags = Flags.SetBit(0); }
        if (/*TrialRemainsNum != 0 && */TrialRemainsNum.HasValue) { Flags = Flags.SetBit(1); }
        if (/*TrialRemainsUntilDate != 0 && */TrialRemainsUntilDate.HasValue) { Flags = Flags.SetBit(1); }
    }

    public void Serialize(IBufferWriter<byte> writer)
    {
        ComputeFlag();
        writer.Write(ConstructorId);
        writer.Write(Flags);
        writer.Write(TranscriptionId);
        writer.Write(Text);
        if (Flags.IsBitSet(1)) { writer.Write(TrialRemainsNum.Value); }
        if (Flags.IsBitSet(1)) { writer.Write(TrialRemainsUntilDate.Value); }
    }

    public void Deserialize(ref ReadOnlyMemory<byte> buffer)
    {
        Flags = buffer.ReadInt32();
        if (Flags.IsBitSet(0)) { Pending = true; }
        TranscriptionId = buffer.ReadInt64();
        Text = buffer.ReadString();
        if (Flags.IsBitSet(1)) { TrialRemainsNum = buffer.ReadInt32(); }
        if (Flags.IsBitSet(1)) { TrialRemainsUntilDate = buffer.ReadInt32(); }
    }
}