
class RecipeManagement {

	constructor() { }

	init(mainViewModel) {
		this.mainViewModel = mainViewModel;
		this.recipeManagerVM = this.mainViewModel.getChildByName('Recipe Manager');
                this.outputs = [null];
                let outputsName = "sigOutputName";
                let tempOutputs = document.getElementsByTagName('sig-control-output');
		if(this.recipeManagerVM) {
			this.recipeManager = this.recipeManagerVM.getDomRef();
			if(this.recipeManager) {
                let load = this.recipeManager._loadRecipe.bind(this.recipeManager); //Speichern der _loadRecipe Methode, des RecipeManagers

                // _loadRecipe Methode überschreiben; Zuerst wird das Rezept angewandt, dann wird die ursprüngliche Methode aufgerufen, um das Rezept
                // im Control anzuzeigen
                this.recipeManager._loadRecipe = async (path) => {
                    sigApi.recipeManager.load(path, 0, 0, 0, () => {}, () => {}, () => {
                        load(path);
                    })
                }

				sigApi.fileManager.readDir('c:\\').then((data) => {
					let recFile = data.parent.nodes.map((node) => {return node.dpne}).reduce((acc, val) => {
						if(val === 'recipe000001') acc = val;
						return acc;
					}, '')

					sigApi.vm.addRenderCallback(this.recipeManager, () => {
						if(!recFile || recFile === '') {
							sigApi.recipeManager.save('c:/userdata/recipe//recipe000001').then(()=>{
								this.recipeManager.filename = 'c:/userdata/recipe//recipe000001';
								setTimeout(() => {
									this.recipeManager._loadRecipe(this.recipeManager.filename)
                                                                     //   outputsName = document.getElementById(this.recipeManager.filename)
                                                                      
								},1000)
								 
							})
						} else {
							this.recipeManager.filename = 'c:/userdata/recipe//recipe000001';
							setTimeout(() => {
								this.recipeManager._loadRecipe(this.recipeManager.filename)
                                                              
							},1000)
                                 
						}
					})
					setTimeout(() => {
						
					}, 5000)
					
				})
			}
		}
		// Add your own initialization

		this.initialized = true;
	}

	cleanUp(mainViewModel) {
		// Add your own cleanUp code

		this.initialized = false;
	}

	onActiveChanged(active) {
		// Add your own active state handling code

		this.active = active;
	}

	static create() {
		return new RecipeManagement();
	}

}

// Register the RecipeManagement for a composite control
window.registerCompositeControlModule('_STD_Tab_Advanced_RecipeManager', RecipeManagement.create);