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

namespace MyTelegram.Schema;

///<summary>
/// Address optionally associated to a <a href="https://corefork.telegram.org/constructor/geoPoint">geoPoint</a>.
/// See <a href="https://corefork.telegram.org/constructor/geoPointAddress" />
///</summary>
[TlObject(0xde4c5d93)]
public sealed class TGeoPointAddress : IGeoPointAddress
{
    public uint ConstructorId => 0xde4c5d93;
    ///<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>
    /// Two-letter ISO 3166-1 alpha-2 country code
    ///</summary>
    public string CountryIso2 { get; set; }

    ///<summary>
    /// State
    ///</summary>
    public string? State { get; set; }

    ///<summary>
    /// City
    ///</summary>
    public string? City { get; set; }

    ///<summary>
    /// Street
    ///</summary>
    public string? Street { get; set; }

    public void ComputeFlag()
    {
        if (State != null) { Flags = Flags.SetBit(0); }
        if (City != null) { Flags = Flags.SetBit(1); }
        if (Street != null) { Flags = Flags.SetBit(2); }
    }

    public void Serialize(IBufferWriter<byte> writer)
    {
        ComputeFlag();
        writer.Write(ConstructorId);
        writer.Write(Flags);
        writer.Write(CountryIso2);
        if (Flags.IsBitSet(0)) { writer.Write(State); }
        if (Flags.IsBitSet(1)) { writer.Write(City); }
        if (Flags.IsBitSet(2)) { writer.Write(Street); }
    }

    public void Deserialize(ref ReadOnlyMemory<byte> buffer)
    {
        Flags = buffer.ReadInt32();
        CountryIso2 = buffer.ReadString();
        if (Flags.IsBitSet(0)) { State = buffer.ReadString(); }
        if (Flags.IsBitSet(1)) { City = buffer.ReadString(); }
        if (Flags.IsBitSet(2)) { Street = buffer.ReadString(); }
    }
}