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

namespace MyTelegram.Schema;

///<summary>
/// Represents a forwarded <a href="https://corefork.telegram.org/api/stories">story</a> or a story mention.
/// See <a href="https://corefork.telegram.org/constructor/messageMediaStory" />
///</summary>
[TlObject(0x68cb6283)]
public sealed class TMessageMediaStory : IMessageMedia
{
    public uint ConstructorId => 0x68cb6283;
    ///<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, indicates that this someone has mentioned us in this story (i.e. by tagging us in the description) or vice versa, we have mentioned the other peer (if the message is outgoing).
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool ViaMention { get; set; }

    ///<summary>
    /// Peer that posted the story.
    /// See <a href="https://corefork.telegram.org/type/Peer" />
    ///</summary>
    public MyTelegram.Schema.IPeer Peer { get; set; }

    ///<summary>
    /// Story ID
    ///</summary>
    public int Id { get; set; }

    ///<summary>
    /// The story itself, if absent fetch it using <a href="https://corefork.telegram.org/method/stories.getStoriesByID">stories.getStoriesByID</a> and the <code>peer</code>/<code>id</code> parameters specified above.
    /// See <a href="https://corefork.telegram.org/type/StoryItem" />
    ///</summary>
    public MyTelegram.Schema.IStoryItem? Story { get; set; }

    public void ComputeFlag()
    {
        if (ViaMention) { Flags = Flags.SetBit(1); }
        if (Story != null) { Flags = Flags.SetBit(0); }
    }

    public void Serialize(IBufferWriter<byte> writer)
    {
        ComputeFlag();
        writer.Write(ConstructorId);
        writer.Write(Flags);
        writer.Write(Peer);
        writer.Write(Id);
        if (Flags.IsBitSet(0)) { writer.Write(Story); }
    }

    public void Deserialize(ref ReadOnlyMemory<byte> buffer)
    {
        Flags = buffer.ReadInt32();
        if (Flags.IsBitSet(1)) { ViaMention = true; }
        Peer = buffer.Read<MyTelegram.Schema.IPeer>();
        Id = buffer.ReadInt32();
        if (Flags.IsBitSet(0)) { Story = buffer.Read<MyTelegram.Schema.IStoryItem>(); }
    }
}