@tool
extends EditorPlugin

# Colors file format example:
#{
#	"color0": "#121212",
#	"color1": "#6D6253",
#	"color2": "#756755",
#	"color3": "#85725A",
#	"color4": "#897A67",
#	"color5": "#958774",
#	"color6": "#A4917A",
#	"color7": "#cbc6be",
#	"color8": "#8e8a85",
#	"color9": "#6D6253",
#	"color10": "#756755",
#	"color11": "#85725A",
#	"color12": "#897A67",
#	"color13": "#958774",
#	"color14": "#A4917A",
#	"color15": "#cbc6be",
#	"active": "#6a5b48",
#	"inactive": "#493e31",
#}

var _menu_item_name: String = " Update WPGTK Theme"
var _colors_file_path: String = "res://addons/wpgtk_theme/config/wpgtk-colors.txt"
var _cache_colors_file_path: String = "res://addons/wpgtk_theme/config/wpgtk-colors-cache.txt"
var _config_colors: Dictionary = {}
var auto_update: bool = true
var _last_modified_time: int = 0

func _enter_tree() -> void:
	add_tool_menu_item(_menu_item_name, _button_pressed)
	set_process(true)
	_last_modified_time = FileAccess.get_modified_time(_colors_file_path)
	
	
func _exit_tree() -> void:
	remove_tool_menu_item(_menu_item_name)
	set_process(false)


func _process(delta: float) -> void:
	if !auto_update: return
	var current_modified_time: int = FileAccess.get_modified_time(_colors_file_path)
	#print("Previous modified time: ", _last_modified_time, " Current modified time: ", current_modified_time)
	if current_modified_time != _last_modified_time:
		print("Colors file has been modified")
		_last_modified_time = current_modified_time
		_load_colors()
	
func _button_pressed() -> void:
	_load_colors()
	

func _load_colors() -> void:
	var file: FileAccess = FileAccess.open(_colors_file_path, FileAccess.READ)
	if file == null:
		printerr("Error loading colors file: ", FileAccess.get_open_error())
		return
		
	var cache_file: FileAccess = FileAccess.open(_cache_colors_file_path, FileAccess.READ_WRITE)
	if cache_file == null:
		printerr("Error loading cache colors file: ", FileAccess.get_open_error())
		return
		
	_config_colors.clear()
	_config_colors = JSON.parse_string(file.get_as_text())
	var _config_colors_cache: Dictionary = JSON.parse_string(cache_file.get_as_text())

	# Check if the colors from the cache are the same
	if str(_config_colors_cache) == str(_config_colors):
		print("Colors are the same, no need to update")
		return

	# Save the new colors to the cache file
	_config_colors_cache = _config_colors.duplicate()
	cache_file.store_string(JSON.stringify(_config_colors_cache))
	
	file.close()
	cache_file.close()
	
	# Convert hex colors to Color objects
	var colors_copy: Dictionary = _config_colors.duplicate()
	for key: String in colors_copy.keys():
		_config_colors[key] = Color(_config_colors[key])
	
	_update_theme()

	
func _update_theme() -> void:
	var editor_settings: EditorSettings = EditorInterface.get_editor_settings()
	editor_settings.set("interface/theme/base_color", _config_colors["inactive"])
	editor_settings.set("interface/theme/accent_color", _config_colors["active"])
	EditorInterface.restart_editor() # Restart the editor to apply the new colors, not sure why this is needed, but its the only way I found to make it work
	
