"""pywebview WinForms не использует icon= — подставляем axon.ico с альфой."""

from __future__ import annotations

import logging
import sys

logger = logging.getLogger(__name__)


def patch_pywebview_taskbar_icon(ico_path: str) -> None:
    if sys.platform != "win32" or not ico_path:
        return
    try:
        from pathlib import Path

        path = Path(ico_path)
        if not path.is_file():
            return

        import clr

        clr.AddReference("System.Drawing")
        from System.Drawing import Icon  # type: ignore

        from webview.platforms import winforms

        browser_form = winforms.BrowserView.BrowserForm
        original_init = browser_form.__init__
        icon_file = str(path.resolve())

        def _init_with_icon(self, window, cache_dir):
            original_init(self, window, cache_dir)
            try:
                self.Icon = Icon(icon_file)
            except Exception as e:
                logger.warning("taskbar icon: %s", e)

        browser_form.__init__ = _init_with_icon
    except Exception as e:
        logger.warning("patch taskbar icon: %s", e)
