function getNameServer()

	return GetRealmName()

end

function JojoMonkPanel_Close()
	JojoMonk_Config.ActiveSFX = JojoMonkGUIFrame_CBSFXActive:GetChecked();
	if(L_UIDropDownMenu_GetText(JojoMonk_CharacterDropDown) ~= L['no_addon_load']) then
		JojoMonk_Config.ActiveCharacter = L_UIDropDownMenu_GetText(JojoMonk_CharacterDropDown);
	else
		L_UIDropDownMenu_SetText(JojoMonk_CharacterDropDown, JojoMonk_Config.ActiveCharacter);
	end
	--JojoMonk_Config.lang = L_UIDropDownMenu_GetText(JojoMonk_LanguageDropDown);
end

function  JojoMonkPanel_CancelOrLoad()

	
	JojoMonkGUIFrame_CBSFXActive:SetChecked(JojoMonk_Config.ActiveSFX);
	
	L_UIDropDownMenu_SetText(JojoMonk_CharacterDropDown, JojoMonk_Config.ActiveCharacter);
	--L_UIDropDownMenu_SetText(JojoMonk_LanguageDropDown, JojoMonk_Config.lang);
end



function JojoMonk_OnLoad(frame)
	realm = getNameServer();
	-- respond to the Player logging in.
    --
	L = {};
	L.locale = GetLocale();
    frame:RegisterEvent("PLAYER_ENTERING_WORLD");
end







function JojoMonk_Initialise()
	if(JojoMonk_Init) then
		return
	end
	 -- initialise data structures
    --
    if (JojoMonk_Config == nil) then
        JojoMonk_Config = { };
		JojoMonk_Config.lang = "English";
		JojoMonk_Config.ActiveSFX = true;
		JojoMonk_Config.ActiveCharacter = L['Dio_Brando_drop'];
    end

        
	
	--Create and initialise the drop down frame for character choose
	CreateFrame("Frame", "JojoMonk_CharacterDropDown", JojoMonkGUIFrame, "L_UIDropDownMenuTemplate");
	L_UIDropDownMenu_Initialize(JojoMonk_CharacterDropDown, JojoMonk_CharacterDropDown_Init);
	L_UIDropDownMenu_JustifyText(JojoMonk_CharacterDropDown, "LEFT");
	L_UIDropDownMenu_SetWidth(JojoMonk_CharacterDropDown, 150);
	L_UIDropDownMenu_SetText(JojoMonk_CharacterDropDown, JojoMonk_Config.ActiveCharacter);

	JojoMonk_CharacterDropDown:Show();
	--[[
	--Create and initialise the drop down frame for language choose
	CreateFrame("Frame", "JojoMonk_LanguageDropDown", JojoMonkGUIFrame, "L_UIDropDownMenuTemplate");
	L_UIDropDownMenu_Initialize(JojoMonk_LanguageDropDown, JojoMonk_LanguageDropDown_Init);
	L_UIDropDownMenu_JustifyText(JojoMonk_LanguageDropDown, "LEFT");
	L_UIDropDownMenu_SetWidth(JojoMonk_LanguageDropDown, 150);
	L_UIDropDownMenu_SetText(JojoMonk_LanguageDropDown, GetLocalLanguageListView()[JojoMonk_Config.lang]); 

	JojoMonk_LanguageDropDown:Hide(); -- desactive language option due to unworking
	
	]]
	
	
	
	JojoMonk_Init = 1;
end



function JojoMonk_CharacterDropDown_Init(self)
	-- Gets the current text in the DropDown frame
	--
	local ddtext = L_UIDropDownMenu_GetText(JojoMonk_CharacterDropDown);
	local info = L_UIDropDownMenu_CreateInfo();
	for index, filename in ipairs(GetLocalCharacterListView()) do
		info.text = filename;
		-- Give it a tick instead of a radio button, and only tick when selected
		--
		info.isNotRadio = true;
		info.checked = (filename == ddtext);

		-- Function to be called when the menu option is selected
		--
		info.func = function (self)
			-- Sets the text of the DropDown frame
			--
			L_UIDropDownMenu_SetText(JojoMonk_CharacterDropDown, self:GetText());
		end

		L_UIDropDownMenu_AddButton(info);
	end
end
--[[
function JojoMonk_LanguageDropDown_Init(self)
	-- Gets the current text in the DropDown frame
	--
	local ddtext = L_UIDropDownMenu_GetText(JojoMonk_LanguageDropDown);
	local info = L_UIDropDownMenu_CreateInfo();
	for index, filename in ipairs(GetLocalLanguageListView()) do
		info.text = filename;
		print("info text lang:");
		print(info.text);
		-- Give it a tick instead of a radio button, and only tick when selected
		--
		info.isNotRadio = true;
		info.checked = (filename == ddtext);

		-- Function to be called when the menu option is selected
		--
		info.func = function (self)
			-- Sets the text of the DropDown frame
			--
			L_UIDropDownMenu_SetText(JojoMonk_LanguageDropDown, self:GetText());
		end

		L_UIDropDownMenu_AddButton(info);
	end
end
]]
function JojoMonk_OnEvent(self, event, ...)

	if (event == "PLAYER_ENTERING_WORLD") then
		-- initialise configuration and frames
		JojoMonk_Initialise();
		
		
		
		JojoMonkGUIFrame_CBSFXActive:SetChecked(JojoMonk_Config.ActiveSFX);
		local isLoad = isActiveCharacterLoad();
		

		if(isLoad == false) then
			JojoMonk_Config.ActiveCharacter = "";
		end
		L_UIDropDownMenu_SetText(JojoMonk_CharacterDropDown, JojoMonk_Config.ActiveCharacter);
		--L_UIDropDownMenu_SetText(JojoMonk_LanguageDropDown, JojoMonk_Config.lang);
	end
end

function isActiveCharacterLoad() -- Verify if our active character is load this time for correct display on the interface

	for index, filename in ipairs(GetLocalCharacterListView()) do 
			if(JojoMonk_Config.ActiveCharacter == filename) then return true; end
		end
	return false;
end

function JojoMonk_PanelOnload(panel)
		
		
		-- Set the text for the check box
		JojoMonkGUIFrame_CBSFXActiveText:SetText(L['Active_SFX']);
		JojoMonkGUIFrame_CBSFXActiveText:SetWidth(80); -- Set the width of the text
		-- Set the text for the frontstring language
		
		--JojoMonkGUIFrame_Language:SetText(L['Language']);	
		--JojoMonkGUIFrame_Language:SetFont(JojoMonkGUIFrame_Language:GetFont(),13);
		-- Set the name for the Category for the Panel
        --
        panel.name = "Jojo Monk SFX " .. GetAddOnMetadata("JojoMonk", "Version");
        -- When the player clicks okay, run this function.
        --
        panel.okay = function (self) JojoMonkPanel_Close(); end;

        -- When the player clicks cancel, run this function.
        --
        panel.cancel = function (self)  JojoMonkPanel_CancelOrLoad();  end;

        -- Add the panel to the Interface Options
        --
        InterfaceOptions_AddCategory(panel);

end