import Resources from '../config/resources'
import { makeResourceItem } from './helpers'

export class Unit {
    init() {
        const defaults = {
            'WOOD': 10,
            'STONE': 10,
            'PEOPLE': 3,
        }
        this.resources = Object.keys(Resources).map(item => makeResourceItem(item, defaults[item] || 0))
    }

    getResources() {
        return this.resources
    }

    getResource(key) {
        return this.resources.find(item => item.type === key)
    }

    getResourceIndex(key) {
        return this.resources.findIndex(item => item.type === key)
    }

    setResourceAmount(type, val) {
        this.resources.forEach((item, i) => {
            if (item.type === type) {
                this.resources[i].value = val
            }
        })
    }

    increaseResources(requirements, k = 1) {
        requirements.forEach(req => {
            const resId = this.getResourceIndex(req.type)
            if (resId < 0) return

            this.resources[resId].value += req.amount * k
        })
    }
}

export const Player = new Unit
export const Bot = new Unit

export default {
    Player,
    Bot
}
