import React from 'react';
import {StyleSheet, View, Text} from 'react-native';
import Library from 'trapilib/dist/lib';
import { TextInput, Image, Button } from 'react-native';

import TextEditor from '../Components/TextEditor';
// import Numbers from '../Components/Numbers';
// import ColorEditor from '../Components/ColorEditor';
import TypeHandler from '../Components/TypeHandler.js';

import BaseSystem from './System';




let {
	CoreSystem,
	ViewSystem,
	ViewNode,
	DataTypes
} = Library; 
function download(filename, text) {
  var element = document.createElement('a');
  element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
  element.setAttribute('download', filename);

  element.style.display = 'none';
  document.body.appendChild(element);

  element.click();

  document.body.removeChild(element);
}
export default class SideBar extends BaseSystem {
	constructor(CoreSystem, ToolBox){
		super();
		this.CoreSystem = CoreSystem;
		this.ToolBox = ToolBox;
	}

	import() {
		let CurrentView = this.CoreSystem.getCurrentView();
		let text = window.prompt("Feed me plz");
		if(text) {
			CurrentView.import(JSON.parse(text), this.ContainerNode.id);
		}
	}
	export() {
		let CurrentView = this.CoreSystem.getCurrentView();
		let data = CurrentView.export();
		download("Export_" + new Date().toISOString()+".txt", JSON.stringify(data));
	}

	removeNode(node) {
		let CurrentView = this.CoreSystem.getCurrentView();
		console.log(node)
		if (CurrentView.has(node)) {
			CurrentView.remove(node);
		}
		this.forceUpdate();
	}

    editNode(node, text,key){
		node.props[key] = text;
		this.forceUpdate();
    }

    createHandler(Structure, viewNodeProps, key, onChange) {
    	let Editor = TypeHandler(Structure);
		let editorProps = {
			title: key,
			onChange: (value) => {
				onChange && onChange(value, key)
			}
		};


		switch(Structure.type){
			case DataTypes.Types.Array:
				editorProps.value = viewNodeProps[key];
				break;
			case DataTypes.Types.Integer:
			case DataTypes.Types.Bool:
				editorProps.number = viewNodeProps[key];
				break;
			case DataTypes.Types.Text:
				editorProps.text = viewNodeProps[key];
				break;
			default:
				//Must create a generic Vuiew Component
				return (<View></View>)
		}

		return <Editor {...editorProps} key={key}/>
    }

	render(){
		let ModuleSystem = this.CoreSystem.ModuleSystem;
		let CurrentView = this.CoreSystem.getCurrentView();
		
		let selectedNode = this.ToolBox.selectedNode;
		
		let Structure = {},
			styles = {},
			nodeStructure = {};
		let viewNodeProps = {},
			nodeProps = {};

		if (selectedNode && CurrentView.has(selectedNode)) {
			this.ContainerNode = selectedNode;
			if(selectedNode.content && selectedNode.content.value) {
				let ctor = ModuleSystem.fromViewNode(selectedNode.content);
				console.log("YOUR LOOKINGH")
				console.log(ctor)
				styles = ctor.Styles;
				viewNodeProps = ModuleSystem.validateProps(ctor, selectedNode.content.props);
				Structure = ctor.Inputs;
			}
			nodeProps = ModuleSystem.validateProps(selectedNode.ctor, selectedNode.props);
			nodeStructure = selectedNode.ctor.Inputs;
		} else {
			this.ContainerNode = null;
		}

		let StyleData = Object.keys(styles || {}).map((key,index) => 
			<option key={selectedNode.id + key}  value = {key} selected={this._selectedStyle === key}>
				{key}	
			</option>

		)

		console.log("@@@@@@@@@@@@@@@@@@")
		console.log(StyleData)
		console.log(styles)
		console.log("!!!!!!!!!!!!!!!!!!!!")
		let contentData = Object.keys(Structure || {}).map((key,index) => 
			<View key={selectedNode.id + key}>
			{this.createHandler(Structure[key], viewNodeProps, key, (v, f) => 
				this.editNode(selectedNode.content, v,f))}
			</View>
		)
		let containerData =Object.keys(nodeStructure || {}).map((key, index) => 
			<View key={selectedNode.id + key}>
			{this.createHandler(nodeStructure[key], nodeProps, key, (v,f) => 
				this.editNode(selectedNode, v, f))}
			</View>
		);



		return (
  			<View>
  				<Text style={SideBarStyle.title}>Side Bar</Text>

  				<View key={Math.random()}>
  					<select onChange={(event) => {
  						this._selectedStyle = event.target.value;
  						this.ContainerNode.content.props = {...this.ContainerNode.content.props, ...styles[event.target.value]};
  						this.forceUpdate();
  					}}>
  						{StyleData}
  					</select>
  				</View>
  				<View>
  					{this.ContainerNode && <View key={this.ContainerNode.id}>
  						<Text>Container</Text>
  						<Button onPress={() => this.import()} title={'Import'}/>
  						<Button onPress={() => this.export()} title={'Export'}/>
  						<TextEditor title="Inner Columns" 
  						text={this.ContainerNode && CurrentView.getChildren(this.ContainerNode).length +''} 
  						onChange={(cols) => {
  							alert("Not working");
  						}}/>
  						<Image  name='near me'
		  				style={{width: 20, height: 20 , color:'#606060'}}
		  				source= {require('../assets/icon.png')}
		  				containerStyle={{}}
		  				onClick={() => this.removeNode(this.ContainerNode)}
		  				/>
  					</View>}
  					{containerData}
  				</View>


  				<View style={PropertiesContainer.container}>
  			 		{contentData}
  			 	</View>


  			 	
  			</View>
  			)
	}
}




const SelectedStyle = StyleSheet.create({
	container:{
		padding:5,
		borderWidth:2,
		borderColor:'green'
	}
})



const PropertiesContainer = StyleSheet.create({
	container:{
		marginTop:20,
		borderTopWidth:0.8,
		borderColor:'#D0D0D0'
	},
	child:{
		padding:10
	}
})

const SideBarStyle = StyleSheet.create({
	container:{
		padding:1,
		flex:1,
		flexDirection:'row'
	},
	title:{
		fontSize:20,
		textAlign:'center',
		fontFamily:'halvetica',
		color:'black',
		borderBottomWidth:1,
		borderBottomColor:'black'
	},
	body:{
		paddingLeft:5,
		paddingRight:5
	}
})