--HIỆN POPUP THÔNG BÁO NHIỆM VỤ HOÀN THÀNH hoặc CẬP NHẬT BƯỚC MỚI MỖI KHI XONG 1 PHẦN NHIỆM VỤ
--CALL FUNCTION: VLF.QuestUpdatedPopIn()

require "ISUI/ISPanelJoypad"
require "ISUI/ISUIElement"
require "ISUI/ISPanel"
require "ISUI/ISLabel"
require "questui/vl_quest_list_ui"

VL_Quest_PopUp_UI = ISPanel:derive("VL_Quest_PopUp_UI")

assert(VLF.QuestUI,  "Stack trace: Missing dependency: VLF.QuestUI - vl_quest_popup_ui.lua")

local PlaySound = VLF.PlaySound
local DelayFunction = VLF.DelayFunction
local VLF_QuestUI = VLF.QuestUI

local widthPop = 250
local heightPop = 100
local uiPosition = {}

function VL_Quest_PopUp_UI:createChildren()
    DelayFunction(function()
        self:close()
    end, 240) -- ~4s
	
	DelayFunction(function()
		--refresh các thông tin hiển thị trên bảng quest và quest text trên màn hình
		VLF_QuestUI.UpdateQuestListDisplay()
    end, 60) -- ~1s
	
	ISPanel.createChildren(self)
end

function VL_Quest_PopUp_UI:prerender()
	local iconType = self.iconType
	local state = self.state
	
    if not self.imageBackground then
		local texture = getTexture("media/ui/background250x100.png")
        if texture then
            self.imageBackground = self:drawTextureScaled(texture, 0, 0, self.width, self.height, 1)
        end
    end
	
	self:drawRectBorder(0, 0, self.width, self.height, 1, 1, 1, 1)
	
    if not self.imageOnBackground then
		local texture = getTexture("media/ui/spiffo_bw1.png")
        if texture then
            self.imageOnBackground = self:drawTextureScaled(texture, 115, 0, 140, 100, 0.2)
        end
    end

    if not self.iconOnBackground then
		local texture = getTexture("media/ui/questicon_128.png")
		local x, y, w, h, a = 3, 7, 95, 95, 0.6
		
		if iconType == "radio_RS" or iconType == "radio_CB" then
			texture = getTexture("media/ui/walkietalkie_128.png")
			x, y, w, h, a = -13, -20, 120, 120, 1
		end
		--[[
		if iconType == "radio_RS" then
			texture = getTexture("media/ui/walkietalkie2_128.png")
			x, y, w, h, a = 7, 0, 95, 95, 0.6
		end
		]]
		
        if texture then
            self.iconOnBackground = self:drawTextureScaled(texture, x, y, w, h, a)
        end
    end
	
	if iconType then
		if not self.subIconOnBackground then
			local texture
			--local x, y, w, h, a = 150, 0, 95, 95, 0.6
			local x, y, w, h, a = 140, -10, 110, 110, 1
			
			if iconType == "radio_RS" then
				texture = getTexture("media/ui/reliefsupplydrop_128.png")
				x, y, w, h, a = 140, -8, 110, 110, 1
				
				if state == "looted" then
					texture = getTexture("media/ui/reliefsupplydrop2_128.png")
				end
			elseif iconType == "radio_CB" then
				texture = getTexture("media/ui/carpetbombing_128.png")
				--texture = getTexture("media/ui/reliefsupplydrop_128.png")	--FIXME: TEST
				--x, y, w, h, a = 140, -8, 110, 110, 1	--FIXME: TEST
				
				if state == "start" then
					texture = getTexture("media/ui/carpetbombing4_128.png")
					x, y, w, h, a = 137, -13, 120, 120, 1
				elseif state == "cancel" then
					texture = getTexture("media/ui/carpetbombing3_128.png")
					x, y, w, h, a = 140, -8, 115, 115, 1
				end
			end
			
			if texture then
				self.subIconOnBackground = self:drawTextureScaled(texture, x, y, w, h, a)
			end
		end
	end

    if not self.textOnWindow then
		local text = getText("IGUI_VLQ_Updated")
		local font = UIFont.Medium
		local x, y, height = 105, 45, 10
		local r, g, b, a = 0.5529, 0.9373, 0.6549, 1
		
		if iconType == "radio_RS" or iconType == "radio_CB" then
			text = getText("IGUI_VLQ_Signal")
			font = UIFont.NewSmall
			x, y, height = (self.width / 2) - (getTextManager():MeasureStringX(font, text) / 2) - 20, 40, 10
			
			local text2 = getText("IGUI_VLQ_Received")
			local x2, y2, height2 = (self.width / 2) - (getTextManager():MeasureStringX(font, text2) / 2) - 20, 60, 10
			self.textOnWindow2 = ISLabel:new(x2, y2, height2, text2, r, g, b, a, font, true)
			self:addChild(self.textOnWindow2)
		end
		
		self.textOnWindow = ISLabel:new(x, y, height, text, r, g, b, a, font, true)
		self:addChild(self.textOnWindow)
    end
end

function VL_Quest_PopUp_UI:render()
	ISPanel.render(self)
	--self:drawRectBorder(0, 0, self.width, self.height, 1, 1, 1, 1)
end

function VL_Quest_PopUp_UI:close()
	self:setVisible(false)
	self:removeFromUIManager()
	uiPosition[self.uiIndex] = nil
end

function VL_Quest_PopUp_UI:new(character, iconType, state, uiIndex, x, y)
    local playerNum = character:getPlayerNum()

	local o = ISPanel.new(self, x, y, width, height)

	o.width = widthPop
	o.height = heightPop
	o.chr = character
	o.iconType = iconType
	o.state = state
	o.uiIndex = uiIndex
    o.playerNum = playerNum
	o.moveWithMouse = false
	o.anchorRight = true
	o.anchorBottom = true
    o.keepOnScreen = true
	return o
end

local function GetFreeUIPosition()
	local playerNum = getPlayer() and getPlayer():getPlayerNum()
	local screenWidth = getPlayerScreenWidth(playerNum)
	local x = (screenWidth/2) - (widthPop/2)
	local y = screenWidth/25
	local space = 20
	local row = 1

	for i = 1, 10 do
		if uiPosition[i] == nil then
		
			--hàng 1
			if i == 1 then
				x = x
			elseif i == 2 then
				x = (screenWidth/2) - (widthPop/2) - widthPop - space
			elseif i == 3 then
				x = (screenWidth/2) - (widthPop/2) + widthPop + space
			elseif i == 4 then
				x = (screenWidth/2) - (widthPop/2) - widthPop * 2 - space * 2
			elseif i == 5 then
				x = (screenWidth/2) - (widthPop/2) + widthPop * 2 + space * 2
			
			--hàng 2
			elseif i == 6 then
				x = (screenWidth/2) - (widthPop/2)
				y = screenWidth/25 + heightPop + space
			elseif i == 7 then
				x = (screenWidth/2) - (widthPop/2) - widthPop - space
				y = screenWidth/25 + heightPop + space
			elseif i == 8 then
				x = (screenWidth/2) - (widthPop/2) + widthPop + space
				y = screenWidth/25 + heightPop + space
			elseif i == 9 then
				x = (screenWidth/2) - (widthPop/2) - widthPop * 2 - space * 2
				y = screenWidth/25 + heightPop + space
			elseif i == 10 then
				x = (screenWidth/2) - (widthPop/2) + widthPop * 2 + space * 2
				y = screenWidth/25 + heightPop + space
			end

			return i, x, y
		end
	end

	return 1, x, y
end

-------------------------------------------------------
-- CALL ĐỂ POP UP XUẤT HIỆN		
--iconType: "quest", "radio_RS", "radio_CB"
--state: "start", "cancel"
function VLF.QuestUpdatedPopIn(iconType, state)
	iconType = iconType or "quest"
	state = state or ""
	
    local character = getPlayer()
	local uiIndex, x, y = GetFreeUIPosition()

    uiPosition[uiIndex] = VL_Quest_PopUp_UI:new(character, iconType, state, uiIndex, x, y)
    uiPosition[uiIndex]:initialise()
    uiPosition[uiIndex]:addToUIManager()
    PlaySound(character, "questcompleted_1")
end





