export class Skills {
    constructor(game) {
        this.game = game;

        this.skills = {
            scavenging: { name: 'Поиск', level: 1, xp: 0, xpNeeded: 10, desc: 'Шанс найти лучший лут' },
            begging:    { name: 'Убеждение', level: 1, xp: 0, xpNeeded: 8, desc: 'Шанс получить больше при попрошайничестве' },
            survival:   { name: 'Выживание', level: 1, xp: 0, xpNeeded: 15, desc: 'Медленнее теряете статы' },
            trading:    { name: 'Торговля', level: 1, xp: 0, xpNeeded: 12, desc: 'Лучшие цены в магазине' },
        };

        this.maxLevel = 10;
    }

    addXP(skillKey, amount) {
        const skill = this.skills[skillKey];
        if (!skill || skill.level >= this.maxLevel) return;

        skill.xp += amount;
        if (skill.xp >= skill.xpNeeded) {
            skill.xp -= skill.xpNeeded;
            skill.level++;
            skill.xpNeeded = Math.floor(skill.xpNeeded * 1.5);
            this.game.notify(`${skill.name} повышен до уровня ${skill.level}!`, 'good');
            this.game.sound.playQuestComplete();
        }
    }

    getLevel(skillKey) {
        return this.skills[skillKey]?.level || 1;
    }

    // Модификаторы
    getScavengeBonus() {
        return 1 + (this.getLevel('scavenging') - 1) * 0.1;
    }

    getBegBonus() {
        return 1 + (this.getLevel('begging') - 1) * 0.15;
    }

    getSurvivalModifier() {
        return 1 - (this.getLevel('survival') - 1) * 0.05;
    }

    getTradeDiscount() {
        return Math.max(0.6, 1 - (this.getLevel('trading') - 1) * 0.05);
    }

    getSaveData() {
        const data = {};
        for (const [key, skill] of Object.entries(this.skills)) {
            data[key] = { level: skill.level, xp: skill.xp, xpNeeded: skill.xpNeeded };
        }
        return data;
    }

    loadSaveData(data) {
        if (!data) return;
        for (const [key, saved] of Object.entries(data)) {
            if (this.skills[key]) {
                this.skills[key].level = saved.level;
                this.skills[key].xp = saved.xp;
                this.skills[key].xpNeeded = saved.xpNeeded;
            }
        }
    }

    reset() {
        for (const skill of Object.values(this.skills)) {
            skill.level = 1;
            skill.xp = 0;
            skill.xpNeeded = skill === this.skills.scavenging ? 10 :
                             skill === this.skills.begging ? 8 :
                             skill === this.skills.survival ? 15 : 12;
        }
    }
}
