#!python3
# coding=utf-8

import os
import time

_VERSION_ = "0.0.1"
_PACKAGE_ = [
    {
        "name": "fire",
        "package": "fire",
        "version": "0.4.0",
    }
]

class X_Git(object):

    def version(self):
        """show version"""
        return _VERSION_

    def update(self):
        """
        update script x
        """
        download = "sudo wget -P /tmp http://resource.rocke.top/script/x"
        chmod = "sudo chmod 777 /tmp/x"
        mv = "sudo mv /tmp/x /usr/local/bin/"
        return run(download, chmod, mv)

    def remote(self, projectName):
        """
        git remote repositorie initialization
        param: projectName
        """
        origin = 'git remote remove origin'
        github = f'git remote add github https://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@github.com/louisyoungx/{projectName}.git'
        gitee = f'git remote add gitee https://gitee.com/louisyoungx/{projectName}.git'
        return run(origin, github, gitee)

    def r(self, projectName):
        """git remote (abbr)"""
        self.remote(projectName)

    def push(self, option=None):
        """git push to github & gitee / git push tags"""
        if option != "tag":
            github = f'git push github'
            gitee = f'git push gitee'
        else:
            github = f'git push github --tags'
            gitee = f'git push gitee --tags'
        return run(github, gitee)
    
    def p(self, option=None):
        """git push (abbr)"""
        self.push(option=None)

    def add(self):
        """git add"""
        Add = "git add ."
        run(Add)
    
    def a(self):
        """git add (abbr)"""
        self.add()

    def commit(self):
        """git commit"""
        print(backStyle_1("Type(Scope): Subject"))
        typeStandard = {
            "feat": "新功能（feature）",
            "fix/to": "修复bug，可以是QA发现的BUG，也可以是研发自己发现的BUG",
            "fix": "产生diff并自动修复此问题适合于一次提交直接修复问题",
            "to": "只产生diff不自动修复此问题适合于多次提交最终修复问题提交时使用fix",
            "docs": "文档（documentation）",
            "style": "格式（不影响代码运行的变动）",
            "refactor": "重构（即不是新增功能，也不是修改bug的代码变动）",
            "perf": "优化相关，比如提升性能、体验",
            "test": "增加测试",
            "chore": "构建过程或辅助工具的变动",
            "revert": "回滚到上一个版本",
            "merge": "代码合并",
            "sync": "同步主线或分支的Bug"
        }

        for key in typeStandard:
            print(f'{frontStyle_4(key)}: {frontStyle_2(typeStandard[key])}')
        for i in range(3):
            Type = input(frontStyle_5("Type: "))
            if Type in typeStandard:
                break
            else:
                print(backStyle_1("Invalid Type, Please retry"))
        else:
            print(backStyle_1("Invalid Type, Quit"))
            return
        print(backStyle_1("Scope用于说明 commit 影响的范围"))
        Scope = input(frontStyle_5("Scope: "))
        print(backStyle_1("Subject是commit目的的简短描述（小于50个字）"))
        Subject = input(frontStyle_5("Subject: "))
        print(f'{Type}({Scope}): {Subject}')
        Commit = f'git commit -m "{Type}({Scope}): {Subject}"'
        run(Commit)
        
    def c(self):
        """git commit (abbr)"""
        self.commit()

    def proxy(self):
        """proxy for shell"""
        env = "export https_proxy=http://127.0.0.1:7890 http_proxy=http://127.0.0.1:7890 all_proxy=socks5://127.0.0.1:7890"
        return run(env)

    def pypi(self):
        testpypi = 'pypi-AgENdGVzdC5weXBpLm9yZwIkNDIwMjVkYzgtMmExMC00MmZmLTg1YzEtMTMzZjRkM2RjNTEzAAIleyJwZXJtaXNzaW9ucyI6ICJ1c2VyIiwgInZlcnNpb24iOiAxfQAABiB2UGmVOVrU5PIROvDfw3bnhQ5Ya3ZodD9CFn8DMvTPqQ'

    def pac(self):
        """proxy & add & commit"""
        self.proxy()
        self.add()
        self.commit()

def run(*commands):
    startTime = time.time()
    for command in commands:
        os.system(command)
    useTime = time.time() - startTime
    # 'complete in {:.2f}s'.format(useTime)
    return frontStyle_6('complete in ') + frontStyle_2(' {:.2f}s'.format(useTime))

"""打印前景色"""
def frontStyle_0(word):
    return f'\033[30m{word}\033[0m'
def frontStyle_1(word):
    return f'\033[31m{word}\033[0m'
def frontStyle_2(word):
    return f'\033[32m{word}\033[0m'
def frontStyle_3(word):
    return f'\033[33m{word}\033[0m'
def frontStyle_4(word):
    return f'\033[34m{word}\033[0m'
def frontStyle_5(word):
    return f'\033[35m{word}\033[0m'
def frontStyle_6(word):
    return f'\033[36m{word}\033[0m'
def frontStyle_7(word):
    return f'\033[37m{word}\033[0m'

"""打印背景色"""
def backStyle_0(word):
    return f'\033[40m{word}\033[0m'
def backStyle_1(word):
    return f'\033[41m{word}\033[0m'
def backStyle_2(word):
    return f'\033[42m{word}\033[0m'
def backStyle_3(word):
    return f'\033[43m{word}\033[0m'
def backStyle_4(word):
    return f'\033[44m{word}\033[0m'
def backStyle_5(word):
    return f'\033[45m{word}\033[0m'
def backStyle_6(word):
    return f'\033[46m{word}\033[0m'
def backStyle_7(word):
    return f'\033[47m{word}\033[0m'

"""打印显示方式"""
def displayStyle_0(word):
    return f'\033[0m{word}\033[0m'
def displayStyle_1(word):
    return f'\033[1m{word}\033[0m'
def displayStyle_2(word):
    return f'\033[4m{word}\033[0m'
def displayStyle_3(word):
    return f'\033[5m{word}\033[0m'
def displayStyle_4(word):
    return f'\033[7m{word}\033[0m'
def displayStyle_5(word):
    return f'\033[8m{word}\033[0m'

"""综合打印"""
def mixStyle(word):
    return f'\033[5;31;47m综合打印\033[0m'

"""色卡"""
def colorBar():
    content = "     "
    bar = backStyle_1(content) + backStyle_2(content) + backStyle_3(content) + backStyle_4(content) + backStyle_5(content) + backStyle_6(content) + backStyle_7(content)
    return bar


if __name__ == '__main__':

    "动态加载依赖包"
    from pip._internal import main as pip_main
    import importlib

    def install(package):
        pip_main(['install', package])

    createVar = locals()

    for line in _PACKAGE_:
        try:
            createVar[line["name"]] = importlib.import_module(line["name"])
        except:
            install(f'{line["package"]}=={line["version"]}')
            createVar[line["name"]] = importlib.import_module(line["name"])

    xg = X_Git()
    fire.Fire(xg)
