--================ Take Care - Health Info UI =================--
--CỬA SỔ TÌNH TRẠNG SỨC KHỎE CỦA NPC
--CALL FUNCTION: VLF.QuestUI.NpcHealthWindow(player, qStep)
--=============================================================--

require "ISUI/ISPanelJoypad"
require "ISUI/ISUIElement"
require "ISUI/ISCollapsableWindow"
require "ISUI/ISScrollingListBox"

VL_Quest_NPC_Health_Info = ISCollapsableWindow:derive("VL_Quest_NPC_Health_Info")

VLF.QuestUI = VLF.QuestUI or {}

local SplitTextByLength = VLF.SplitTextByLength
local GetPlayerGlobalModDataQ1 = VLGMD.GetPlayerGlobalModDataQ1
local Caldistance = VLF.Caldistance

local fontHeightSmall = getTextManager():getFontHeight(UIFont.Small)    --26
local fontHeightMedium = getTextManager():getFontHeight(UIFont.Medium)
local fontHeightLarge = getTextManager():getFontHeight(UIFont.Large)

function VL_Quest_NPC_Health_Info:createChildren()
    ISCollapsableWindow.createChildren(self)
end

function VL_Quest_NPC_Health_Info:prerender()
    ISCollapsableWindow.prerender(self)
end

function VL_Quest_NPC_Health_Info:render()
    ISCollapsableWindow.render(self)
    
    --Nếu cách xa NPC thì tự đóng
    if Caldistance(self.player, self.zNPC) > 6 then
        self:close()
    end
end

function VL_Quest_NPC_Health_Info:close()
    self:setVisible(false)
    self:removeFromUIManager()
    
    --reset window
    VLF.var.showNpcHealthWindow = nil
end

--Close khi ấn ESC
function VL_Quest_NPC_Health_Info:onKeyPress(key)
    if key == Keyboard.KEY_ESCAPE then
        self:close()
    end
end

function VL_Quest_NPC_Health_Info:new(player, width, height, qStep, zNPC)
    local playerNum = player:getPlayerNum()
    local x = (getPlayerScreenWidth(playerNum) / 16)
    local y = (getPlayerScreenHeight(playerNum) / 16)

    local o = ISCollapsableWindow.new(self, x, y, width, height)
    o.player = player
    o.qStep = qStep
    o.zNPC = zNPC
    o.playerNum = playerNum
    o.moveWithMouse = true
    o.anchorLeft = true
    o.anchorRight = true
    o.anchorTop = true
    o.anchorBottom = true
    o.resizable = false
    o:setWantKeyEvents(true)
    return o
end

function VL_Quest_NPC_Health_Info:initialise()
    ISCollapsableWindow.initialise(self)

    local y = fontHeightSmall * 2
    
    --Khởi tạo khu vực trong UI có thể scroll chuột
    self.questList = ISScrollingListBox:new(10, y, self.width - 20, self.height - 20 - y)
    self.questList:initialise()
    self.questList:instantiate()
    self.questList.itemheight = self.height - 20 - y
    
    self.questList.selected = 0
    self.questList.joypadParent = self
    self.questList.font = UIFont.NewSmall
    self.questList.doDrawItem = self.drawQuestItem
    self.questList.drawBorder = true
    self:addChild(self.questList)
    ------------------------------------------------------------------

    local qStep = self.qStep
    local name = VLF.npcData[qStep].name

    self.questList:addItem("NPCHealthInfo_" .. qStep, {qStep = qStep, name = name, health = getText("IGUI_VLQ_TakeCare_HealthInfo_" .. qStep)})
end

--DRAW TẤT CẢ CÁC ITEM Ở addItem
function VL_Quest_NPC_Health_Info:drawQuestItem(y, item, alt)
    --local a = 0.8
    local r, g, b, a = 0.98, 0.98, 0.98, 1
    local xOffset = 20
    local yOffset = y + 10
    local iData = item.item
    local itemHeight = self.itemheight
    
    --Cập nhật dữ liệu NPC Health Info tại đây để bảng Info luôn cập nhật dữ liệu mới
    local hunger, thirst, dirty, poisoned
    local qStep = iData.qStep
    local playerGMD_Quest_Q1 = GetPlayerGlobalModDataQ1(getPlayer(), qStep)
    if playerGMD_Quest_Q1[qStep].npcLastUpdateTime then
        hunger = playerGMD_Quest_Q1[qStep].npcHunger
        thirst = playerGMD_Quest_Q1[qStep].npcThirst
        --dirty = playerGMD_Quest_Q1[qStep].npcDirty
        poisoned = playerGMD_Quest_Q1[qStep].npcPoisoned
    end
 
    --XỬ LÝ TEXT
    --Thay văn bản thuần <LINE> thành ký tự ASCII 10 \n để nhận dạng lệnh xuống dòng
    local text = iData.health:gsub("<LINE>", "\n")

    --Chia dòng theo \n
    local lines = {}
    for line in text:gmatch("([^\n]+)") do
        table.insert(lines, line)
    end
    
    --Chia dòng khi vượt quá giới hạn ký tự
    local _lines = {}
    for _,line in ipairs(lines) do         
        local result = SplitTextByLength(line, self.width * 0.65, UIFont.NewSmall) --100
        for i, l in ipairs(result) do
            table.insert(_lines, l)
        end
    end
    
    --Tăng chiều cao cho border nếu có nhiều dòng text
    if #_lines > 1 then
        local i = #_lines - 1
        itemHeight = itemHeight + i * fontHeightSmall
    end
    
    --Vẽ đường viền mỗi ô
    self:drawRectBorder(0, (y), self:getWidth(), itemHeight - 1, a, self.borderColor.r, self.borderColor.g, self.borderColor.b)
    if self.selected == item.index then
        self:drawRect(0, (y), self:getWidth(), itemHeight - 1, 1, 0.17647, 0.26667, 0.26667)
    end
    
    --======================TEXT ON UI======================--
    
    --NPC Health Status --Title
    local title
    if iData.name then
        title = iData.name .. getText("IGUI_VLQ_TakeCare_Info_Title")
    else
        title = getText("IGUI_VLQ_TakeCare_Info_Title")
    end
    self:drawTextCentre(title, self.width / 2, yOffset, 1, 1, 1, 1, UIFont.NewLarge)
    
    --Kẻ đường viền
    yOffset = yOffset + fontHeightLarge
    local textWdt = getTextManager():MeasureStringX(UIFont.NewLarge, title)
    self:drawRect((self.width / 2) - (textWdt / 2), yOffset, textWdt, 1, a, r, g, b)
    
    local xDiv = self.width / 4
    local space = 15
    
    --Hiện trạng cơ thể
    yOffset = yOffset + fontHeightMedium
    self:drawTextRight(getText("IGUI_VLQ_TakeCare_Info_Health"), xDiv, yOffset, r, g, b, a, UIFont.Small)

    for i, line in ipairs(_lines) do
        if i >= 2 then
            yOffset = yOffset + fontHeightSmall
        end
        self:drawText(line, xDiv + space, yOffset, 0.98, 0.98, 0.98, 0.5, UIFont.Small)
    end   

    --Mức độ đói
    yOffset = yOffset + fontHeightSmall
    self:drawTextRight(getText("IGUI_VLQ_TakeCare_Info_Hunger"), xDiv, yOffset, r, g, b, a, UIFont.Small)
    
    local hungerLvl
    if hunger then
        if hunger < 20 then hungerLvl = getText("IGUI_VLQ_TakeCare_Info_Hunger_lvl0")
        elseif hunger >= 20 and hunger < 40 then hungerLvl = getText("Moodles_Hungry_lvl1")
        elseif hunger >= 40 and hunger < 60 then hungerLvl = getText("Moodles_Hungry_lvl2")
        elseif hunger >= 60 and hunger < 80 then hungerLvl = getText("Moodles_Hungry_lvl3")
        elseif hunger >= 80 then hungerLvl = getText("Moodles_Hungry_lvl4")
        end
    else 
        hungerLvl = "nil"
    end    
    self:drawText(hungerLvl, xDiv + space, yOffset, 0.98, 0.98, 0.98, 0.5, UIFont.Small)
    
    --Mức độ khát
    yOffset = yOffset + fontHeightSmall
    self:drawTextRight(getText("IGUI_VLQ_TakeCare_Info_Thirst"), xDiv, yOffset, r, g, b, a, UIFont.Small)
    
    local thirstLvl
    if thirst then
        if thirst < 20 then thirstLvl = getText("IGUI_VLQ_TakeCare_Info_Thirstr_lvl0")
        elseif thirst >= 20 and thirst < 40 then thirstLvl = getText("Moodles_Thirst_lvl1")
        elseif thirst >= 40 and thirst < 60 then thirstLvl = getText("Moodles_Thirst_lvl2")
        elseif thirst >= 60 and thirst < 80 then thirstLvl = getText("Moodles_Thirst_lvl3")
        elseif thirst >= 80 then thirstLvl = getText("Moodles_Thirst_lvl4")
        end
    else 
        thirstLvl = "nil"
    end   
    self:drawText(thirstLvl, xDiv + space, yOffset, 0.98, 0.98, 0.98, 0.5, UIFont.Small)
    
    --[[
    --Mức độ sạch sẽ cơ thể
    yOffset = yOffset + fontHeightSmall
    self:drawTextRight(getText("IGUI_VLQ_TakeCare_Info_Dirty"), xDiv, yOffset, r, g, b, a, UIFont.Small)
    
    local dirtyLvl
    if dirty then
        if dirty < 20 then dirtyLvl = getText("IGUI_VLQ_TakeCare_Info_Dirty_lvl0")
        elseif dirty >= 20 and dirty < 40 then dirtyLvl = getText("IGUI_VLQ_TakeCare_Info_Dirty_lvl1")
        elseif dirty >= 40 and dirty < 60 then dirtyLvl = getText("IGUI_VLQ_TakeCare_Info_Dirty_lvl2")
        elseif dirty >= 60 and dirty < 80 then dirtyLvl = getText("IGUI_VLQ_TakeCare_Info_Dirty_lvl3")
        elseif dirty >= 80 then dirtyLvl = getText("IGUI_VLQ_TakeCare_Info_Dirty_lvl4")
        end
    else
        dirtyLvl = "nil"
    end 
    self:drawText(dirtyLvl, xDiv + space, yOffset, 0.98, 0.98, 0.98, 0.5, UIFont.Small)
    ]]
    
    --Mức độ nhiễm độc
    yOffset = yOffset + fontHeightSmall
    self:drawTextRight(getText("IGUI_VLQ_TakeCare_Info_Poisoned"), xDiv, yOffset, r, g, b, a, UIFont.Small)
    
    local poisonedLvl
    if poisoned then
        if poisoned < 20 then poisonedLvl = getText("IGUI_VLQ_TakeCare_Info_Poisoned_lvl0")
        elseif poisoned >= 20 and poisoned < 40 then poisonedLvl = getText("IGUI_VLQ_TakeCare_Info_Poisoned_lvl1")
        elseif poisoned >= 40 and poisoned < 60 then poisonedLvl = getText("IGUI_VLQ_TakeCare_Info_Poisoned_lvl2")
        elseif poisoned >= 60 and poisoned < 80 then poisonedLvl = getText("IGUI_VLQ_TakeCare_Info_Poisoned_lvl3")
        elseif poisoned >= 80 then poisonedLvl = getText("IGUI_VLQ_TakeCare_Info_Poisoned_lvl4")
        end
    else 
        poisonedLvl = "nil"
    end   
    self:drawText(poisonedLvl, xDiv + space, yOffset, 0.98, 0.98, 0.98, 0.5, UIFont.Small)
    
    return y + itemHeight
end

--CALL
function VLF.QuestUI.NpcHealthWindow(player, qStep, zNPC)
    if not VLF.var.showNpcHealthWindow then
        VLF.var.showNpcHealthWindow = VL_Quest_NPC_Health_Info:new(player, 500, 350, qStep, zNPC)
        VLF.var.showNpcHealthWindow:initialise()
        VLF.var.showNpcHealthWindow:addToUIManager()
    else
        VLF.var.showNpcHealthWindow:close()
    end
end






