import React, {Fragment} from 'react';
import { View } from 'react-native';

import { Resolver } from './Types';
import Injection from './systems/InjectionSystem';

// EventSystem

export default class Module extends React.Component{

	constructor(props) {
		super(props);
		this.Core = Injection.inject('Core');
		this.Modules = Injection.inject('Modules');


		let {
			ModuleID,
			EventSystem
		} = props;
		let eventName = ModuleID + "HitCheck"
		EventSystem && EventSystem.on(eventName, ({x,y}) => {
			return true;
		});
		this.isTemplate = false
		//console.log("GEN EV: " , eventName, EventSystem);
		this._containerStyle = {
			marginLeft: 24,
			marginRight: 24
		}
		this.counter = 0;
	}

	getId() {
		return this.props.ModuleID;
	}

	renderModule(ctor, namespace = "default", props = {}) {
		let VS = this.props.CoreSystem.ViewSystem;
		if(!this.props.CoreSystem.ModuleSystem.get(ctor, namespace)) return null;
		return VS.renderModule({
          	value: ctor,
          	namespace,
          	props
        }, this.getId() +"C"+ (this.counter++));
	}

	createAction(action) {
		if(!this.props.CoreSystem) {
			console.warn("This module was constructed without a CoreSystem");
			return;
		}
		return this.props.CoreSystem.ActionSystem.createAction(action);
	}

	setContainerStyle(style) { 
		this._containerStyle = style;
	}

	display() {
		throw new Error("You must extend this base Module and override display method!");
	}

	module(mod, props) {
		return mod ? 
		this.renderModule(mod.value, mod.namespace, props) : null;
	}
	moduleArray(modules, props) {
		return modules ? modules.map((mod) => this.module(mod,props)) : null;
	}
	render() {
		this.counter = 10100;
		return <View ModuleID={this.props.ModuleID} style={[{overflow:'hidden'}]} MY_WRAPPER_FROM_BASE_MODULE={true}>
			{this.display()}
		</View>
		// throw new Error("You must extend this base Module and override render method!");
	}

	getProps() {
		return Resolver(this.constructor.Inputs, this.props);
	}
}