/**
 * Created by baoyinghai on 6/28/17.
 */
import React from 'react';
import NGTCreator from './NGTCreator';

import TEST_OBJECT from './testObj';

export default class Page extends React.Component {

  constructor(props) {
    super(props);
    this.state = {
      ...TEST_OBJECT.DSC
    };
    // this.ART = this.props.ART;
    this.ART = TEST_OBJECT.ART;
    this.NGT = NGTCreator.ngtCreate(this.ART);
  }

  _dispatch = (path = [], value) => {
    const last = path.pop();
    path.reduce((obj, p) => obj[p], this.state)[last] = value;
    this.setState({
      ...this.state
    });
  };

  pageGenerator = node => {
    if (!node) return null;
    const appendProps = { dispatch: this._dispatch };
    if (node.childrenNode) {
      appendProps.children = node.childrenNode.map(this.pageGenerator);
    }
    return node.renderComp(this.state, appendProps);
  };

  render() {
    // const { ART } = this.props;
    const Comp = this.pageGenerator(this.NGT);
    return Comp;
  }
}
