#!/usr/bin/python3
# coding=utf-8

import os
import argparse

from lib.gerrit import Gerrit
from lib.gitlab import Gitlab
from lib.github import Github


def new_project(proj_name, proj_type, git_target=None):

    all_target = True
    if git_target:
        all_target = False
        git_targets = git_target.split(',')

    if all_target or 'gerrit' in git_targets:
        g = Gerrit()
        print('\n--- gerrit ---')
        code, msg = g.create_project(proj_name, proj_type)

        if not str(code).startswith('2'):
            print(msg)
            print('E: failed')
        else:
            print('finish gerrit')

    if (all_target or 'github' in git_targets) and proj_type == 'public':
        print('\n--- github ---')
        gh = Github()
        code, msg = gh.create_project(proj_name)

        if not str(code).startswith('2'):
            print("++++")
            print(msg)
            print('E: failed')
        else:
            print("++++")
            print(msg)
            print('finish github')

    if all_target or 'gitlab' in git_targets:
        print('\n--- gitlab ---')
        gl = Gitlab()
        code, msg = gl.create_project(proj_name)

        if not str(code).startswith('2'):
            print(msg)
            print('E: failed')
        else:
            print('finish gitlab')


if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('name', help='project name')
    parser.add_argument('-t', '--type', help='project type [public | private | web]')
    parser.add_argument('-g', '--git', help='choose one or multi target, [gerrit | github | gitlab], eg: github,gitlab')
    args = parser.parse_args()
    proj_name = args.name
    proj_type = args.type
    git = args.git

    new_project(proj_name, proj_type, git)
