require("lQuery")
local configurator = require("configurator.configurator")
local utils = require "plugin_mechanism.utils"
local completeMetamodel = require "OWLGrEd_UserFields.completeMetamodel"
local profileMechanism = require "OWLGrEd_UserFields.profileMechanism"
local syncProfile = require "OWLGrEd_UserFields.syncProfile"
local viewMechanism = require "OWLGrEd_UserFields.viewMechanism"

lQuery.model.add_property("AA#StyleSetting", "procSetValue")

utils.copy(tda.GetProjectPath() .. "\\Plugins\\OWLGrEd_UserFields\\aaViewVertical.BMP",
           tda.GetProjectPath() .. "\\Pictures\\OWLGrEd_UserFields_aaViewVertical.BMP")
		   
utils.copy(tda.GetProjectPath() .. "\\Plugins\\OWLGrEd_UserFields\\aaViewVerticalActivated.BMP",
           tda.GetProjectPath() .. "\\Pictures\\OWLGrEd_UserFields_aaViewVerticalActivated.BMP")

lQuery("AA#View[showInPalette='true']"):each(function(view)
		--local picture = lQuery("ToolbarElementType[caption=" .. view:attr("name") .. "]"):attr("picture")
		--if picture~=nil then utils.delete(tda.GetProjectPath() .. "\\Pictures\\"..picture) end
		lQuery("ToolbarElementType[caption=" .. view:attr("name") .. "]"):delete()
end)
		   
local pathContextType = tda.GetProjectPath() .. "\\Plugins\\OWLGrEd_UserFields\\user\\AutoLoad"
local fileTable = syncProfile.attrdir(pathContextType)
for i,v in pairs(fileTable) do
	local profileNameStart = string.find(v, "/")
	local profileName = string.sub(v, profileNameStart+1, string.len(v)-4)

	local profile = lQuery("AA#Profile[name='" .. profileName .. "']")
	--izdzest AA# Dalu
	lQuery(profile):find("/field"):each(function(obj)
		profileMechanism.deleteField(obj)
	end)
	--saglabajam stilus
	lQuery("GraphDiagram:has(/graphDiagramType[id='OWL'])"):each(function(diagram)
		utilities.execute_cmd("SaveDgrCmd", {graphDiagram = diagram})
	end)
	--palaist sinhronizaciju
	syncProfile.syncProfile(profileName)
	viewMechanism.deleteViewFromProfile(profileName)
		--izdzest profilu, extension
	lQuery(profile):delete()
	lQuery("Extension[id='" .. profileName .. "']"):delete()
end

completeMetamodel.loudAutoLoudProfiles(pathContextType)



lQuery("AA#View[showInPalette='true']"):each(function(view)
	--local view = lQuery("AA#Profile[name='PaletteViews']/view[name='CompactHorizontalView']")
	--local view = lQuery("AA#Profile[name='PaletteViews']/view[name='Horizontal']")
	local owl_dgr_type = lQuery("GraphDiagramType[id=OWL]")
	local toolbarTypeOwl = owl_dgr_type:find("/toolbarType")
	if toolbarTypeOwl:is_empty() then
	  toolbarTypeOwl = lQuery.create("ToolbarType", {graphDiagramType = owl_dgr_type})
	end
		
	local view_manager_toolbar_el = lQuery.create("ToolbarElementType", {
		toolbarType = toolbarTypeOwl,
		id = view:id(),
		caption = view:attr("name"),
		picture = view:attr("inActiveIcon"),
		procedureName = "OWLGrEd_UserFields.styleMechanism.applyViewFromToolBar"
	})	
end)

configurator.make_toolbar(lQuery("GraphDiagramType[id=projectDiagram]"))
configurator.make_toolbar(lQuery("GraphDiagramType[id=OWL]"))


return true
-- return false, error_string