InvenSimulator = CreateFrame("Frame")
InvenSimulator:SetScript("OnEvent", function(self, event, ...) self[event](self, ...) end)
InvenSimulator:RegisterEvent("ADDON_LOADED")

BluePrint = BluePrint or function() end

local _G = getfenv(0)
local type = _G.type
local UnitClass = _G.UnitClass
local UnitSex = _G.UnitSex
local UnitRace = _G.UnitRace
local GetLocale = _G.GetLocale

BINDING_HEADER_INVENSIMULATOR = "인벤 시뮬레이터"
BINDING_NAME_INVENSIMULATOR_TOGGLE = "창 열기/닫기"

function InvenSimulator:ADDON_LOADED()
	self:UnregisterEvent("ADDON_LOADED")
	if self.icon then return end
	SLASH_INVENSIMULATOR1 = "/is"
	SLASH_INVENSIMULATOR2 = "/ㅑㄴ"
	SLASH_INVENSIMULATOR3 = "/시뮬"
	SLASH_INVENSIMULATOR4 = "/시뮬레이터"
	SlashCmdList["INVENSIMULATOR"] = self.Toggle
	self.class, self.race = self:GetClassAndRace()
	self.icon = "Interface\\Icons\\inv_pet_babyblizzardbear"
	self.addonName = BINDING_HEADER_INVENSIMULATOR
	self.version = GetAddOnMetadata("InvenSimulator", "Version")
	self.website = GetAddOnMetadata("InvenSimulator", "X-Website")
	InvenSimulatorDB = InvenSimulatorDB or { minimapButton = {} }
	InvenSimulatorDB.scale = InvenSimulatorDB.scale or 1
	self.db = InvenSimulatorDB
	LibStub("LibDataBroker-1.1"):NewDataObject("InvenSimulator", {
		type = "launcher",
		text = "InvenSimulator",
		OnClick = self.OnClick,
		icon = self.icon,
		OnTooltipShow = function(tooltip)
			if type(tooltip) == "table" and tooltip.AddLine then
				InvenSimulator:OnTooltip(tooltip)
			end
		end,
	})
	LibStub("LibMapButton-1.1"):CreateButton(self, "InvenSimulatorMapButton", self.icon, 142, self.db.minimapButton)
	self:CreateOptionFrame()
	self:HandleMapButton()
end

function InvenSimulator:GetClassAndRace()
	local class = UnitClass("player")
	if type(class) == "string" then
		class = class:trim()
		if class == "" then
			class = nil
		end
	else
		class = nil
	end
	local race = UnitRace("player")
	if type(race) == "string" then
		race = race:trim()
		if race == "" then
			race = nil
		end
	else
		race = nil
	end
	return class, race
end

function InvenSimulator:OnClick(button)
	if button == "RightButton" then
		InterfaceOptionsFrame_OpenToCategory(InvenSimulatorOptionFrame)
	else
		InvenSimulator:Toggle()
	end
end

function InvenSimulator:Toggle()
	if GetLocale() ~= "koKR" then
		return ChatFrame1:AddMessage("[인벤 시뮬레이터]: |cffffff00한글 클라이언트만 지원합니다.")
	elseif InvenSimulator.class == nil or InvenSimulator.race == nil then
		InvenSimulator.class, InvenSimulator.race = InvenSimulator:GetClassAndRace()
		if InvenSimulator.class == nil or InvenSimulator.race == nil then
			return ChatFrame1:AddMessage("[인벤 시뮬레이터]: |cffffff00캐릭터 정보 오류가 발생했습니다. 다른 애드온에서 캐릭터의 직업 혹은 종족을 불러오는 API 함수를 강제로 수정했거나 룩변환 또는 외부 폰트 사용이 원인일 수 있습니다.|r")
		end
	end
	if InvenSimulatorUI then
		if InvenSimulatorUI:IsShown() then
			InvenSimulatorUI:Hide()
		else
			InvenSimulatorUI:Show()
		end
	else
		InvenSimulator.sex2 = UnitSex("player") == 3 and "Female" or "Male"
		InvenSimulator.race2 = select(2, UnitRace("player"))
		LoadAddOn("InvenSimulatorUI")
	end
end

function InvenSimulator:OnTooltip(tooltip)
	tooltip = tooltip or GameTooltip
	tooltip:AddLine(InvenSimulator.addonName.." v"..InvenSimulator.version)
	tooltip:AddLine(InvenSimulator.website, 1, 1, 1)
	tooltip:AddLine("클릭: 창 열기", 1, 1, 0)
	tooltip:AddLine("우클릭: 옵션창 열기", 1, 1, 0)
end

function InvenSimulator:HandleMapButton()
	if InvenSimulatorMapButton then
		InvenSimulatorDB.minimapButton.show = not InvenSimulatorDB.mapbuttonHidden
		InvenSimulatorDB.minimapButton.dragable = not InvenSimulatorDB.mapbuttonLock
		if InvenSimulatorDB.minimapButton.show then
			InvenSimulatorMapButton:Show()
		else
			InvenSimulatorMapButton:Hide()
		end
	end

end