from cheat_engine import CheatEngine

class ActivationEngine:
    def __init__(self):
        self.engine = CheatEngine()
        self.active_cheats = {}

    def toggle(self, game_meta, pattern_info):
        key = pattern_info.get('cheat_key', pattern_info.get('cheat_name'))
        enabled = self.active_cheats.get(key, False)
        new_state = not enabled
        success, msg = self.engine.toggle_cheat(game_meta, pattern_info, new_state)
        if success:
            self.active_cheats[key] = new_state
        return success, msg

    def activate(self, game_meta, trainer_data):
        method = trainer_data.get('method', 'memory')

        if method == 'toggle':
            return self.toggle(game_meta, trainer_data)

        if method == 'memory':
            process_name = game_meta.get('process_name') or game_meta.get('exe')
            if not process_name:
                return False, "Keine Prozessinfo für dieses Spiel"
            success, msg = self.engine.attach(process_name)
            if not success:
                return False, msg
            pattern = trainer_data.get('pattern', '')
            offset = int(trainer_data.get('offset', 0))
            value = trainer_data.get('value', '0')
            value_type = trainer_data.get('value_type', 'int32')
            success, msg = self.engine.find_and_patch(pattern, offset, value, value_type)
            return success, msg if not success else f"Cheat aktiv: {trainer_data['name']}"

        elif method == 'savegame':
            path = game_meta.get('save_path', '')
            import os
            if not path or not os.path.exists(os.path.expandvars(path)):
                return False, "Savegame-Pfad nicht gefunden"
            latest_save = max(
                [os.path.join(os.path.expandvars(path), f) for f in os.listdir(os.path.expandvars(path))],
                key=os.path.getmtime
            )
            success, msg = self.engine.edit_savegame(latest_save, trainer_data['field'], trainer_data['value'])
            return success, msg

        elif method == 'bridge':
            command = trainer_data.get('command', '')
            params = trainer_data.get('value')
            return self.engine.activate_command(command, params)

        elif method == 'live_scan':
            return False, "Live-Scan über den Scan-Dialog durchführen"

        return False, "Unknown activation method"

    def attach(self, process_name_or_pid):
        if isinstance(process_name_or_pid, int):
            return self.engine.attach_by_pid(process_name_or_pid)
        return self.engine.attach(process_name_or_pid)

    def scan(self, value, value_type='int32'):
        return self.engine.scan_value(value, value_type)

    def rescan(self, addresses, value, value_type='int32'):
        return self.engine.rescan_value(addresses, value, value_type)

    def patch(self, address, value, value_type='int32'):
        return self.engine.write_memory_value(address, value, value_type)

    def detach(self):
        self.active_cheats.clear()
        return self.engine.detach()
