--[[
 -- @Description:
 -- Hello , DiaoSi ~ ( or not ? ) ,glad you saw my comment
 -- If you think my mod is okay , you can sponsor my breakfast by a tiny amount donation  ($_$)
 -- paypal:td1madao@163.com     alipay:td1madao@163.com
 -- If any question ,Please email me
 -- What's more ,don't modify and republish my mods . 3Q for cooperation
 -- What's more ,don't modify and republish my mods . 3Q for cooperation
 -- What's more ,don't modify and republish my mods . 3Q for cooperation
 -- @version 1.0.0
 -- @author td1madao
 -- @email td1madao@163.com
 -- @qq 360810498
 -- @date 16/8/6
 ]]
local TD = TD.td1madao.rivendst
local skillbar = require("widgets/rivendstskillbar")
local hud = "images/td_riven_dst_hud.xml"
local Text = require "widgets/text"
local Rivendst_player_skill_register = Class(function(self, inst)
    TD.see()
    self.inst = inst
    self.handler = TheInput:AddKeyHandler(function(key, down) self:player_keyHandler(key, down) end)
end)
function Rivendst_player_skill_register:player_keyHandler(key, down)
    TD.see()
    local player = self.inst
    if player ~= nil then
        if key and not down then
            player:PushEvent("keyup", { inst = self.inst, player = player, key = key })
        elseif key and down then
            player:PushEvent("keydown", { inst = self.inst, player = player, key = key })
        end
    end
end

function Rivendst_player_skill_register:player_register(tag, x, y)
    TD.see()
    local function sendKillCastRequest()
        TD.see()
        TD.getC(self.inst, "rivendst_common_skill_manager"):player_cast(tag)
    end

    local key = TUNING[string.format("TD1MADAO_RIVENDST_%s", string.upper(tag))]
    self.inst:ListenForEvent("keyup", function(inst, data)
        if data.inst == self.inst and data.key == key then
            sendKillCastRequest()
        end
    end)
    if self.inst and self.inst.td1madaoRivendstSkillControls then
        local td1madaoRivendstSkillControls = self.inst.td1madaoRivendstSkillControls
        td1madaoRivendstSkillControls[tag] = td1madaoRivendstSkillControls:AddChild(skillbar(hud, tag .. ".tex", x, y, true, tag, self.inst, sendKillCastRequest))
    end
end


function Rivendst_player_skill_register:player_regist_lv_and_exp()
    TD.see()
    local player = self.inst
    local td1madaoRivendstSkillControls = player.td1madaoRivendstSkillControls
    if player ~= nil and td1madaoRivendstSkillControls then
        td1madaoRivendstSkillControls.level = td1madaoRivendstSkillControls:AddChild(Text(BODYTEXTFONT, 40))
        td1madaoRivendstSkillControls.level:SetHAlign(ANCHOR_MIDDLE)
        td1madaoRivendstSkillControls.level:SetHAnchor(ANCHOR_LEFT)
        td1madaoRivendstSkillControls.level:SetColour(0, 0.5, 0, 1)
        td1madaoRivendstSkillControls.exp = td1madaoRivendstSkillControls:AddChild(Text(BODYTEXTFONT, 25))
        td1madaoRivendstSkillControls.exp:SetHAlign(ANCHOR_MIDDLE)
        td1madaoRivendstSkillControls.exp:SetHAnchor(ANCHOR_LEFT)
        td1madaoRivendstSkillControls.exp:SetColour(0.5, 0.5, 0, 1)
        local syn = TD.getC(player, "rivendst_common_synchronizer")
        syn:player_fresh_att()
        local perioder = TD.getC(player, "rivendst_common_perioder")
        local function refresh()
            local buffer = perioder.buffer
            if not buffer.scale and not buffer.scale2 then
                local scr_w, scr_h = TheSim:GetScreenSize()
                local scale = (scr_w / 1366)
                buffer.scale = scale
                buffer.scale2 = scr_h / 706 / scale
            end
            local scale = buffer.scale
            local scale2 = buffer.scale2
            if perioder.bak.scale == scale and perioder.bak.scale2 == scale2 then
                return
            end
            td1madaoRivendstSkillControls.level:SetScaleMode(SCALEMODE_PROPORTIONAL)
            td1madaoRivendstSkillControls.level:SetPosition(MADAO.RIVENDST_UI_LEVEL_X * scale, MADAO.RIVENDST_UI_LEVEL_Y * scale, 0)
            td1madaoRivendstSkillControls.exp:SetScaleMode(SCALEMODE_PROPORTIONAL)
            td1madaoRivendstSkillControls.exp:SetPosition(MADAO.RIVENDST_UI_EXP_X * scale, MADAO.RIVENDST_UI_EXP_Y * scale, 0)
        end

        perioder:common_addTask("td1madao_rivendst_widget_level_and_exp_refresh", refresh, 1)
        refresh()
    end
end

return Rivendst_player_skill_register