#!/usr/bin/env python3
#-*- coding: utf-8 -*- 

# GIMP plugin to manage collections of add-ons

# (c) Ofnuts 2025
#
#   History: (earlier versions by SeanBogie & MareroQ)
#
#   v3.0: 2013-05-26: Rewrite and cleanup:
#			- one single Python file to handle all managers
#			- user changes are handled with a configuration file
#			- can be used without a configuration file
#			- uses symlinks when possible (Linux, OSX)
#			- keeps activated resources in subfolders (allows filtering in the Gimp lists)
#			- UI simplification
#   v4.0: 2025.04.30: Convert to Gimp3, Gtk3 & Python3
#           - further simplified to reduce configuration
#			- remove handling of scripts (always a special case and not handled by Gimp3 anyway)
#           - always use directories
#			- dynamically retrieve "activated" sets, no need for ".activated" files


#
#   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.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

authors='Ofnuts'

import gi
gi.require_version('Gimp', '3.0')
from gi.repository import Gimp
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk

from gimphelpers import *
from configuration import *
from container import *
from manager import *

import sys
from dataclasses import dataclass, field

def N_(txt): return txt
def _(txt): return GLib.dgettext(None, txt)
blurb: str = N_('Activate/deactivate sets')

# Technical data specific to each type
@dataclass
class ResourceProcedure:
	type: str
	refresh: Callable

procedures: dict[str,ResourceProcedure]=\
{
	'resource-manager-brushes':   ResourceProcedure('brushes',Gimp.brushes_refresh),
	'resource-manager-dynamics':  ResourceProcedure('dynamics',Gimp.dynamics_refresh,),
	'resource-manager-fonts':     ResourceProcedure('fonts',Gimp.fonts_refresh),
	'resource-manager-gradients': ResourceProcedure('gradients',Gimp.gradients_refresh),
	'resource-manager-palettes':  ResourceProcedure('palettes',Gimp.palettes_refresh),
	'resource-manager-patterns':  ResourceProcedure('patterns',Gimp.patterns_refresh),
}
	
### Initialization
configuration: Configuration=Configuration(__file__)
configuration.loadConfiguration()

# Initialize the managers listed in the ini file
#enabled=configuration.getValue('all','enable',None).split()
#for resourceType in procedures:
#	if resourceType in enabled:
#		trace('Creating manager for %s' % resourceType)
#		#manager=ResourceManager(configuration, commonConfigVars, resourceType)
#		#manager.register()
		
def showDialog(procedure: Gimp.Procedure,
			   run_mode: Gimp.RunMode,
			   image: Gimp.Image,
			   drawables: list[Gimp.Drawable],
			   config:Gimp.ProcedureConfig,
			   run_data: Any):
	procName=procedure.get_name()
	trace(f'Executing procedure {procName}')
	manager=ResourceManager(configuration,procedures[procName])
	manager.showMainDialog()
	manager.main()
	trace(f'Exiting procedure {procName}')

	return procedure.new_return_values(Gimp.PDBStatusType.SUCCESS, GLib.Error())

class ResourceManagerPlugin(Gimp.PlugIn):
	def do_query_procedures(self):
		enabledTypes=configuration.getDefaultValue('enable').split()
		trace("In do_query_procedures")
		enabledProcs=[procName for procName,procData in procedures.items() if procData.type in enabledTypes]
		trace(f'Enabled procedures: {enabledProcs}')
		return enabledProcs

	def do_create_procedure(self, procName):
		trace(f'Creating procedure for {procName}')
		procedure: Gimp.Procedure = Gimp.ImageProcedure.new(self, procName,
									   Gimp.PDBProcType.PLUGIN,
									   run_func=showDialog,
									   run_data=procedures[procName]
									)
		procData=procedures[procName]
		resourceType=procData.type
		procedure.set_image_types('')
		procedure.set_sensitivity_mask(Gimp.ProcedureSensitivityMask.ALWAYS)
		procedure.set_attribution("Ofnuts", "Ofnuts", "2025")
		procedure.set_menu_label(_(configuration.getValue(resourceType,'menu_label')))
		procedure.add_menu_path(_(configuration.getValue(resourceType,'menu_path')))
		procedure.set_documentation(_(configuration.getValue(resourceType,'menu_blurb')),None,None)
		trace(f'End of creation for procedure {procName}')
		return procedure

	def do_set_i18n(self, procname):
		return False, None, None

Gimp.main(ResourceManagerPlugin.__gtype__, sys.argv)

