#  Pyrogram - Telegram MTProto API Client Library for Python
#  Copyright (C) 2017-2020 Dan <https://github.com/delivrance>
#
#  This file is part of Pyrogram.
#
#  Pyrogram is free software: you can redistribute it and/or modify
#  it under the terms of the GNU Lesser General Public License as published
#  by the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.
#
#  Pyrogram is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU Lesser General Public License for more details.
#
#  You should have received a copy of the GNU Lesser General Public License
#  along with Pyrogram.  If not, see <http://www.gnu.org/licenses/>.

from io import BytesIO

from pyrogram.raw.core.primitives import Int, Long, Int128, Int256, Bool, Bytes, String, Double, Vector
from pyrogram.raw.core import TLObject
from pyrogram import raw
from typing import List, Union, Any

# # # # # # # # # # # # # # # # # # # # # # # #
#               !!! WARNING !!!               #
#          This is a generated file!          #
# All changes made in this file will be lost! #
# # # # # # # # # # # # # # # # # # # # # # # #


class DcOption(TLObject):  # type: ignore
    """This object is a constructor of the base type :obj:`~pyrogram.raw.base.DcOption`.

    Details:
        - Layer: ``117``
        - ID: ``0x18b7a10d``

    Parameters:
        id: ``int`` ``32-bit``
        ip_address: ``str``
        port: ``int`` ``32-bit``
        ipv6 (optional): ``bool``
        media_only (optional): ``bool``
        tcpo_only (optional): ``bool``
        cdn (optional): ``bool``
        static (optional): ``bool``
        secret (optional): ``bytes``
    """

    __slots__: List[str] = ["id", "ip_address", "port", "ipv6", "media_only", "tcpo_only", "cdn", "static", "secret"]

    ID = 0x18b7a10d
    QUALNAME = "types.DcOption"

    def __init__(self, *, id: int, ip_address: str, port: int, ipv6: Union[None, bool] = None, media_only: Union[None, bool] = None, tcpo_only: Union[None, bool] = None, cdn: Union[None, bool] = None, static: Union[None, bool] = None, secret: Union[None, bytes] = None) -> None:
        self.id = id  # int
        self.ip_address = ip_address  # string
        self.port = port  # int
        self.ipv6 = ipv6  # flags.0?true
        self.media_only = media_only  # flags.1?true
        self.tcpo_only = tcpo_only  # flags.2?true
        self.cdn = cdn  # flags.3?true
        self.static = static  # flags.4?true
        self.secret = secret  # flags.10?bytes

    @staticmethod
    def read(data: BytesIO, *args: Any) -> "DcOption":
        flags = Int.read(data)
        
        ipv6 = True if flags & (1 << 0) else False
        media_only = True if flags & (1 << 1) else False
        tcpo_only = True if flags & (1 << 2) else False
        cdn = True if flags & (1 << 3) else False
        static = True if flags & (1 << 4) else False
        id = Int.read(data)
        
        ip_address = String.read(data)
        
        port = Int.read(data)
        
        secret = Bytes.read(data) if flags & (1 << 10) else None
        return DcOption(id=id, ip_address=ip_address, port=port, ipv6=ipv6, media_only=media_only, tcpo_only=tcpo_only, cdn=cdn, static=static, secret=secret)

    def write(self) -> bytes:
        data = BytesIO()
        data.write(Int(self.ID, False))

        flags = 0
        flags |= (1 << 0) if self.ipv6 is not None else 0
        flags |= (1 << 1) if self.media_only is not None else 0
        flags |= (1 << 2) if self.tcpo_only is not None else 0
        flags |= (1 << 3) if self.cdn is not None else 0
        flags |= (1 << 4) if self.static is not None else 0
        flags |= (1 << 10) if self.secret is not None else 0
        data.write(Int(flags))
        
        data.write(Int(self.id))
        
        data.write(String(self.ip_address))
        
        data.write(Int(self.port))
        
        if self.secret is not None:
            data.write(Bytes(self.secret))
        
        return data.getvalue()
