const { languageForDatabaseType } = require('../config/common');
module.exports = function(name,argObj) {
   
    let {dependencies, devDependencies} = require('../config/languageDependencies.json')[argObj.language]
    const dataBaseBags = languageForDatabaseType[argObj.language][argObj.databaseType]
    dataBaseBags.forEach( item => {
        if ( item.type =='pro' ) {
            dependencies[item.name] = item.version
        }else {
            devDependencies[item.name] = item.version
        }
        
    })

    if (argObj.language == 'nodejs') return nodeBagTemplate(name, dependencies, devDependencies);
    if (argObj.language == 'php') return phpBagTemplate(name, dependencies, devDependencies);
    switch (argObj.language) {
        case 'nodejs':
            return nodeBagTemplate(name, dependencies, devDependencies);
            break;
        case 'php':
            return phpBagTemplate(name, dependencies, devDependencies);
        case 'java':
            return javaBagTemplate(name, dependencies, devDependencies);
        default:
            break;
    }

}

const nodeBagTemplate = function (name, dependencies, devDependencies) {
    return `
{
    "name": "${name}",
    "version": "1.0.0",
    "description": "",
    "main": "app.js",
    "scripts": {
        "test": "echo \\"Error: no test specified\\" && exit 1",
        "start:dev": "nodemon ./app.js"
    },
    "repository": {
        "type": "git",
        "url": ""
    },
    "keywords": [],
    "author": "",
    "license": "ISC",
    "dependencies":${JSON.stringify(dependencies,null, 8)},
    "devDependencies":${JSON.stringify(devDependencies,null, 8)}
}`

}

const phpBagTemplate =  function (name, dependencies, devDependencies) {
    return `
    {
        "name": "${name}",
        "type": "project",
        "description": "The Laravel Framework.",
        "keywords": [
            "framework",
            "laravel"
        ],
        "license": "MIT",
        "require": ${JSON.stringify(dependencies,null, 8)},
        "require-dev": ${JSON.stringify(devDependencies,null, 8)},
        "config": {
            "optimize-autoloader": true,
            "preferred-install": "dist",
            "sort-packages": true
        },
        "extra": {
            "laravel": {
                "dont-discover": []
            }
        },
        "autoload": {
            "psr-4": {
                "App\\": "app/"
            },
            "classmap": [
                "database/seeds",
                "database/factories"
            ],
            "files": [
                "app/helpers.php"
            ]
        },
        "autoload-dev": {
            "psr-4": {
                "Tests\\": "tests/"
            }
        },
        "minimum-stability": "dev",
        "prefer-stable": true,
        "scripts": {
            "post-autoload-dump": [
                "Illuminate\\Foundation\\ComposerScripts::postAutoloadDump",
                "@php artisan package:discover --ansi"
            ],
            "post-root-package-install": [
                "@php -r \"file_exists('.env') || copy('.env.example', '.env');\""
            ],
            "post-create-project-cmd": [
                "@php artisan key:generate --ansi"
            ]
        }
    }    
`
}

const javaBagTemplate =  function (name, dependencies, devDependencies) {
    return `
    {
        "name": "${name}",
        "type": "project",
        "description": "The Laravel Framework.",
        "keywords": [
            "framework",
            "laravel"
        ],
        "license": "MIT",
        "require": ${JSON.stringify(dependencies,null, 8)},
        "require-dev": ${JSON.stringify(devDependencies,null, 8)},
        "config": {
            "optimize-autoloader": true,
            "preferred-install": "dist",
            "sort-packages": true
        },
        "extra": {
            "laravel": {
                "dont-discover": []
            }
        },
        "autoload": {
            "psr-4": {
                "App\\": "app/"
            },
            "classmap": [
                "database/seeds",
                "database/factories"
            ],
            "files": [
                "app/helpers.php"
            ]
        },
        "autoload-dev": {
            "psr-4": {
                "Tests\\": "tests/"
            }
        },
        "minimum-stability": "dev",
        "prefer-stable": true,
        "scripts": {
            "post-autoload-dump": [
                "Illuminate\\Foundation\\ComposerScripts::postAutoloadDump",
                "@php artisan package:discover --ansi"
            ],
            "post-root-package-install": [
                "@php -r \"file_exists('.env') || copy('.env.example', '.env');\""
            ],
            "post-create-project-cmd": [
                "@php artisan key:generate --ansi"
            ]
        }
    }    
`
}