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

namespace MyTelegram.Schema;

///<summary>
/// Coordinates and size of a clicable rectangular area on top of a story.
/// See <a href="https://corefork.telegram.org/constructor/mediaAreaCoordinates" />
///</summary>
[TlObject(0xcfc9e002)]
public sealed class TMediaAreaCoordinates : IMediaAreaCoordinates
{
    public uint ConstructorId => 0xcfc9e002;
    ///<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 abscissa of the rectangle's center, as a percentage of the media width (0-100).
    ///</summary>
    public double X { get; set; }

    ///<summary>
    /// The ordinate of the rectangle's center, as a percentage of the media height (0-100).
    ///</summary>
    public double Y { get; set; }

    ///<summary>
    /// The width of the rectangle, as a percentage of the media width (0-100).
    ///</summary>
    public double W { get; set; }

    ///<summary>
    /// The height of the rectangle, as a percentage of the media height (0-100).
    ///</summary>
    public double H { get; set; }

    ///<summary>
    /// Clockwise rotation angle of the rectangle, in degrees (0-360).
    ///</summary>
    public double Rotation { get; set; }

    ///<summary>
    /// The radius of the rectangle corner rounding, as a percentage of the media width.
    ///</summary>
    public double? Radius { get; set; }

    public void ComputeFlag()
    {
        if (Radius>0) { Flags = Flags.SetBit(0); }
    }

    public void Serialize(IBufferWriter<byte> writer)
    {
        ComputeFlag();
        writer.Write(ConstructorId);
        writer.Write(Flags);
        writer.Write(X);
        writer.Write(Y);
        writer.Write(W);
        writer.Write(H);
        writer.Write(Rotation);
        if (Flags.IsBitSet(0)) { writer.Write(Radius.Value); }
    }

    public void Deserialize(ref ReadOnlyMemory<byte> buffer)
    {
        Flags = buffer.ReadInt32();
        X = buffer.ReadDouble();
        Y = buffer.ReadDouble();
        W = buffer.ReadDouble();
        H = buffer.ReadDouble();
        Rotation = buffer.ReadDouble();
        if (Flags.IsBitSet(0)) { Radius = buffer.ReadDouble(); }
    }
}