
declare const alertEx;

import {questData} from './questData';
import { QuestItem } from './questItem';

interface Inventory {
    code:string,
    desc: string
}

export enum Inventories {
    DISKET = 'Дискета 5 дюймов',
    PASS = 'Пропуск сотрудника НИИ',
    CODE_WORD = 'Код (0042)',
    COIN = 'Монета'
}

export class QuestEngine {
    
    questItem:QuestItem;
    inventory:Inventory[] = [];

    constructor(){
        this.goto(10);
        // this.takeInventory('CODE_WORD');
        // this.takeInventory('PASS');
        // this.takeInventory('DISKET');
        // this.takeInventory('COIN');
    }

    getNextQuest(index:number){
        let item:QuestItem = new QuestItem();
        if (!questData[index]) {
            console.error(questData,index);
            throw `can not get quest data with index ${index}`;
        }    
        item.fromJSON(questData[index]);
        item.engine = this;
        if (item.onShow) item.onShow(this);
        this.questItem = item;
    }

    goto(id:number) {
        let index = questData.findIndex(it=>it.id===id);
        if (index==-1) throw `can not goto ${id}`;
        this.getNextQuest(index);
    }

    takeInventory(code:string) {
        let desc = Inventories[code];
        if (!desc) throw `no such inventory with code ${code}`;
        this.inventory.push({code,desc})
    }

    hasInventory(code:string) {
        let desc = Inventories[code];
        if (!desc) throw `no such inventory with code ${code}`;
        return this.inventory.findIndex(it=>it.code==code)>-1;
    }

    applyInventory(inv:Inventory){
         let result = false;
         if (this.questItem.onInventory) result = this.questItem.onInventory(this,inv.code);
         if (!result) alertEx(`Вы попытались применить предмет "${inv.desc}", но ничего не произошло`);   
    }

    resolveAction(q){
        this.questItem.resolveAction(q);
    }


}