import sys
import os


def create_javascript_file(file_name, javascript_stub):
    """

    :param file_name: path with file name relative to script
    :return:
    """
    with open(file_name, 'w') as f:
        f.write(javascript_stub)


def make_module_dirs(path, dir_names):
    """

    :param path:
    :param dir_names:
    :return:
    """
    module_dir_paths = []
    module_name = dir_names[0] + '/'
    if not os.path.exists(path + module_name):
        os.makedirs(path + module_name + '/')
        module_dir_paths.append(path + module_name + '/')

    for dir_name in dir_names[1:]:
        if not os.path.exists(path + module_name + dir_name):
            os.makedirs(path + module_name + dir_name + '/')
            module_dir_paths.append(path + module_name + dir_name + '/')

    return module_dir_paths


if __name__ == '__main__':
    """
    create directory and files for new module
    - <module_name> : <top level dir for module>
    -- <module_name>.js : empty module stub
    -- <controllers> : <dir for module controllers>
    --- <module_name>.controller.js : empty controller stub
    -- <services> : <dir for module services>
    --- <module_name>.service.js : empty controller stub

    path : relative path to directory to create new angular module structure in

    """
    if sys.argv[1] == '/':
        path = sys.argv[1]
    else:
        path = sys.argv[1] + '/'
    module_name = sys.argv[2]

    module_dir_paths = make_module_dirs(path,
                                        [module_name, 'controllers', 'services'])

    module_file_name = module_dir_paths[0] + module_name + '.module.js'
    controller_file_name = module_dir_paths[1] + module_name + '.controller.js'
    service_file_name = module_dir_paths[2] + module_name + '.service.js'

    javascript_stub = "(function(){\n  'use strict;'\n\n" \
                      "  /*Implement code*/ \n\n})();"

    create_javascript_file(module_file_name, javascript_stub)
    create_javascript_file(controller_file_name, javascript_stub)
    create_javascript_file(service_file_name, javascript_stub)
