const fs = require('fs');

const startPath = 'src/Screens/Root';
const findNavigator = (render = (route, name) => {}, importRender = (route, name) => {}, path = startPath, route = '') => {
    let result = '';
    const list = fs.readdirSync(path, { withFileTypes: true })
        .filter(dir => dir.isDirectory())
        .map(dir => dir.name);
    for(let dir of list) {
        if (dir=='component') continue;
        findNavigator(render, importRender, path + '/' + dir, route + '/' + dir);
    }

    if(route != '') {
        const names = fs.readdirSync(path, { withFileTypes: true })
            .filter(file => file.isFile() && file.name.endsWith('.js'))
            .map(file => {
                let name = file.name.replace('.js', '');
                if(name == 'index') name = 'Index';
                return {name: name, routePostfix: name == "Index" ? "" : name};
            });
        if(names.length > 0) {
            render(route, names);
            importRender(route, names);
        }
    };
}

/* Generate Screens */
const generateScreens = () => {
    const screenRootPath = '~/Screens/Root';

    let navigators = '';
    let importers = '';
    findNavigator((route, names) => {
        let navigator = '';
        let findIndex = false;

        navigator += `\t\t'${route}': createStackNavigator({\n`;
        for(name of names) {
            if(name.name == 'Index') findIndex = true;
            const postfix = name.routePostfix != "" ? "/"+name.routePostfix: "";
            const importName = (route.replace(/\//g, '_') + postfix.replace(/\//g, '_')).replace('_','');
            navigator += `\t\t\t'${name.name}': ${importName},\n`;
        }
        navigator += `\t\t}, { ...navigationOptions }),\n`;

        if(findIndex) navigators += navigator;
    }, (route, names) => {
        let importer = '';
        let findIndex = false;

        for(name of names) {
            if(name.name == 'Index') findIndex = true;
            const postfix = name.routePostfix != "" ? "/"+name.routePostfix: "";
            const importName = (route.replace(/\//g, '_') + postfix.replace(/\//g, '_')).replace('_', '');
            importers += `import ${importName} from '${screenRootPath}${route}${postfix}';\n`;
        }

        if(findIndex) importers += importer;
    });

    const string = `import { createStackNavigator } from 'react-navigation-stack';
${importers}

export default ScreensCreator = (navigationOptions) => {
    return {
${navigators}
    }
};
`;
    
    fs.writeFileSync('src/Generated/Screens.js', string, 'utf8');
}
generateScreens();

/* Generate Independent */
const generateIndependent = () => {
    const popupRootPath = '~/Screens/Independent';

    let navigators = '';
    let importers = '';
    findNavigator((route, names) => {
        for(name of names) {
            const postfix = name.routePostfix != "" ? "/"+name.routePostfix: "";
            const importName = (route.replace(/\//g, '_') + postfix.replace(/\//g, '_')).replace('_','');
            navigators += `\t\t'${route}/${name.name}': ${importName},\n`;
        }
    }, (route, names) => {
        for(name of names) {
            const postfix = name.routePostfix != "" ? "/"+name.routePostfix: "";
            const importName = (route.replace(/\//g, '_') + postfix.replace(/\//g, '_')).replace('_', '');
            importers += `import ${importName} from '${popupRootPath}${route}${postfix}';\n`;
        }
    }, 'src/Screens/Independent');

    const string = `
${importers}

export default IndependentCreator = () => {
    return {
${navigators}
    }
};
`;
    
    fs.writeFileSync('src/Generated/Independent.js', string, 'utf8');
}
generateIndependent();
