"use strict";

let mydb;

class Mod {
    async postDBLoadAsync(container) {
        const db = container.resolve("DatabaseServer").getTables();
        const modLoader = container.resolve("PreAkiModLoader");
        const importerUtil = container.resolve("ImporterUtil");
        const locales = db.locales.global;
        const items = db.templates.items;
        const handbook = db.templates.handbook.Items;

        mydb = await importerUtil.loadRecursiveAsync(`${modLoader.getModPath("SamSWAT-L85A2")}database/`);

        for (const item in mydb.templates.items) {
            items[item] = mydb.templates.items[item];
        }

        for (const item of mydb.templates.handbook.Items) {
            handbook.push(item);
        }

        for (const preset in mydb.globals.ItemPresets) {
            db.globals.ItemPresets[preset] = mydb.globals.ItemPresets[preset];
        }

        for (const item of mydb.traders.assort.assorts.items) {
            db.traders[mydb.traders.assort.traderId].assort.items.push(item);
        }

        for (const bc in mydb.traders.assort.assorts.barter_scheme) {
            db.traders[mydb.traders.assort.traderId].assort.barter_scheme[bc] = mydb.traders.assort.assorts.barter_scheme[bc];
        }

        for (const level in mydb.traders.assort.assorts.loyal_level_items) {
            db.traders[mydb.traders.assort.traderId].assort.loyal_level_items[level] = mydb.traders.assort.assorts.loyal_level_items[level];
        }

        for (const locale of Object.values(locales)) {
            for (const [itemId, template] of Object.entries(mydb.locales.en.templates)) {
                for (const [key, value] of Object.entries(template)) {
                    locale[`${itemId} ${key}`] = value;
                }
            }
            for (const [itemId, template] of Object.entries(mydb.locales.en.preset)) {
                for (const value of Object.values(template)) {
                    locale[`${itemId}`] = value;
                }
            }
        }

        db.globals.config.Mastering.push(
            {
                "Name": "L85A2",
                "Templates": [
                    "weapon_l85a2_556x45"
                ],
                "Level2": 1000,
                "Level3": 3000
            }
        );
    }
}

module.exports = { mod: new Mod() }