import Tool from './Tool';
import {Tools} from '../Tools/index' 
import React from 'react';
import {StyleSheet,View , Text} from 'react-native';
import Mouse from './mouse';
import Keyboard from './keyboard';

import BaseSystem from './System';


function log(args){
	console.log(args)
}

export default class ToolBox extends BaseSystem {
	constructor(CoreSystem, defaultTool){
		super();
		this.CoreSystem = CoreSystem;
		this.tools = {};
		this.activeTool = null;
		this.selectedNode = null;
		this.loadTools(defaultTool);

		this.Keyboard = new Keyboard().listen();
	    this.Keyboard.on(27, () => {
	    	if(this.activeTool) {
	    		this.activeTool.active = false;
	    		this.activeTool = null;
	      		this.forceUpdate();
	      	}
	    });
	}

	on(e) { return this.activeTool.on(e) }

	editNode(node){
		this.selectedNode = node;
		return this.activeTool.editViewNode(this.CoreSystem,node);
	}

	selectNode(node) {
		this.selectedNode && (this.selectedNode.props.selected = false);
		this.selectedNode = node;
		this.selectedNode.props.selected = true;
	}

	selectTool(key){
		this.activeTool && (this.activeTool.active = false);
		let tool = this.tools[key];
		this.activeTool = tool;	
		tool.active = true;
		this.forceUpdate()
	}

	loadTools(defaultTool){
		log("Loading Tools")
		Object.keys(Tools).map((key) => {
			if( Tools[key].prototype instanceof Tool === false ){
				throw new Error("This is not a Tool")
			}
			if(this.tools[key]){
				log("Tool already Exists: ",key);
			}
			this.tools[key] = new Tools[key]();
		})
		if(defaultTool && this.tools[defaultTool]) {
			this.activeTool = this.tools[defaultTool];
		}	
	}

	render(){
		let tools = Object.keys(this.tools).map((key) => {
			let toolStyle = styles.tool;
			if(this.activeTool !== null){
				if(this.activeTool.constructor.name === key){
					toolStyle = styles.activeTool;
				}
			}
			return(<View onClick={() => this.selectTool(key) }  style={toolStyle} key={key}>
				{this.tools[key].render()}
			</View>)
		});
		return tools;
	}

}
/** Must be remove from here**/
const styles = StyleSheet.create({
	tool:{
	},
	activeTool:{
	
	}
})