import { updatePlanetResources } from "../../db/planets";
import { getAllResources } from "../../db/resources";
import { Planet } from "./PlanetManager";
import ResourceManager from "./abstract/ResourceManager";

export default class PlanetResourceManager extends ResourceManager {
    planet: Planet;

    constructor(planet: Planet) {
        super();
        this.planet = planet;
    }

    async init(resourceData: { id: string, amount: number, lastUpdated: Date, perHourMiningRate: number }[]) {
        const resources = await getAllResources();

        this.resourcesDB = resources;

        if(resourceData.length === 0) {
            resourceData = [
                {
                    id: "coal",
                    amount: 11,
                    lastUpdated: new Date(),
                    perHourMiningRate: 11
                },
                {
                    id: "iron",
                    amount: 22,
                    lastUpdated: new Date(),
                    perHourMiningRate: 22
                },
                {
                    id: "gold",
                    amount: 33,
                    lastUpdated: new Date(),
                    perHourMiningRate: 33
                },
                {
                    id: "water",
                    amount: 44,
                    lastUpdated: new Date(),
                    perHourMiningRate: 44
                },
                {
                    id: "sulfuric-acid",
                    amount: 55,
                    lastUpdated: new Date(),
                    perHourMiningRate: 55
                },
                {
                    id: "liquid-nitrogen",
                    amount: 66,
                    lastUpdated: new Date(),
                    perHourMiningRate: 66
                },
                {
                    id: "hydrogen",
                    amount: 77,
                    lastUpdated: new Date(),
                    perHourMiningRate: 77
                },
                {
                    id: "oxygen",
                    amount: 88,
                    lastUpdated: new Date(),
                    perHourMiningRate: 88
                },
                {
                    id: "helium-3",
                    amount: 99,
                    lastUpdated: new Date(),
                    perHourMiningRate: 99
                }
            ];

            await updatePlanetResources(this.planet._id, this.resources);
        }
    
        resourceData.forEach(resource => {
            const resFromDB = resources.find(res => res.id === resource.id);
            if(resFromDB) this.resources.push({
                id: resource.id,
                amount: resource.amount,
                lastUpdated: resource.lastUpdated,
                perHourMiningRate: resource.perHourMiningRate,
                data: resFromDB
            })
        });

        return this;
    }

    getStorageCapacities() {
        const mapStorageToResource: { [key: string]: string } = {
            "coal-storage": "coal",
            "iron-storage": "iron",
            "gold-storage": "gold",
            "water-tank": "water",
            "acid-tank": "sulfuric-acid",
            "nitrogen-tank": "liquid-nitrogen",
            "hydrogen-tank": "hydrogen",
            "oxygen-tank": "oxygen",
            "helium3-tank": "helium-3",
        }

        const output: { id: string, capacity: number }[] = [];
    
        for(const building of this.planet.buildings.buildings) {
            if(building.data.category === 'storage') {
                const resource = mapStorageToResource[building.data.id];
                if(resource) {
                    output.push({ id: resource, capacity: building.level * 10_000 });
                }
            }
        }

        return output;
    }

    async sync() {
        await updatePlanetResources(this.planet._id, this.resources.map(res => {
            return {
                id: res.id,
                amount: res.amount,
                lastUpdated: res.lastUpdated,
                perHourMiningRate: res.perHourMiningRate
            }
        }));
    }
}