#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# ================================================== #
# This file is a part of PYGPT package               #
# Website: https://pygpt.net                         #
# GitHub:  https://github.com/szczyglis-dev/py-gpt   #
# MIT License                                        #
# Created By  : Marcin Szczygliński                  #
# Updated Date: 2026.01.03 17:00:00                  #
# ================================================== #

from PySide6.QtCore import QUrl
from PySide6.QtGui import QDesktopServices

class Url:
    def __init__(self, window=None):
        """
        Filesystem URL handler

        :param window: Window instance
        """
        self.window = window

    def handle(self,  url: QUrl):
        """
        Handle URL, bridge action or local file

        :param url: url
        """
        if not url.toString().strip():
            return

        # JS bridge
        if url.toString().startswith('bridge://open_find'):
            pid = int(url.toString().split(':')[2])
            if pid in self.window.ui.nodes['output']:
                self.window.ui.nodes['output'][pid].find_open()
            return
        elif url.toString() == 'bridge://escape':
            self.window.controller.access.on_escape()
            return
        elif url.toString() == 'bridge://focus':
            pid = self.window.controller.ui.tabs.get_current_pid()
            if pid in self.window.ui.nodes['output']:
                self.window.ui.nodes['output'][pid].on_focus_js()
            return
        elif url.toString().startswith('bridge://play_video/'):
            self.window.controller.media.play_video(url.toString().replace("bridge://play_video/", ""))
            return
        elif url.toString().startswith('bridge://open_image/'):
            self.window.tools.get("viewer").open_preview(url.toString().replace("bridge://open_image/", ""))
            return
        elif url.toString().startswith('bridge://download/'):
            self.window.controller.files.download_local(url.toString().replace("bridge://download/", ""))
            return

        # -------------
        extra_schemes = (
            'extra-audio-read',
            'extra-code-copy',
            'extra-copy',
            'extra-delete',
            'extra-edit',
            'extra-join',
            'extra-replay'
        )

        # local file
        if not url.scheme().startswith('http') and url.scheme() not in extra_schemes:
            self.window.controller.files.open(url.toLocalFile())

        # extra actions
        elif url.scheme() == 'extra-delete':  # ctx item delete
            id = url.toString().split(':')[1]
            self.window.controller.ctx.extra.delete_item(int(id))
        elif url.scheme() == 'extra-edit':  # ctx item edit
            id = url.toString().split(':')[1]
            self.window.controller.ctx.extra.edit_item(int(id))
        elif url.scheme() == 'extra-copy':  # ctx item copy
            id = url.toString().split(':')[1]
            self.window.controller.ctx.extra.copy_item(int(id))
        elif url.scheme() == 'extra-replay':  # ctx regen response
            id = url.toString().split(':')[1]
            self.window.controller.ctx.extra.replay_item(int(id))
        elif url.scheme() == 'extra-audio-read':  # ctx audio read
            id = url.toString().split(':')[1]
            self.window.controller.ctx.extra.audio_read_item(int(id))
        elif url.scheme() == 'extra-join':  # ctx join
            id = url.toString().split(':')[1]
            self.window.controller.ctx.extra.join_item(int(id))
        elif url.scheme() == 'extra-code-copy':  # copy code block
            id = url.toString().split(':')[1]
            self.window.controller.ctx.extra.copy_code_block(int(id))
        else:
            # external link
            if url.scheme().startswith('http'):
                self.window.controller.dialogs.info.open_url(url.toString())
                return

            QDesktopServices.openUrl(url)
