--------------------------------------------------------------------------------
-- INITIALIZATION
--------------------------------------------------------------------------------
local CC = userMods.GetGlobalConfigSection("CrosshairChange")
local IconTable = {"NormalCrosshair","ExtraCrosshair","ExtraCrosshair2","ExtraCrosshair3","ExtraCrosshair4","ExtraCrosshair5","ExtraCrosshair6","ExtraCrosshair7",}
local TablePosition = 1

local function UpdateCrosshair()
	for i, v in stateMainForm:GetNamedChildren() do
		if v:GetName() == "ContextShipDeviceCrosshair" then
			v:SetBackgroundTexture(common.GetAddonRelatedTexture(IconTable[(CC["Icon"])]))
			v:SetBackgroundBlendEffect(BLEND_EFFECT_ALPHABLND)
			v:SetBackgroundColor(CC["Color"])
		end
	end
end

--CONFIG WINDOW COOPERATION
function ConfigInitEvent()
	userMods.SendEvent("CONFIG_INIT_EVENT_RESPONSE", { sender = common.GetAddonName() })
end

function ConfigEvent()
	userMods.SendEvent("CONFIG_EVENT_RESPONSE", {NoB = "Icon", name = "Icon", btnType = "Tabled", texture = common.GetAddonRelatedTexture(IconTable[(CC["Icon"])]), sizeY = 64, sizeX = 64})
	userMods.SendEvent("CONFIG_EVENT_RESPONSE", {NoB = "Color", name = "Color", btnType = "Color", color = CC["Color"]})
end

function ConfigTabled(p)
	if p.dir == "+" then
		TablePosition = TablePosition + 1
	elseif p.dir == "-" then
		TablePosition = TablePosition - 1
	end
	local num = 0
	for i, v in IconTable do
		num = num + 1
	end
	if IconTable[TablePosition] then
	elseif TablePosition > 1 then
		TablePosition = 1
	elseif TablePosition < 1 then
		TablePosition = num
	end
	CC["Icon"] = TablePosition
	UpdateCrosshair()
	userMods.SetGlobalConfigSection("CrosshairChange", CC)
	userMods.SendEvent("CONFIG_CHANGE_BUTTON_RESPONSE", {NoB = "Icon", name = "Icon", texture = common.GetAddonRelatedTexture(IconTable[(CC["Icon"])]), sizeY = 64, sizeX = 64})
end

function ConfigColor(p)
	CC["Color"] = p.color
	UpdateCrosshair()
	userMods.SetGlobalConfigSection("CrosshairChange", CC)
end

function Init()
	if CC == nil then
		CC = {}
		CC["Icon"] = 1
		CC["Color"] = {r=1;g=0;b=0;a=1}
		userMods.SetGlobalConfigSection("CrosshairChange", CC)
	end
	TablePosition = CC["Icon"]
	UpdateCrosshair()
	--CONFIG WINDOW EVENTS (INIT, EVENT, COLOR, TABLED)
	common.RegisterEventHandler( ConfigInitEvent, "CONFIG_INIT_EVENT" )
	common.RegisterEventHandler( ConfigEvent, "CONFIG_EVENT_"..common.GetAddonName())
	common.RegisterEventHandler( ConfigTabled, "CONFIG_TABLED_"..common.GetAddonName())
	common.RegisterEventHandler( ConfigColor, "CONFIG_COLOR_"..common.GetAddonName())
end
--------------------------------------------------------------------------------
common.RegisterEventHandler( Init, "EVENT_AVATAR_CREATED")
if avatar.IsExist() then
Init()
end
--------------------------------------------------------------------------------