import {Gamesystem} from "./Gamesystem";
import {SimpleState} from "./states/SimpleState";
import {SimpleTransition} from "./transitions/SimpleTransition";
import {State} from "./states/State";
import {Transition} from "./transitions/Transition";
import {ProductState} from "./states/ProductState";
import {ProductTransition} from "./transitions/ProductTransition";
import {ProductGamesystem} from "./ProductGamesystem";
export class SimpleGamesystem extends Gamesystem<SimpleState, SimpleTransition> {

  parentGamesystem: ProductGamesystem | undefined



  createState(label: string, description: string): SimpleState | undefined {
    if(label == null) {
      return undefined;
    }

    if(description == null) {
      description = "";
    }

    const state = new SimpleState(label, description);
    if(this.states.find(s => s.stateLabel == label) == undefined) {
      this.states.push(state);
      return state;
    } else {
      return undefined
    }
  }

  createTransition(startingState: SimpleState, endingState: SimpleState): SimpleTransition | undefined{
    if((startingState == null || endingState == null) || startingState === endingState) {
      return undefined;
    }

    const transition = new SimpleTransition(startingState, endingState);
    if(this.transitions.find(t => t.startingState === startingState && t.endingState === endingState) == undefined) {
      this.transitions.push(transition)
      return transition;
    } else {
      startingState.removeOutgoingTransition(transition);
      endingState.removeIncomingTransition(transition);
      return undefined
    }

  }


  removeState(state: SimpleState): boolean {
    const updatedStates = this.states.filter(s => s !== state);
    const updated = updatedStates.length != this.states.length;
    this.states = updatedStates;

    this.transitions = this.transitions.filter(t => t.startingState !== state && t.endingState !== state);

    return updated;
  }
}