import React, { Fragment, useEffect, useState,useRef } from 'react'
import ReactDOM from 'react-dom'
import PropTypes from 'prop-types';
import SvgIcon from '@material-ui/core/SvgIcon';
import Icon from '@material-ui/core/Icon';
import { alpha, fade, makeStyles, withStyles } from '@material-ui/core/styles';
import TreeView from '@material-ui/lab/TreeView';
import ExpandMoreIcon from '@material-ui/icons/ExpandMore';
import ChevronRightIcon from '@material-ui/icons/ChevronRight';
import TreeItem, { useTreeItem } from '@material-ui/lab/TreeItem';
import Collapse from '@material-ui/core/Collapse';
import { useSpring, animated } from 'react-spring'; // web.cjs is required for IE 11 support
//import '../css/style.css';

function TreeViewMUI(props) {


  const [expanded, setExpanded] = React.useState([]);
  const [selected, setSelected] = React.useState([]);
  const [nodosOpen, setNodosOpen] = useState(["1"]);
  const mounted = useRef();
   function MinusSquare(props) {
      return (
         <SvgIcon fontSize="inherit" style={{ width: 14, height: 14 }} {...props}>
            {/* tslint:disable-next-line: max-line-length */}
            <path d="M22.047 22.074v0 0-20.147 0h-20.12v0 20.147 0h20.12zM22.047 24h-20.12q-.803 0-1.365-.562t-.562-1.365v-20.147q0-.776.562-1.351t1.365-.575h20.147q.776 0 1.351.575t.575 1.351v20.147q0 .803-.575 1.365t-1.378.562v0zM17.873 11.023h-11.826q-.375 0-.669.281t-.294.682v0q0 .401.294 .682t.669.281h11.826q.375 0 .669-.281t.294-.682v0q0-.401-.294-.682t-.669-.281z" />
         </SvgIcon>
      );
   }
   function MinusSquare2(props) {
      return (
         <Icon className="fa fa-minus-circle" fontSize="inherit"  {...props}>
         </Icon>
      );
   }


   function PlusSquare(props) {
      return (
         <SvgIcon fontSize="inherit" style={{ width: 14, height: 14 }} {...props}>
            {/* tslint:disable-next-line: max-line-length */}
            <path d="M22.047 22.074v0 0-20.147 0h-20.12v0 20.147 0h20.12zM22.047 24h-20.12q-.803 0-1.365-.562t-.562-1.365v-20.147q0-.776.562-1.351t1.365-.575h20.147q.776 0 1.351.575t.575 1.351v20.147q0 .803-.575 1.365t-1.378.562v0zM17.873 12.977h-4.923v4.896q0 .401-.281.682t-.682.281v0q-.375 0-.669-.281t-.294-.682v-4.896h-4.923q-.401 0-.682-.294t-.281-.669v0q0-.401.281-.682t.682-.281h4.923v-4.896q0-.401.294-.682t.669-.281v0q.401 0 .682.281t.281.682v4.896h4.923q.401 0 .682.281t.281.682v0q0 .375-.281.669t-.682.294z" />
         </SvgIcon>
      );
   }

   function PlusSquare2(props) {
      return (
         <Icon className="fa fa-plus-circle" fontSize="inherit" {...props}>
         </Icon>
      );
   }

   function CloseSquare(props) {
      return (
         <SvgIcon className="close" fontSize="inherit" style={{ width: 14, height: 14 }} {...props}>
            {/* tslint:disable-next-line: max-line-length */}
            <path d="M17.485 17.512q-.281.281-.682.281t-.696-.268l-4.12-4.147-4.12 4.147q-.294.268-.696.268t-.682-.281-.281-.682.294-.669l4.12-4.147-4.12-4.147q-.294-.268-.294-.669t.281-.682.682-.281.696 .268l4.12 4.147 4.12-4.147q.294-.268.696-.268t.682.281 .281.669-.294.682l-4.12 4.147 4.12 4.147q.294.268 .294.669t-.281.682zM22.047 22.074v0 0-20.147 0h-20.12v0 20.147 0h20.12zM22.047 24h-20.12q-.803 0-1.365-.562t-.562-1.365v-20.147q0-.776.562-1.351t1.365-.575h20.147q.776 0 1.351.575t.575 1.351v20.147q0 .803-.575 1.365t-1.378.562v0z" />
         </SvgIcon>
      );
   }

   function CloseSquare2(props) {
      return (
         <Icon className="fa fa-times-circle" fontSize="inherit" style={{ width: 14, height: 14 }} {...props}>
         </Icon>
      );
   }

   function TransitionComponent(props) {
      const style = useSpring({
         from: { opacity: 0, transform: 'translate3d(20px,0,0)' },
         to: { opacity: props.in ? 1 : 0, transform: `translate3d(${props.in ? 0 : 20}px,0,0)` },
      });

      return (
         <animated.div style={style}>
            <Collapse {...props} />
         </animated.div>
      );
   }
   TransitionComponent.propTypes = {
      /**
       * Show the component; triggers the enter or exit states
       */
      in: PropTypes.bool,
   };


   const useStyles = makeStyles({
      root: {
         height: '100%',
         flexGrow: 1,
         maxWidth: '100%',
         color: 'white',
         //fontSize: '14px',
         //background:'cyan'
         "@global": {
            ".MuiTreeItem-content": {
               "&:hover": {
                  color: '#10A599',
               },
               marginTop: '21px',
               marginLeft: '1.05em',
            },
         },
      },
   });



   //   const renderTree = (nodes) => (
   //     <TreeItem key={nodes.id} nodeId={nodes.id} label={nodes.name} TransitionComponent={TransitionComponent} >
   //       {Array.isArray(nodes.children) ? nodes.children.map((node) => renderTree(node)) : null} 
   //     </TreeItem>
   //   );


   const renderTree = (nodes) => (
      <StyledTreeItem key={nodes.id} nodeId={nodes.id.toString()} label={nodes.name}>
         {Array.isArray(nodes.children) ? nodes.children.map((node) => renderTree(node)) : null}
      </StyledTreeItem>
   );



   const StyledTreeItem = withStyles((theme) => ({
      iconContainer: {
         '& .close': {
            opacity: 0.3,
         },
      },
      group: {
         marginLeft: 21,
         paddingLeft: 10.8,
         borderLeft: `1px dashed ${theme.palette.common.white}`,
         //borderLeftColor:'white',       
         //fontSize: '14px',
         //color:'white'

      },
      root: {
         position: "relative",
         "&:before": {
            pointerEvents: "none",
            content: '""',
            position: "absolute",
            width: 27,
            left: -9.6,
            top: 10,
            borderBottomColor: 'white',
            // horizontal border
            borderBottom: (props) =>
               // only display if the TreeItem is not root node
               props.nodeId !== "1" &&
                  // only display if the TreeItem has any child nodes
                  props.children &&
                  props.children.length > 0
                  ? `1px dashed ${theme.palette.common.white}`
                  : `1px dashed ${theme.palette.common.white}`

         },
      },
      label: {
         fontSize: '16px',
         textAlign: 'left',
      },

   }))((props) => <TreeItem {...props} onLabelClick={onLabelClick} onIconClick={onIconClick} TransitionComponent={TransitionComponent} />);



   let pasa = true;

   const onIconClick = (nodeId) => {
      pasa = false;
   }


   const onLabelClick = (nodeId) => {
      pasa = true;
   }
   const [opnnodos, setOpennodos] = useState([]);
   const [eventoSel, setEventoSel] = useState(true);


   const classes = useStyles();
   const onNodeSelect = (event, nodeId) => {
      //debugger;
      if (pasa == true){
         let list = {}
         list = buscarNodo(props.data, nodeId);
         props.handlerArbol(list);         
      } 
      setSelected(nodeId);
   }

   const buscarNodo = (pLista, pId) => {
      debugger
      let lista = null;
      for (let j = 0; j < pLista.length; j++) {
         if (pLista[j].id == pId) {
            lista = { id: pLista[j].id, name: pLista[j].name, valor: pLista[j].valor };
            return lista;
         }

         if (pLista[j].children != undefined) {
            lista = buscarNodo(pLista[j].children, pId);
            if (lista != null) {
               return lista;
            }
         }
         if (lista != null) {
            return lista;
         }
      }
      return lista
   }


   //let open = ['1', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '147', '77', '137', '2'];

   return (
      <Fragment>
                  <TreeView
                     className={classes.root}
                     defaultCollapseIcon={<MinusSquare2 />}
                     defaultExpandIcon={<PlusSquare2 />}
                     defaultEndIcon={<CloseSquare2 />}
                     selected={selected}
                     multiSelect={false}
                     onNodeToggle ={props.handleToggle}
                     onNodeSelect={onNodeSelect}
                     //defaultExpanded={props.defaultExpanded}
                     expanded={props.defaultExpanded}
                  >
                     {renderTree(props.data[0])}
                  </TreeView>
      </Fragment>
   );
};
export default TreeViewMUI;

