{"size": 3887, "ext": "js", "max_stars_repo_path": "parser/state.js", "max_stars_repo_name": "scratch4d/text2scratch", "max_stars_repo_stars_event_min_datetime": "2018-02-08T15:38:41.000Z", "max_stars_repo_stars_event_max_datetime": "2020-10-22T01:40:40.000Z", "max_issues_repo_path": "parser/state.js", "max_issues_repo_name": "CodeLabClub/scratch-LN", "max_issues_repo_issues_event_min_datetime": "2018-02-18T16:28:44.000Z", "max_issues_repo_issues_event_max_datetime": "2018-11-04T20:49:10.000Z", "max_forks_repo_path": "parser/state.js", "max_forks_repo_name": "CodeLabClub/scratch-LN", "max_forks_repo_forks_event_min_datetime": "2018-03-24T19:40:55.000Z", "max_forks_repo_forks_event_max_datetime": "2021-06-09T07:51:42.000Z"}
{"max_stars_count": 20, "max_issues_count": 98, "max_forks_count": 8, "avg_line_length": 21.0108108108, "max_line_length": 109, "alphanum_fraction": 0.5562130178}
/**
 * Storage of the state of the workspace
 *
 * things that need to be possible are:
 *      - get the last block and the type of the block
 *
 * @file   This files defines the State class.
 * @author Ellen Vanhove.
 */
import {INPUTTYPE, MODUS} from "./typeConfig";


export class State {

    constructor() {
        this.reset();
    }

    /**
     * reset the state:
     *  removes all stored information
     */
    reset() {
        //list of all blocks
        this.blocks = [];
        //this.blocks.push({ID:-1}); //this should not happen normally but this way nothing breaks during dev
        this.modus = MODUS.NONE;
        this.interrupted = false;
        //when opening a new context the previous is stored here
        this.storage = [];
        this.expectedInput = INPUTTYPE.NONE;
    }



    pushStorage(){
       this.storage.push(
            {
                blocks : this.blocks,
                modus: this.modus
            }
        );
       this.blocks = [];
    }

    popStorage(){
        if(this.storage.length>0) {
            let stored = this.storage.pop();
            this.setBack(stored);
        }
    }

    setBack(stored) {
        this.blocks = stored.blocks;
        this.modus = stored.modus;
    }

    isBuildingStackBlock(){
        return this.modus === MODUS.STACK;
    }

    isBuildingReporterBlock(){
        return this.modus === MODUS.REPORTER;
    }

    isBuildingBooleanBlock(){
        return this.modus === MODUS.BOOLEAN;
    }

    getModus(){
        return this.modus;
    }

    /**
     * store informaton about a block
     * @param id the id of the block
     * @param shape the shape of the block
     */
    addBlock(id){
        this.blocks.push({ID:id})
    }


    /**
     * return the id of the last added block
     * @returns {string}
     */
    getFirstBlockID(){
        return this.blocks[0].ID;
    }


    /**
     * return the id of the last added block
     * @returns {string}
     */
    getLastBlockID(){
        return this.blocks[this.blocks.length-1].ID;
    }

    startStack(){
        this.pushStorage();
        this.modus = MODUS.STACK;
        this.interrupted=false;
    }

    endStack(){
        this.popStorage();
    }

    interruptStack(){
        if(this.storage.length>0) {
            let stored = this.storage[0];
            this.setBack(stored);
            this.storage = [];
        }
        this.interrupted = true;
    }

    isInterruptedStack(){
        return this.interrupted;
    }

    openBooleanBlock(){
        this.pushStorage();
        this.modus = MODUS.BOOLEAN;
    }

    closeBooleanBlock(){
        this.popStorage();
    }

    openReporterBlock(){
        this.pushStorage();
        this.modus = MODUS.REPORTER;
    }

    closeReporterBlock(){
        this.popStorage();
    }

    amountOfPreviousBlocksOnStack(){
        return this.blocks.length;
    }

    hasPreviousBlocksOnStack(){
        return this.blocks.length > 0;
    }

    hasPreviousConnectedBlocks(){
        return this.hasPreviousBlocksOnStack() || this.storage.length > 1;
    }

    isExpectingBoolean(){
        return this.expectedInput === INPUTTYPE.BOOLEAN;
    }

    isExpectingNumber(){
        return this.expectedInput === INPUTTYPE.NUMBER
            || this.expectedInput === INPUTTYPE.WHOLE_NUMBER
            || this.expectedInput === INPUTTYPE.POSITIVE_NUMBER
            || this.expectedInput === INPUTTYPE.INTEGER
            || this.expectedInput === INPUTTYPE.ANGLE;
    }

    setExpectingInput(type){
        if(! type in INPUTTYPE){
            throw new Error("type not valid");
        }
        this.expectedInput = type;
    }

    getExpectingInputType(){
        return this.expectedInput;
    }

    expectBoolean(){
        this.expectedInput=INPUTTYPE.BOOLEAN;
    }

    expectNothing(){
        this.expectedInput = INPUTTYPE.NONE;
    }

}