#=============================================================================
#
#  Copyright (c) Kitware, Inc.
#  All rights reserved.
#  See LICENSE.txt for details.
#
#  This software is distributed WITHOUT ANY WARRANTY; without even
#  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
#  PURPOSE.  See the above copyright notice for more information.
#
#=============================================================================

""" loadSMTKPluginInParaView.py:

Test the ParaView plugins generated by SMTK for opening .smtk files.

"""

import ctypes
import os
import unittest

from paraview import simple as pvsimple

import smtk
import smtk.common
import smtk.resource
import smtk.operation
import smtk.extension.paraview.plugin as plugin
import smtk.testing


class LoadSMTKPluginInParaView(smtk.testing.TestCase):

    def testLoadPolygonSessionPlugin(self):
        # Create a new operation manager
        operationManager = smtk.operation.Manager.create()

        # Register any loaded plugins to the operation manager
        plugin.registerPluginsTo(operationManager)

        # Record the number of available operations
        beforePlugin = len(operationManager.availableOperations())
        print('new operation manager:', beforePlugin, 'available operations')

        # Load the polygon session plugin
        pvsimple.LoadPlugin("$<TARGET_FILE:smtkPolygonSessionPlugin>", True)

        # Register loaded plugins to the operation manager
        plugin.registerPluginsTo(operationManager)

        # Record the number of available operations
        availableOps = operationManager.availableOperations()
        afterPlugin = len(availableOps)
        print('after adding polygon session:', afterPlugin, 'available operations')
        for op in availableOps:
            print(op)

        # Check that the number of operations has increased
        if not afterPlugin > beforePlugin:
            raise RuntimeError('Automatic plugin registration failed')

        # Register loaded plugins to the operation manager again
        plugin.registerPluginsTo(operationManager)

        # Record the number of available operations
        afterReload = len(operationManager.availableOperations())
        print('after reregistering plugins:', afterReload, 'available operations')

        # Check that the number of operations has increased
        if afterPlugin != afterReload:
            raise RuntimeError('reregistering plugins should have no effect')

        # Unregister loaded plugins from the operation manager
        plugin.unregisterPluginsFrom(operationManager)

        # Record the number of available operations
        afterUnload = len(operationManager.availableOperations())
        print('after unloading plugins:', afterUnload, 'available operations')

        # Check that the number of operations is back to its start value
        if beforePlugin != afterUnload:
            raise RuntimeError('Plugin unload failed')


if __name__ == '__main__':
    smtk.testing.process_arguments()
    unittest.main()
