local _G = _G
local core = InvenSimulatorUI

local of = CreateFrame("Frame", "InvenSimulatorOtherCharFrame", UIParent, "UIPanelDialogTemplate")
of:Hide()
of:SetFrameStrata(core:GetFrameStrata())
of:SetToplevel(true)

function of:CreateFrame()
	self.levelElements = {}
	self:SetScript("OnShow", nil)
	self:SetWidth(305)
	self:SetHeight(184)
	self.title:SetText("인벤 시뮬레이터 - 다른 캐릭터")
	InvenSimulatorCharDB.otherCharPos = InvenSimulatorCharDB.otherCharPos or { "CENTER", 0, 0 }
	self:SetPoint(unpack(InvenSimulatorCharDB.otherCharPos))
	self.levelElements[InvenSimulatorOtherCharFrameClose] = 1
	self:EnableMouse(true)
	self:SetClampedToScreen(true)
	self:SetMovable(true)
	self:RegisterForDrag("LeftButton")
	self:SetScript("OnDragStart", self.StartMoving)
	self:SetScript("OnDragStop", function(self)
		self:StopMovingOrSizing()
		InvenSimulatorUI.db.otherCharPos[1], _, _, InvenSimulatorUI.db.otherCharPos[2], InvenSimulatorUI.db.otherCharPos[3] = self:GetPoint()
	end)
	self.slots = {}
	local slotList = { 1, 2, 3, 15, 5, 4, 19, 9, 10, 6, 7, 8, 11, 12, 13, 14, 16, 17, 18 }
	local function buttonOnEnter(self)
		GameTooltip:SetOwner(self, "ANCHOR_RIGHT")
		if of.db and of.db[self:GetID()] then
			GameTooltip:SetHyperlink(of.db[self:GetID()])
		elseif self:GetID() == 18 then
			GameTooltip:AddLine(RANGEDSLOT)
		else
			GameTooltip:AddLine(InvenSimulatorUI.slots[self:GetID()].tooltipText)
		end
		GameTooltip:Show()
	end
	for i = 1, 19 do
		self.slots[i] = CreateFrame("Button", "InvenSimulatorOtherCharFrameSlot"..i, self)
		self.levelElements[self.slots[i]] = 1
		self.slots[i]:SetID(i)
		self.slots[i]:SetWidth(36)
		self.slots[i]:SetHeight(36)
		self.slots[i]:SetScript("OnEnter", buttonOnEnter)
		self.slots[i]:SetScript("OnLeave", GameTooltip_Hide)
		self.slots[i].icon = self.slots[i]:CreateTexture(nil, "OVERLAY")
		self.slots[i].icon:SetAllPoints()
	end
	for p, v in ipairs(slotList) do
		if v == 1 then
			self.slots[v]:SetPoint("TOPLEFT", self, 10, -66)
		elseif v == 10 then
			self.slots[v]:SetPoint("TOP", self.slots[slotList[1]], "BOTTOM", 0, 0)
		elseif v == 16 then
			self.slots[v]:SetPoint("TOP", self.slots[slotList[9]], "BOTTOM", 0, 0)
		else
			self.slots[v]:SetPoint("LEFT", self.slots[slotList[p - 1]], "RIGHT", 0, 0)
		end
	end

	self.list = {}
	for p in pairs(InvenSimulatorSaveDB) do
		tinsert(self.list, p)
	end
	sort(self.list)
	InvenSimulatorCharDB.lastShowChar = InvenSimulatorCharDB.lastShowChar or self.list[1]
	local dropdownFunc = function(self)
		InvenSimulatorCharDB.lastShowChar = self.value
		UIDropDownMenu_SetSelectedValue(self.owner, self.value)
		of:UpdateChar()
	end
	local dropdownInitialize = function(self)
		local info = UIDropDownMenu_CreateInfo()
		for _, text in ipairs(of.list) do
			info.text = text
			info.value = text
			info.func = self.func
			info.owner = UIDROPDOWNMENU_OPEN_MENU
			info.checked = InvenSimulatorCharDB.lastShowChar == text and 1 or nil
			UIDropDownMenu_AddButton(info)
		end
	end
	self.dropdown = CreateFrame("Frame", "InvenSimulatorOtherCharFrameDropdown", self, "UIDropDownMenuTemplate")
	self.levelElements[self.dropdown] = 1
	self.dropdown:SetID(1)
	self.dropdown:EnableMouse(true)
	self.dropdown:SetPoint("TOP", self, "TOP", 0, -25)
	UIDropDownMenu_SetWidth(self.dropdown, 270)
	UIDropDownMenu_JustifyText(self.dropdown, "CENTER")
	self.dropdown.func = dropdownFunc
	self.dropdown.Initialize = dropdownInitialize
	UIDropDownMenu_Initialize(self.dropdown, self.dropdown.Initialize)
	UIDropDownMenu_SetSelectedValue(self.dropdown, InvenSimulatorCharDB.lastShowChar)
	self.charInfo = self:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
	self.charInfo:SetPoint("TOP", self.dropdown, "BOTTOM", 0, 5)
	self.delete = CreateFrame("Button", nil, self, "UIPanelButtonTemplate")
	self.levelElements[self.delete] = 1
	self.delete:SetPoint("BOTTOMRIGHT", -6, 10)
	self.delete:SetWidth(60)
	self.delete:SetHeight(22)
	self.delete:SetText("삭제")
	self.delete:SetScript("OnClick", function()
		for p, v in pairs(of.list) do
			if v == InvenSimulatorCharDB.lastShowChar then
				tremove(of.list, p)
				InvenSimulatorSaveDB[v] = nil
				break
			end
		end
		if of.list[1] then
			InvenSimulatorCharDB.lastShowChar = of.list[1]
			UIDropDownMenu_Initialize(self.dropdown, self.dropdown.Initialize)
			UIDropDownMenu_SetSelectedValue(self.dropdown, InvenSimulatorCharDB.lastShowChar)
			of:UpdateChar()
		else
			of:Hide()
			InvenSimulatorUIOpenOtherCharFrame:Disable()
		end
	end)
	self:UpdateChar()
	self:Show()
end

function of:UpdateChar()
	self.db = InvenSimulatorSaveDB[InvenSimulatorCharDB.lastShowChar]
	self.charInfo:SetText(self.db.info)
	for i = 1, 19 do
		if self.db[i] then
			self.slots[i].icon:SetTexture(GetItemIcon(self.db[i]))
		elseif i == 18 then
			self.slots[i].icon:SetTexture("Interface\\Paperdoll\\UI-PaperDoll-Slot-Ranged")
		else
			self.slots[i].icon:SetTexture(InvenSimulatorUI.slots[i].backgroundTextureName)
		end
	end
end

of:SetScript("OnShow", of.CreateFrame)