--ICON NHIỆM VỤ HIỂN THỊ TRÊN MÀN HÌNH - NÚT BẤM ĐỂ MỞ BẢNG THÔNG TIN NHIỆM VỤ
--Tự tạo khi nhân vật đăng nhập

require "OptionScreens/MainScreen"
require "ISUI/ISPanelJoypad"
require "ISUI/ISPanel"
require "questui/vl_quest_list_ui"

VL_Quest_Button_UI = ISPanel:derive("VL_Quest_Button_UI")

VLF.QuestUI = VLF.QuestUI or {}
local VLF_QuestUI = VLF.QuestUI
VLF.questPanelData = nil

local OpenQuestWindow = VLF.QuestUI.OpenQuestWindow
local width = getCore():getScreenWidth()

local function setSize()
    local size = 48
    if width > 3000 then size = 80
	elseif width > 2100 then size = 64
	else
		size = 48
    end
    return size
end

function VL_Quest_Button_UI:new(playerIndex, player, enabled)
	local bWidth = setSize()
	local bHeight = bWidth
	local panelData = {}
	panelData = ISPanel:new(getCore():getScreenWidth()/23, getCore():getScreenHeight()/7, bWidth, bHeight)
	setmetatable(panelData, self)
	self.__index = self
	
	panelData.playerIndex = playerIndex
	panelData.player = player
	panelData.player_isDead = false
	panelData.infopanel = getPlayerInfoPanel(player:getPlayerNum())
	panelData.moveWithMouse = true
	panelData.rightMouseDown = false
	panelData.dragging = false
    panelData.settingsPanel = nil
	panelData.enabled = enabled
	panelData:setVisible(enabled)
	return panelData
end

function VL_Quest_Button_UI:settingsPanelPos()
	local x = self:getX()
	local y = self:getY()-self.settingsPanel:getHeight()-2
	local width = self:getWidth()

	if y < 1 then
		y = self:getY() + self:getHeight() + 2
	end

	return x,y,width
end

function VL_Quest_Button_UI:prerender()
	local size = setSize()
	self:drawTexture(getTexture("media/ui/questicon_" .. tostring(size) .. ".png"),0,0, 0.85)
end

function VL_Quest_Button_UI:close()
    self:setVisible(false)
    self:removeFromUIManager()
end

function VL_Quest_Button_UI:onMouseDown(x, y)
	self.rightMouseDown = true
	ISPanel.onMouseDown(self, x, y)
end

function VL_Quest_Button_UI:onMouseMove(dx, dy)
	if self.rightMouseDown == true then
		self.dragging = true
	end
	ISPanel.onMouseMove(self, dx, dy)
end

function VL_Quest_Button_UI:onMouseUp(x, y)
	if not self.dragging and not self.player_isDead then
		getSoundManager():playUISound("UISelectListItem")
		--self.infopanel:toggleView(getText("IGUI_XP_Health"))
		--Quest_List_UI:new(getPlayer())
		OpenQuestWindow()
	end
	self.dragging = false
	self.rightMouseDown = false

	ISPanel.onMouseUp(self, x, y)
end

function VL_Quest_Button_UI:onMouseDoubleClick(x, y)
    self:setVisible(false)
    self:removeFromUIManager()
end

function VLF.CreatQuestIcon(idx, player)
	local _VLF = VLF
	
	if not _VLF.questPanelData then
		_VLF.questPanelData = VL_Quest_Button_UI:new(idx, player, true)
		_VLF.questPanelData:addToUIManager()
	end
end
local CreatQuestIcon = VLF.CreatQuestIcon
Events.OnCreatePlayer.Remove(CreatQuestIcon)
Events.OnCreatePlayer.Add(CreatQuestIcon)

local function OnPlayerDeath_RemoveQuestIcon(player)
	local _VLF = VLF
	
    if _VLF.questPanelData then
        _VLF.questPanelData:close()
        _VLF.questPanelData = nil
    end
end
Events.OnPlayerDeath.Remove(OnPlayerDeath_RemoveQuestIcon)
Events.OnPlayerDeath.Add(OnPlayerDeath_RemoveQuestIcon)

--============================================================--
--UI NÚT BẤM QUEST LIST Ở MAIN MENU KHI ESC
local mainScreenInstantiate = MainScreen.instantiate
--local OpenQuestWindow = VLF_QuestUI.OpenQuestWindow
--Tạo nút bấm ở Main Menu

function MainScreen:instantiate()
    mainScreenInstantiate(self)

    local mainScreen = MainScreenState.getInstance();
	if mainScreen ~= nil and (ISDemoPopup.instance == nil) then
		local player = getPlayer()
        if player then
            local playerNum = player:getPlayerNum()
            local width = 170
            local height = 50
            local x = getPlayerScreenWidth(playerNum) - width - 10
            local y = getPlayerScreenHeight(playerNum) / 2

            local questListButton = ISButton:new(x, y, width, height, getText("IGUI_VLQ_TitleUI"), self, VLF_QuestUI.OpenQuestWindow)
            self:addChild(questListButton)

        end
	end
end