# -*- coding: utf-8 -*-

"""
/***************************************************************************
 MpiRrimPlugin
                                 A QGIS plugin
 DEMからMPI-RRIMを作成します
 Generated by Plugin Builder: http://g-sherman.github.io/Qgis-Plugin-Builder/
                              -------------------
        begin                : 2026-03-19
        copyright            : (C) 2026 by Y. Iwasa
        email                : yiwasa0607@gmail.com
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/
"""

__author__ = 'Y. Iwasa'
__date__ = '2026-03-19'
__copyright__ = '(C) 2026 by Y. Iwasa'

# This will get replaced with a git SHA1 when you do a git archive

__revision__ = '$Format:%H$'

import os
import sys
import inspect
import processing
from qgis.PyQt.QtWidgets import QAction
from qgis.PyQt.QtGui import QIcon
from qgis.utils import iface

from qgis.core import QgsProcessingAlgorithm, QgsApplication
from .mpi_rrim_plugin_provider import MpiRrimPluginProvider

cmd_folder = os.path.split(inspect.getfile(inspect.currentframe()))[0]

if cmd_folder not in sys.path:
    sys.path.insert(0, cmd_folder)


class MpiRrimPluginPlugin(object):

    def __init__(self):
        self.provider = None

    def initProcessing(self):
        """Init Processing provider for QGIS >= 3.8."""
        self.provider = MpiRrimPluginProvider()
        QgsApplication.processingRegistry().addProvider(self.provider)

    def initGui(self):
        self.initProcessing()
        # フォルダ内にある icon.png をアイコンとして読み込む
        icon_path = os.path.join(os.path.dirname(__file__), 'icon.png')
        self.action = QAction(QIcon(icon_path), "Stereo MPI-RRIM Creator", iface.mainWindow())
        self.action.triggered.connect(self.run)
        
        # ツールバーにボタンを追加
        iface.addToolBarIcon(self.action)

    def unload(self):
        # 既にC++側でオブジェクトが削除されている場合はエラーを無視する
        try:
            QgsApplication.processingRegistry().removeProvider(self.provider)
        except RuntimeError:
            pass
            
        iface.removeToolBarIcon(self.action)
    
    def run(self):
        # さきほど id() で設定した 'mpirrim' と、アルゴリズムの 'create_mpi_rrim' を繋いで呼び出す
        processing.execAlgorithmDialog('mpirrim:create_mpi_rrim')
