-- @Author: 我是派蒙啊
-- @Date:   2024-06-15 12:43:08
-- @Last Modified by:   我是派蒙啊
-- @Last Modified time: 2024-10-22 19:07:21
local mod = get_mod("OverheatWarning")
mod.settings = mod:persistent_table("settings")
local current_slot = "slot_primary"
local my_favorite_part = false
local show_time = 0
local heat = 0

mod.on_setting_changed = function(setting_id)
    mod.settings[setting_id] = mod:get(setting_id)
end

local function initialize_settings_cache()
    mod.settings["overheat_limit"] = mod:get("overheat_limit")
end
initialize_settings_cache()

local get_local_player = function()
    return Managers.player:local_player(1)
end

local is_ability_available = function()
    local unit = get_local_player().player_unit
    if not unit then return false end
    return ScriptUnit.extension(unit, "ability_system"):remaining_ability_charges("combat_ability") > 0
end

local get_infinite_talent = function()
    local player = get_local_player()
    local profile = player._profile
    local talents = profile.talents
    return talents.psyker_overcharge_stance_infinite_casting
end

local get_shout_talent = function()
    local player = get_local_player()
    local profile = player._profile
    local talents = profile.talents
    return talents.psyker_combat_ability_shout or talents.psyker_shout_vent_warp_charge
end

local git = function()
    local player = get_local_player()
    local profile = player._profile
    local talents = profile.talents
    for k, v in pairs(talents) do
        print(k)
    end
end

mod:command("git", "git", git)

mod:hook_safe("PlayerUnitWeaponExtension", "on_slot_wielded",
    function(self, slot_name, t, skip_wield_action)
        if self._player.viewport_name == "player1" then
            current_slot = slot_name
        end
    end)

local color = { 255, 255, 0, 0 }
mod:hook(CLASS.HudElementOvercharge, "update", function(func, self, ...)
    func(self, ...)
    local player_extensions = self._parent:player_extensions()
    local player_unit_data = player_extensions and player_extensions.unit_data

    -- Get heat
    local warp_charge_level = player_unit_data and player_unit_data:read_component("warp_charge").current_percentage or 0
    heat = warp_charge_level

    -- Change to warning
    local widget = self._widgets_by_name.overcharge or self._widgets_by_name.overheat

    if heat >= mod.settings["overheat_limit"] and show_time <= 0 then
        widget.visible = true
        widget.alpha_multiplier = 1.0
        widget.content.warning_text = mod:localize("will_die")
        widget.style.warning_text.text_color = table.clone(color)
    end

    -- May be we could have a buff...
    if heat >= 1 then
        my_favorite_part = true
    end

    -- We dont have the infinite talent...
    if not get_infinite_talent() or (heat < 1 and not my_favorite_part) then return end

    -- Get buff system
    local buff_extensions = ScriptUnit.extension(get_local_player().player_unit, "buff_system")
    if not buff_extensions then return end

    -- Find infinite buff
    for _, buff in pairs(buff_extensions._buffs_by_index) do
        local template = buff:template()
        if template.name == "psyker_overcharge_stance_infinite_casting" then
            local remaining = buff:duration_progress() or 1
            show_time = buff:duration() * remaining - 0.7
            if show_time <= 0 then my_favorite_part = false end
            break
        end
    end
end)

local _input_action_hook = function(func, self, action_name)
    -- If we could shout...
    if action_name == "combat_ability_pressed" and get_shout_talent()
        and heat >= 1 and is_ability_available() then
        return true
    end
    return func(self, action_name)
end

mod:hook(CLASS.InputService, "_get", _input_action_hook)
mod:hook(CLASS.InputService, "_get_simulate", _input_action_hook)
