local re = re
local sdk = sdk
local d2d = d2d
local imgui = imgui
local log = log
local json = json
local draw = draw
local thread = thread
local require = require
local tostring = tostring
local pairs = pairs
local ipairs = ipairs
local math = math
local string = string
local table = table
local type = type

local Core = require("_CatLib")
local CONST = require("_CatLib.const")
local Utils = require("_CatLib.utils")
local Imgui = require("_CatLib.imgui")

local mod = Core.NewMod("Item Buff Extender")
mod.EnableCJKFont(18)

if mod.Config.ItemConfig == nil then
    mod.Config.ItemConfig = {}
end

local _M = {}

local ItemParam

local SortItemIDs = {
    "Kairiki",
    "Kairiki_G",
    "Nintai",
    "Nintai_G",
    "Jerky",
    "DashJuice",
    "Immunizer",
    "HotDrink",
    "CoolerDrink",
    "KijinDrink",
    "KijinDrink_G",
    "KoukaDrink",
    "KoukaDrink_G",
    "KijinPowder",
    "KoukaPowder",
    "KijinAmmo",
    "KoukaAmmo",
}

local ItemIDs = {
    Kairiki = 125, -- 怪力种子
    Kairiki_G = 168, -- 怪力药丸
    Nintai = 126, -- 忍耐种子
    Nintai_G = 171, -- 忍耐药丸
    
    -- Jerky = -1,

    DashJuice = 163,
    Immunizer = 164,

    HotDrink = 166,
    CoolerDrink = 165,

    KijinDrink = 167, -- 鬼人药
    KijinDrink_G = 169,
    KoukaDrink = 170, -- 硬化药
    KoukaDrink_G = 172,

    KijinPowder = 175, -- 鬼人粉尘
    KoukaPowder = 176, -- 硬化粉尘
    KijinAmmo = 412, -- 鬼人弹
    KoukaAmmo = 413, -- 硬化弹
}

function _M.InitItemConfig()
    if ItemParam == nil then
        return
    end

    for key, id in pairs(ItemIDs) do
        if mod.Config.ItemConfig[key] == nil then
            local timer = ItemParam:get_field(string.format("_%s_Time", key))
            if timer then
                mod.Config.ItemConfig[key] = {
                    Enable = true,
                    ID = id,
                    OriginalTimer = timer,
                    Timer = timer,
                }
            end
        end
    end
    mod.SaveConfig()
end

function _M.ApplyItemConfig()
    if ItemParam == nil then
        return
    end

    for key, id in pairs(ItemIDs) do
        local data = mod.Config.ItemConfig[key]
        if data ~= nil and data.Enable and data.Timer ~= nil and data.Timer > 0 then
            ItemParam:set_field(string.format("_%s_Time", key), data.Timer)
        end
    end
end

function _M.Init()
    if ItemParam then
        return
    end
    ItemParam = Core.GetPlayerManager()._Catalog._PlayerItemParam

    _M.InitItemConfig()
    _M.ApplyItemConfig()
end

_M.Init()
Core.OnLoading(function ()
    ItemParam = nil
    _M.Init()
end)

mod.Menu(function ()
    local changed, configChanged = false, false

    imgui.text("Edit All Listed Items Max Time:")
    
    local times = 1
    Imgui.Button("2x", function ()
        times = 2
    end)
    imgui.same_line()
    Imgui.Button("3x", function ()
        times = 3
    end)
    imgui.same_line()
    Imgui.Button("5x", function ()
        times = 5
    end)
    imgui.same_line()
    Imgui.Button("10x", function ()
        times = 10
    end)
    imgui.same_line()
    Imgui.Button("0.5x", function ()
        times = 0.5
    end)
    imgui.same_line()
    Imgui.Button("0.1x", function ()
        times = 0.1
    end)

    local restore = Imgui.Button("Restore")

    imgui.same_line()
    Imgui.Button("Apply", function ()
        _M.ApplyItemConfig()
    end)

    for _, key in pairs(SortItemIDs) do
        local data = mod.Config.ItemConfig[key]
        if data and data.ID > 0 then
            if times ~= 1 then
                data.Timer = math.min(math.ceil(data.Timer * times), 6000)
                configChanged = true
            end
            if restore then
                data.Timer = data.OriginalTimer
                configChanged = true
            end

            changed, data.Enable = imgui.checkbox(string.format("##Enable%d", data.ID), data.Enable)
            configChanged = configChanged or changed
            imgui.same_line()

            imgui.text(Core.GetItemName(data.ID))
            imgui.same_line()

            changed, data.Timer = imgui.slider_float(string.format("Max Timer##Timer%d", data.ID), data.Timer, 1, 6000)
            configChanged = configChanged or changed
        end
    end

    if configChanged then
        _M.ApplyItemConfig()
    end

    return configChanged
end)
