"""Hidden open-world canisters: no more raw credit junk"""
from sc_lua import csv, strip_loot_entries

MOD = {
    'id': 'canister_loot',
    'name': 'Hidden canisters: no raw credit junk',
    'description': 'Removes the raw credits trash from the hidden loot canisters making it far more common for them to drop something good. Sometimes a credit farm item will still drop, but its still much better than before.',
    'default_enabled': True,
    'author': 'Furebel',
    'order': 10,
    'files': {'loot': 'data/gamedata/def/objects/ow_loot_tables.lua'},
    'tweakables': [
        {'key': 'remove_rare', 'type': 'bool', 'default': False, 'advanced': True,
         'label': 'Also remove the ow_Rare_* items',
         'help': 'Use this if an item that only gives credits (for example Insignia-like tokens) still shows up.'},
        {'key': 'equalize', 'type': 'bool', 'default': False, 'advanced': True,
         'label': 'Give every remaining item the same chance',
         'help': 'Flattens the table. Ammo then drops less often, rare items more often.'},
        {'key': 'tables', 'type': 'text', 'default': ','.join('OW2_GoodJunk%d' % i for i in range(1, 7)),
         'advanced': True, 'label': 'Loot tables to clean (comma separated)',
         'help': 'Names from ow_loot_tables.lua. Wrong names are reported in the log and skipped.'},
    ],
}


def apply(files, v, log):
    def is_junk(line, item):
        if item.startswith('ow_Rare_'):
            return v['remove_rare']
        return item.startswith('ow_')

    files['loot'], report = strip_loot_entries(files['loot'], csv(v['tables']), is_junk, v['equalize'])
    for name, r in report.items():
        log('%s: not found' % name if r is None else '%s: removed %d, %d left' % (name, r[0], r[1]))
