-- handle docs cost and other lootbox specific items
local talking_npc_faction
function on_get_item_cost(kind, obj, profile, calculated_cost, ret)
	local discount = profile.discount
    if profile.mode == 2 then
        return
    end
    local sec = obj:section()
    local factions = SYS_GetParam(0, sec, "factions")
    if not factions or factions == "" then return end

    local tbl = str_explode(factions, ",")
    t2k_table(tbl)
    
    cost = SYS_GetParam(2, sec, "cost")
    if tbl[talking_npc_faction] then
        cost = cost * 1.5
    end
    ret.new_cost = cost
end



local trader_npc_id = 0

-- purge all docs in npc inv, on sale they should vanish
function ActorMenu_on_trade_started()
    create_tools()
	local trader_npc = mob_trade.GetTalkingNpc()
    trader_npc_id = trader_npc:id()
    talking_npc_faction = get_real_community(trader_npc)
end


function ActorMenu_on_trade_closed()
    if trader_npc_id == 0 then 
        return 
    end
	local trader_npc = level.object_by_id(trader_npc_id)
    if not trader_npc then 
        return 
    end
    trader_npc:iterate_inventory(function(temp, item)
        if SYS_GetParam(0, item:section(), "factions") then
            alife_release(item)
        end
    end)
    talking_npc_faction = nil    
end

local furniture = {
    ["esc_m_trader"] = true,
    ["red_m_lesnik"] = true
}

local blacklisted_comms = {
    ["trader"] = true,
    ["monster"] = true
}

function get_real_community(npc)

	if furniture[npc:name()] then
		return "stalker"
	end
	local community = character_community(npc)
	if not blacklisted_comms[community] then
		return community
	end
	local squad_community = get_object_squad(npc):get_squad_community()
	if not blacklisted_comms[squad_community] then
		return squad_community
	else
		return "stalker"
	end
end

-- spawn kit and picks in technician inv

function IsMechanic(npc)
    return string.find(npc:section(), "mechanic") or string.find(npc:section(), "tech")
end

function create_tools()

    local npc = mob_trade.GetTalkingNpc()
    if IsMechanic(npc) then
        local num_picks = 0
        -- local num_set = 0
        -- local num_bundle = 0

        local function search(temp, item)
            local item_section = item:section()
            if item_section == "lockpick" then
                num_picks = num_picks + 1
            -- elseif item_section == "bundle_lockpick" then
            --     num_bundle = num_bundle + 1
            -- elseif item_section == "lockpick_set" then
            --     num_set = num_set + 1
            end
        end
        npc:iterate_inventory(search, nil)
        num_picks = 5 - num_picks
        if num_picks > 0 then
            for i=1, num_picks do
                alife_create_item("lockpick", npc)
            end 
        end
        -- num_bundle = 5 - num_bundle
        -- if num_bundle > 0 then
        --     for i=1, num_bundle do
        --         alife_create_item("bundle_lockpick", npc)
        --     end 
        -- end
        -- if num_set == 0 then
        --     alife_create_item("lockpick_set", npc)
        -- end
    end
end

CanRepair = inventory_upgrades.can_repair_item
function inventory_upgrades.can_repair_item(sec)
    if arti_lootboxes.is_box(nil, sec) then return true
    else return CanRepair(sec) end
end

QuestionRepair = inventory_upgrades.question_repair_item
function inventory_upgrades.question_repair_item( item_name, item_condition, can, mechanic ) --( string, float, bool, string )
    if arti_lootboxes.is_box(nil, item_name) then
        if not arti_lootboxes.check_open_compatibility(item_name, "lockpick") then 
            return game.translate_string("st_tech_open_fail")
        end
        local price = inventory_upgrades_mp.how_much_repair( item_name, item_condition )
        if db.actor:money() < price then
            return game.translate_string("st_tech_open")..": "..price.." RU\\n"..game.translate_string("ui_inv_not_enought_money")..": "..price-db.actor:money().." RU"
        else
            return game.translate_string("st_tech_open").." "..price.." RU. "..game.translate_string("st_tech_open_q")
        end
    else
        return QuestionRepair(item_name, item_condition, can, mechanic)
    end
end

HowMuch = inventory_upgrades_mp.how_much_repair 
function inventory_upgrades_mp.how_much_repair( item_name, item_condition )
    if arti_lootboxes.is_box(nil, item_name) then
        if not arti_lootboxes.check_open_compatibility(item_name, "lockpick") then return 999999
        else
            local cof = game_difficulties.get_eco_factor("repair") or 1.67
            return ini_sys:r_u32(item_name, "cost") * cof * 0.6
        end
    else return HowMuch(item_name, item_condition)
    end
end

-- RModeRepair = ui_inventory.UIInventory.RMode_OnRepair

-- function ui_inventory.UIInventory.RMode_OnRepair(self)

-- end

RModeRepairYes = ui_inventory.UIInventory.RMode_RepairYes
function ui_inventory.UIInventory:RMode_RepairYes()
	self:Print(nil, "RMode_RepairYes")
	if (not self.upgr.id) then
		return
	end
	
	local obj = level.object_by_id(self.upgr.id)
	if (not obj) then
		return
	end
    RModeRepairYes(self)
    if arti_lootboxes.is_box(obj) then
        arti_lootboxes.open_lootbox_timer(self.upgr.id, true) 
    end
end

function on_game_start()

    RegisterScriptCallback("ActorMenu_on_trade_started", ActorMenu_on_trade_started)
    
    RegisterScriptCallback("ActorMenu_on_trade_closed", ActorMenu_on_trade_closed)

    if utils_item.on_get_item_cost then
        RegisterScriptCallback("on_get_item_cost", on_get_item_cost)
    end
end