import DBShip from "../../types/db/DBShip";
import SystemManager from "./managers/SystemManager";
import ShipManager from "./managers/abstract/ShipManager";

export default class Ship {
    manager: ShipManager;
    data: DBShip;
    amount: number;

    constructor(manager: ShipManager, data: DBShip, amount: number) {
        this.manager = manager;
        this.data = data;
        this.amount = amount;
    }

    async checkRequiredResources(): Promise<boolean> {
        const resources = await this.manager.manager.resources.calculateCurrentAvailableResources();

        const requirements = this.data.requirements.resources;

        let canBuild = true;

        for(let res of requirements) {
            const resource = resources.find(r => r.id === res.id);
            if(!resource) return false;

            if(resource.amount < (res.amount * this.amount)) {
                canBuild = false;
                break;
            }
        }

        return canBuild;
    }

    async checkRequirements(): Promise<{ canBuild: boolean, error: string }> {
        if(!(this.manager.manager instanceof SystemManager)) {
            const playerBuildings = this.manager.manager.buildings.buildings;
            let playerBuildingsCanBuild = { canBuild: true, missing: "" };
            this.data.requirements.buildings.forEach((buildingReq: any) => {
                if((playerBuildings.filter((building) => building.data.id === buildingReq.id)[0]?.level ?? 0) < buildingReq.level) {
                    playerBuildingsCanBuild = { canBuild: false, missing: `${buildingReq.id} level ${buildingReq.level} required, found ${playerBuildings.filter((building) => building.data.id === buildingReq.id)[0]?.level ?? 0}` };
                    return;
                }
            });
            if(!playerBuildingsCanBuild.canBuild) return {
                canBuild: false,
                error: playerBuildingsCanBuild.missing
            }
        } /*else { //TODO: check for structures requirements
            const structures = this.manager.manager.structures.structures;
            let playerStructuresCanBuild = { canBuild: true, missing: "" };
            this.data.requirements.structures.forEach((buildingReq: any) => {
                if((structures.filter((building) => building.data.id === buildingReq.id)[0]?.level ?? 0) < buildingReq.level) {
                    playerStructuresCanBuild = { canBuild: false, missing: `${buildingReq.id} level ${buildingReq.level} required, found ${structures.filter((structure) => structure.data.id === buildingReq.id)[0]?.level ?? 0}` };
                    return;
                }
            });
            if(!playerStructuresCanBuild.canBuild) return {
                canBuild: false,
                error: playerStructuresCanBuild.missing
            }
        }*/

        // research
        const playerResearch = this.manager.manager instanceof SystemManager ? this.manager.manager.data.ownedBy.research : this.manager.manager.system.data.ownedBy.research;
        let playerResearchCanBuild = { canBuild: true, missing: "" };
        for(const researchReq of this.data.requirements.research) {
            if(playerResearch.research.find((research) => research.id === researchReq.id)?.level ?? 0 < researchReq.level) {
                playerResearchCanBuild = { canBuild: false, missing: `${researchReq.id} level ${researchReq.level} required, found ${playerResearch.research.find((research) => research.id === researchReq.id)?.level ?? 0}` };
            }
        };
        
        if(!playerResearchCanBuild.canBuild) return {
            canBuild: false,
            error: playerResearchCanBuild.missing
        }

        return {
            canBuild: true,
            error: ""
        }
    }
}