/// <reference path="../../_system/_shared/Initializing.ts"/>

class Consumable extends Initializing{
	type='meal';
    
    alcohol=0;
    
    /**
     * Energy 1 means a typical amount of calories. This number is multiplicated with hunger for calculations.
     * @type {number}
     */
    energy: number=0;
    energyFlat=0;
    hunger=0;
    thirst;

    moodletIds:string[]=[];

    static get(modificatorId:string):Consumable{
		let staticData = setup.consumables[modificatorId];
        if(!staticData)
            return this.null;
		return new Consumable().init(staticData);
	}

	static get null(){return NullConsumable.INSTANCE;}

    get energyBalance(){
        return this.hunger * this.energy + this.energyFlat;
    }

	consume(playerCharacter:PlayerCharacter,percentage:number|'auto'|undefined=undefined){
        //let consumePercentage = percentage;
        let consumePercentage:number;

        if(typeof percentage == 'number')
            consumePercentage = percentage;

        let thirstReduction:number;

        switch(this.type){
            case 'candy':
                playerCharacter.cumCleanByActivity('eat');
                consumePercentage ??= 100;
                thirstReduction = this.thirst ?? 0;
                break;
            case 'drink':
                playerCharacter.cumCleanByActivity('drink');
                consumePercentage ??= 100; 
                thirstReduction = this.thirst ?? Infinity;
                break;
            case 'meal':
                playerCharacter.cumCleanByActivity('eat');
                if(!consumePercentage || percentage === 'auto')
				    consumePercentage = Math.min(100,(100 - playerCharacter.pcs_energy) / this.hunger * 100);
                thirstReduction = this.thirst ?? 0;
                break;			
        }
        playerCharacter.pcs_hydra += thirstReduction * consumePercentage / 100;
        playerCharacter.pcs_energy += this.hunger * consumePercentage / 100;
        playerCharacter.energy += this.energyBalance * consumePercentage / 100;

        if(this.alcohol)
		    playerCharacter.alko += this.alcohol;

        for(const moodletId of this.moodletIds)
            playerCharacter.moodletApplyById(moodletId);
    }
}

class NullConsumable extends Consumable{
	private static _instance = new NullConsumable();
	static get INSTANCE(){return this._instance;}

    consume(playerCharacter:PlayerCharacter){}
}



setup.Consumable = Consumable;