import React, {Component} from 'react';
import {connect} from 'react-redux';
import {Route, Switch} from 'react-router-dom';
import { withRouter } from 'react-router';
import {setActiveSubModule} from '../../actions/page/pageActions';
import {MainContainers} from '../../constants/appContainers';
import MyComponent from './MyComponent.jsx';

class WiaasRouter extends Component {
    filterModules(wiaasModules) {
        const filteredModules = [];
        let subModules = [];
        if(wiaasModules.subModules){
            Object.keys(wiaasModules.subModules).forEach((moduleKey) => {
                subModules = subModules.concat(wiaasModules.subModules[moduleKey]);
            });
        }
        const mergedModules = wiaasModules.modules.concat(subModules);
        mergedModules.forEach((module) => {
            if(typeof MainContainers[module.name] !== 'undefined'){
                filteredModules.push(module);

                const params = MainContainers[module.name].params;
                if(params.length){
                    const auxModule = Object.assign({}, module);
                    params.forEach((param)=> {
                        auxModule.url += '/:' + param;
                    });
                    filteredModules.push(auxModule);
                }
            }else{
                console.log('Unavailable module' + module.name);
            }
        });

        return  filteredModules;
    }

    isInMenu(module) {
        return parseInt(module.isInMenu, 10) === 1;
    }

    componentWillReceiveProps(nextProps) {
        const location = nextProps.location  && nextProps.location.pathname ?  nextProps.location.pathname : null;
        const wiaasModules = nextProps.wiaasModules && nextProps.wiaasModules.modules ? this.filterModules(nextProps.wiaasModules) : null;

        if(location && wiaasModules && location !== '/') {
            let moduleByPath = wiaasModules.find(moduleData => {
                return location.indexOf('/' + moduleData.url) !== -1;
            });
            if(moduleByPath){
                if(moduleByPath.moduleUrl){
                    nextProps.dispatch(setActiveSubModule(moduleByPath.url));
                    nextProps.addActiveClass(moduleByPath.moduleUrl);
                }else{
                    nextProps.addActiveClass(moduleByPath.url);
                }
            }
        }else{
            if(wiaasModules){
                const firstInMenu = wiaasModules.find(moduleData => {
                    return this.isInMenu(moduleData);
                });
                nextProps.addActiveClass(firstInMenu.url);
            }
        }
    }

    render() {
        const wiaasModules = this.props.wiaasModules && this.props.wiaasModules.modules ? this.filterModules(this.props.wiaasModules) : [];

        return (
            wiaasModules &&
            <div className="app-content">
                <Switch>
                    <Route exact path='/' render={(props) => (<MyComponent {...props} TagName={MainContainers.Dashboards.container}/>)} />
                    {wiaasModules.map((module, index) =>
                             <Route key={'container-' + index}
                                exact
                                path={'/' + module.url}
                                render={(props) => (<MyComponent {...props} TagName={MainContainers[module.name].container}/>)}
                                />
                    )}
                </Switch>
            </div>
        );
    }
}
const mapStateToProps = (state) => ({wiaasModules: state.auth.modules});

export default withRouter(connect(mapStateToProps)(WiaasRouter));
