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

namespace MyTelegram.Schema.Help;

///<summary>
/// Name, ISO code, localized name and phone codes/patterns of a specific country
/// See <a href="https://corefork.telegram.org/constructor/help.country" />
///</summary>
[TlObject(0xc3878e23)]
public sealed class TCountry : ICountry
{
    public uint ConstructorId => 0xc3878e23;
    ///<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 this country should not be shown in the list
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool Hidden { get; set; }

    ///<summary>
    /// ISO code of country
    ///</summary>
    public string Iso2 { get; set; }

    ///<summary>
    /// Name of the country in the country's language
    ///</summary>
    public string DefaultName { get; set; }

    ///<summary>
    /// Name of the country in the user's language, if different from the original name
    ///</summary>
    public string? Name { get; set; }

    ///<summary>
    /// Phone codes/patterns
    ///</summary>
    public TVector<MyTelegram.Schema.Help.ICountryCode> CountryCodes { get; set; }

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

    }

    public void Serialize(IBufferWriter<byte> writer)
    {
        ComputeFlag();
        writer.Write(ConstructorId);
        writer.Write(Flags);
        writer.Write(Iso2);
        writer.Write(DefaultName);
        if (Flags.IsBitSet(1)) { writer.Write(Name); }
        writer.Write(CountryCodes);
    }

    public void Deserialize(ref ReadOnlyMemory<byte> buffer)
    {
        Flags = buffer.ReadInt32();
        if (Flags.IsBitSet(0)) { Hidden = true; }
        Iso2 = buffer.ReadString();
        DefaultName = buffer.ReadString();
        if (Flags.IsBitSet(1)) { Name = buffer.ReadString(); }
        CountryCodes = buffer.Read<TVector<MyTelegram.Schema.Help.ICountryCode>>();
    }
}