"""
file_unpacked_at_path_message.py

Class definitions for File-based event notifications originally generated by
the Unpacker.
"""
import json
from uuid import UUID

from .base import FSPluginMessageBase


class FSFileUnpackedAtPathMessage(FSPluginMessageBase):
    """Class for messages from the originating from the unpacker. This message specifies
    a file in the context of a specific firmware, at a specific path. It is serializable
    as JSON string.

    Args:
        file_hash: SHA256 hash of source file to process.
        firmware_hash: SHA256 hash of source firmware in which the given file and path are relevant.
        file_path: A path of the file in the context of the given firmware.
        mime_type: Mime type of the given file.
        fwan_process_id: Used for all related status tracking events. Defaults to uuid4().
        trigger_downstream_plugins: If set to False does not send a message to trigger
            normal downstream events. Defaults to True.
    """
    def __init__(self,
                 file_hash: str,
                 firmware_hash: str,
                 file_path: str,
                 mime_type: str,
                 fwan_process_id: UUID,
                 trigger_downstream_plugins: bool = True):
        super().__init__(fwan_process_id, trigger_downstream_plugins)
        self.file_hash = file_hash
        self.firmware_hash = firmware_hash
        self.file_path = file_path
        self.mime_type = mime_type

    @property
    def storage_id(self) -> str:
        """Since both `firmware_hash` and `file_hash` are important in this context
        (both need overrides and configs at a firmware/file combination), the storage_id
        is both concatenated with a "path-like" separator ( / ).
        """
        return f'{self.firmware_hash}/{self.file_hash}'

    @property
    def status_id(self) -> str:
        """Since `file_hash` is not unique, based on the same file being located in many different
        places in the same firmware, `file_path` is added to make `status_id` unique
        per firmware.
        """
        return f'{self.file_hash}:{self.file_path}'

    @staticmethod
    def deserialize(json_string: str):
        return FSFileUnpackedAtPathMessage(**json.loads(json_string))
