import os

from parallels.core import messages
from parallels.core.logging import get_logger
from parallels.core.utils.config_utils import global_section
from parallels.core.utils.entity import Entity

logger = get_logger(__name__)


class SourcePanelInfo(Entity):
    """Information about detected source panel"""

    def __init__(self, panel_type, panel_dir):
        """
        :type panel_type: str | unicode
        :type panel_dir: str | unicode
        """
        self._panel_type = panel_type
        self._panel_dir = panel_dir

    @property
    def panel_type(self):
        """Panel type ID (e.g. 'plesk', 'confixx', etc)

        :rtype: str | unicode
        """
        return self._panel_type

    @property
    def panel_dir(self):
        """Directory where all panel files (Python modules, dump agent, configuration description, etc) resides
        :rtype: str | unicode
        """
        return self._panel_dir


class SourcePanelDetector(object):
    @staticmethod
    def detect(global_context):
        """Detect source panel type

        :type global_context: parallels.core.global_context.GlobalMigrationContext
        :rtype: parallels.core.utils.panel_detector.source.SourcePanelInfo
        """
        source_panel_type = global_section(global_context.config).get('source-type')
        logger.debug(messages.DEBUG_SOURCE_PANEL, source_panel_type)
        root_modules_dir = os.path.normpath(
            os.path.join(os.path.dirname(__file__), '..', '..', '..')
        )
        panel_dir = os.path.join(root_modules_dir, 'plesk', 'source', source_panel_type)
        return SourcePanelInfo(
            panel_type=source_panel_type,
            panel_dir=panel_dir
        )
