import './index.scss';

import React from 'react';
import { CommonSiderMenuComponent } from './CommonSiderMenu.component';

export class CommonSiderMenu extends React.PureComponent {

    state = {
        defaultSelectedKeys: [] ,
        defaultOpenKeys: [] ,
        openKeys: [] ,
        selectedKeys: []
    };

    jump = itemInfo => {
        this.setState({
            openKeys: [ itemInfo.keyPath[ 1 ] ] ,
            selectedKeys: [ itemInfo.keyPath[ 0 ] ]
        });

        this.props.history.push(itemInfo.key);
    };


    componentDidMount() {
        this.props.history.listen(this.resetKeys);
        if (this.props.list && this.props.list.length) {
            this.setDefaultOpenKeys(this.props);
            this.setDefaultSelectedKeys(this.props);
        }
    }

    UNSAFE_componentWillReceiveProps(nextProps) {
        setTimeout(() => {
            this.setDefaultOpenKeys(nextProps);
            this.setDefaultSelectedKeys(nextProps);
        } , null);
    }

    resetKeys = location => {
        const {pathname} = location;
        const splitPathname = pathname.split('/');
        const openKey = splitPathname.slice(0 , 3).join('/');
        const selectedKey = splitPathname.slice(0 , 4).join('/');
        this.setState({
            openKeys: [ openKey ] ,
            selectedKeys: [ selectedKey ]
        });
    };

    setDefaultOpenKeys(props) {
        const {currentIndex , allList} = props;
        const mapList = allList[ currentIndex ].children || [];
        const {pathname} = props.history.location;
        let notFound = true;

        mapList.forEach(v => {
            if (notFound) {
                if (new RegExp('^' + v.url).test(pathname)) {
                    notFound = false;
                    this.setState({
                        defaultOpenKeys: [ v.url ] ,
                        openKeys: [ v.url ]
                    });
                }
            }
        });

    }

    setDefaultSelectedKeys(props) {
        const {currentIndex , allList} = props;
        const mapList = allList[ currentIndex ].children || [];
        const {pathname} = props.history.location;
        let notFound = true;
        mapList.forEach(v => {
            if (notFound) {
                if (new RegExp('^' + v.url + '$').test(pathname)) {

                    notFound = false;
                    this.setState({
                        defaultSelectedKeys: [ v.url ] ,
                        selectedKeys: [ v.url ]
                    });
                    return;
                }
                v.children && v.children.forEach(cv => {
                    if (new RegExp('^' + cv.url).test(pathname) && notFound) {
                        notFound = false;

                        this.setState({
                            defaultSelectedKeys: [ cv.url ] ,
                            selectedKeys: [ cv.url ]
                        });
                    }
                });
            }

        });
    }

    handleSubMenuChange = openKeys => this.setState({openKeys});

    render() {
        const {list , systemDescription} = this.props;
        const {defaultSelectedKeys , defaultOpenKeys , openKeys , selectedKeys} = this.state;
        return (
            <CommonSiderMenuComponent
                list={list}
                systemDescription={systemDescription}
                jump={this.jump}
                defaultSelectedKeys={defaultSelectedKeys}
                defaultOpenKeys={defaultOpenKeys}
                openKeys={openKeys}
                selectedKeys={selectedKeys}
                onOpenChange={this.handleSubMenuChange}
            />
        );
    }
}
