#  FORK PYROFORK <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, Optional, Any

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


class SponsoredMessage(TLObject):  # type: ignore
    """Telegram API type.

    Constructor of :obj:`~pyrogram.raw.base.SponsoredMessage`.

    Details:
        - Layer: ``177``
        - ID: ``ED5383F7``

    Parameters:
        random_id (``bytes``):
            N/A

        message (``str``):
            N/A

        recommended (``bool``, *optional*):
            N/A

        show_peer_photo (``bool``, *optional*):
            N/A

        can_report (``bool``, *optional*):
            N/A

        from_id (:obj:`Peer <pyrogram.raw.base.Peer>`, *optional*):
            N/A

        chat_invite (:obj:`ChatInvite <pyrogram.raw.base.ChatInvite>`, *optional*):
            N/A

        chat_invite_hash (``str``, *optional*):
            N/A

        channel_post (``int`` ``32-bit``, *optional*):
            N/A

        start_param (``str``, *optional*):
            N/A

        webpage (:obj:`SponsoredWebPage <pyrogram.raw.base.SponsoredWebPage>`, *optional*):
            N/A

        app (:obj:`BotApp <pyrogram.raw.base.BotApp>`, *optional*):
            N/A

        entities (List of :obj:`MessageEntity <pyrogram.raw.base.MessageEntity>`, *optional*):
            N/A

        button_text (``str``, *optional*):
            N/A

        sponsor_info (``str``, *optional*):
            N/A

        additional_info (``str``, *optional*):
            N/A

    """

    __slots__: List[str] = ["random_id", "message", "recommended", "show_peer_photo", "can_report", "from_id", "chat_invite", "chat_invite_hash", "channel_post", "start_param", "webpage", "app", "entities", "button_text", "sponsor_info", "additional_info"]

    ID = 0xed5383f7
    QUALNAME = "types.SponsoredMessage"

    def __init__(self, *, random_id: bytes, message: str, recommended: Optional[bool] = None, show_peer_photo: Optional[bool] = None, can_report: Optional[bool] = None, from_id: "raw.base.Peer" = None, chat_invite: "raw.base.ChatInvite" = None, chat_invite_hash: Optional[str] = None, channel_post: Optional[int] = None, start_param: Optional[str] = None, webpage: "raw.base.SponsoredWebPage" = None, app: "raw.base.BotApp" = None, entities: Optional[List["raw.base.MessageEntity"]] = None, button_text: Optional[str] = None, sponsor_info: Optional[str] = None, additional_info: Optional[str] = None) -> None:
        self.random_id = random_id  # bytes
        self.message = message  # string
        self.recommended = recommended  # flags.5?true
        self.show_peer_photo = show_peer_photo  # flags.6?true
        self.can_report = can_report  # flags.12?true
        self.from_id = from_id  # flags.3?Peer
        self.chat_invite = chat_invite  # flags.4?ChatInvite
        self.chat_invite_hash = chat_invite_hash  # flags.4?string
        self.channel_post = channel_post  # flags.2?int
        self.start_param = start_param  # flags.0?string
        self.webpage = webpage  # flags.9?SponsoredWebPage
        self.app = app  # flags.10?BotApp
        self.entities = entities  # flags.1?Vector<MessageEntity>
        self.button_text = button_text  # flags.11?string
        self.sponsor_info = sponsor_info  # flags.7?string
        self.additional_info = additional_info  # flags.8?string

    @staticmethod
    def read(b: BytesIO, *args: Any) -> "SponsoredMessage":
        
        flags = Int.read(b)
        
        recommended = True if flags & (1 << 5) else False
        show_peer_photo = True if flags & (1 << 6) else False
        can_report = True if flags & (1 << 12) else False
        random_id = Bytes.read(b)
        
        from_id = TLObject.read(b) if flags & (1 << 3) else None
        
        chat_invite = TLObject.read(b) if flags & (1 << 4) else None
        
        chat_invite_hash = String.read(b) if flags & (1 << 4) else None
        channel_post = Int.read(b) if flags & (1 << 2) else None
        start_param = String.read(b) if flags & (1 << 0) else None
        webpage = TLObject.read(b) if flags & (1 << 9) else None
        
        app = TLObject.read(b) if flags & (1 << 10) else None
        
        message = String.read(b)
        
        entities = TLObject.read(b) if flags & (1 << 1) else []
        
        button_text = String.read(b) if flags & (1 << 11) else None
        sponsor_info = String.read(b) if flags & (1 << 7) else None
        additional_info = String.read(b) if flags & (1 << 8) else None
        return SponsoredMessage(random_id=random_id, message=message, recommended=recommended, show_peer_photo=show_peer_photo, can_report=can_report, from_id=from_id, chat_invite=chat_invite, chat_invite_hash=chat_invite_hash, channel_post=channel_post, start_param=start_param, webpage=webpage, app=app, entities=entities, button_text=button_text, sponsor_info=sponsor_info, additional_info=additional_info)

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

        flags = 0
        flags |= (1 << 5) if self.recommended else 0
        flags |= (1 << 6) if self.show_peer_photo else 0
        flags |= (1 << 12) if self.can_report else 0
        flags |= (1 << 3) if self.from_id is not None else 0
        flags |= (1 << 4) if self.chat_invite is not None else 0
        flags |= (1 << 4) if self.chat_invite_hash is not None else 0
        flags |= (1 << 2) if self.channel_post is not None else 0
        flags |= (1 << 0) if self.start_param is not None else 0
        flags |= (1 << 9) if self.webpage is not None else 0
        flags |= (1 << 10) if self.app is not None else 0
        flags |= (1 << 1) if self.entities else 0
        flags |= (1 << 11) if self.button_text is not None else 0
        flags |= (1 << 7) if self.sponsor_info is not None else 0
        flags |= (1 << 8) if self.additional_info is not None else 0
        b.write(Int(flags))
        
        b.write(Bytes(self.random_id))
        
        if self.from_id is not None:
            b.write(self.from_id.write())
        
        if self.chat_invite is not None:
            b.write(self.chat_invite.write())
        
        if self.chat_invite_hash is not None:
            b.write(String(self.chat_invite_hash))
        
        if self.channel_post is not None:
            b.write(Int(self.channel_post))
        
        if self.start_param is not None:
            b.write(String(self.start_param))
        
        if self.webpage is not None:
            b.write(self.webpage.write())
        
        if self.app is not None:
            b.write(self.app.write())
        
        b.write(String(self.message))
        
        if self.entities is not None:
            b.write(Vector(self.entities))
        
        if self.button_text is not None:
            b.write(String(self.button_text))
        
        if self.sponsor_info is not None:
            b.write(String(self.sponsor_info))
        
        if self.additional_info is not None:
            b.write(String(self.additional_info))
        
        return b.getvalue()
