#!python3
# coding=utf-8

import os
import time

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

class X_Command(object):

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

    def update(self):
        """
        update script x
        """
        download = "sudo curl -k -o /tmp/x https://src.rocke.top/script/x"
        chmod = "sudo chmod 777 /tmp/x"
        mv = "sudo mv /tmp/x /usr/local/bin/"
        return run(download, chmod, mv)

    
    def install(self, script):
        """
        install script
        [x, t, xg, clash, vim-plug, rgb, vimrc]
        """
        if (script == 'x'):
            """x tool"""
            command = "sudo curl -o /tmp/x https://src.rocke.top/script/x ; sudo chmod 777 /tmp/x ; sudo mv /tmp/x /usr/local/bin/ ; python3 -m pip install fire"
            print(frontStyle_6(command))

        elif (script == 't'):
            """t tool"""
            update_x = self.update()
            download = "sudo curl -o /tmp/t https://src.rocke.top/script/t"
            chmod = "sudo chmod 777 /tmp/t"
            mv = "sudo mv /tmp/t /usr/local/bin/"
            install_requests = "python3 -m pip install requests"
            install_fire = "python3 -m pip install fire"
            install_fire = "python3 -m pip install openai"
            return run(update_x, download, chmod, mv, install_requests, install_fire)

        elif (script == 'z'):
            """z tool"""
            update_x = self.update()
            download = "sudo curl -o /tmp/z https://src.rocke.top/script/z"
            chmod = "sudo chmod 777 /tmp/z"
            mv = "sudo mv /tmp/z /usr/local/bin/"
            install_requests = "python3 -m pip install requests"
            install_fire = "python3 -m pip install fire"
            install_fire = "python3 -m pip install openai"
            return run(update_x, download, chmod, mv, install_requests, install_fire)

        elif (script == 'c'):
            """c tool"""
            update_x = self.update()
            download = "sudo curl -o /tmp/c https://src.rocke.top/script/c"
            chmod = "sudo chmod 777 /tmp/c"
            mv = "sudo mv /tmp/c /usr/local/bin/"
            install_requests = "python3 -m pip install requests"
            install_fire = "python3 -m pip install fire"
            install_fire = "python3 -m pip install openai"
            install_fire = "python3 -m pip install rich"
            return run(update_x, download, chmod, mv, install_requests, install_fire)

        elif (script == 'pr'):
            """pr tool"""
            update_x = self.update()
            download = "sudo curl -o /tmp/pr https://src.rocke.top/script/pr"
            chmod = "sudo chmod 777 /tmp/pr"
            mv = "sudo mv /tmp/pr /usr/local/bin/"
            install_requests = "python3 -m pip install requests"
            install_fire = "python3 -m pip install fire"
            install_fire = "python3 -m pip install openai"
            return run(update_x, download, chmod, mv, install_requests, install_fire)
            
        elif (script == 'xg'):
            """x git tool"""
            download = "sudo curl -o /tmp/xg https://src.rocke.top/script/xg"
            chmod = "sudo chmod 777 /tmp/xg"
            mv = "sudo mv /tmp/xg /usr/local/bin/"
            return run(download, chmod, mv)
            
        elif (script == 'clash'):
            """shellclash"""
            cmd = "export url='https://cdn.jsdelivr.net/gh/juewuy/ShellClash@master' && curl -sSk -o /tmp/install.sh $url/install.sh  && sh /tmp/install.sh && source /etc/profile &> /dev/null"
            env = "source /etc/profile"
            return run(cmd, env)

        elif (script == 'vim-plug'):
            """vim plugin"""
            install_vim_plug = 'curl -fLo ~/.vim/autoload/plug.vim --create-dirs \
    https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim'
            return run(install_vim_plug)

        elif (script == "rgb"):
            """rgb tool transform rgb from decimalism to hexadecimal"""
            download = "sudo curl -o /tmp/rgb https://src.rocke.top/script/rgb"
            chmod = "sudo chmod 777 /tmp/rgb"
            mv = "sudo mv /tmp/rgb /usr/local/bin/"
            return run(download, chmod, mv)

        elif (script == "vimrc"):
            """vimrc - vim config"""
            download = "sudo curl -o /tmp/vimrc https://src.rocke.top/script/vimrc"
            chmod = "sudo chmod 777 /tmp/vimrc"
            mkdirUser = "mkdir ~/.vim"
            mkdirRoot = "mkdir /root/.vim"
            cpUser = "sudo cp /tmp/vimrc ~/.vim/vimrc"
            cpRoot = "sudo cp /tmp/vimrc /root/.vim/vimrc"
            rm = "sudo rm /tmp/vimrc"
            return run(download, chmod, mkdirUser, mkdirRoot, cpUser, cpRoot, rm)
        

        elif (script == "zpy"):
            """zpy chinese python"""
            download = "sudo curl -o /tmp/zpy https://src.rocke.top/script/zpy"
            chmod = "sudo chmod 777 /tmp/zpy"
            mv = "sudo mv /tmp/zpy /usr/local/bin/"
            return run(download, chmod, mv)

        else:
            return "No Script: " + script
    
    def uninstall(self, script):
        """
        uninstall script
        [x, t, rgb, zpy]
        """
        if (script == 'x'):
            """x tool"""
            rm = "sudo rm /usr/local/bin/x"
            return run(rm)

        elif (script == 't'):
            """t tool"""
            rm = "sudo rm /usr/local/bin/t"
            return run(rm)

        elif (script == 'xg'):
            """x git tool"""
            rm = "sudo rm /usr/local/bin/xg"
            return run(rm)

        elif (script == 'rgb'):
            """rgb tool transform rgb from decimalism to hexadecimal"""
            rm = "sudo rm /usr/local/bin/rgb"
            return run(rm)
        
        elif (script == 'zpy'):
            """zpy chinese python"""
            rm = "sudo rm /usr/local/bin/zpy"
            return run(rm)

        else:
            return "No Script: " + script        

    def homebrew(self):
        """HomeBrew Permission Dened"""
        return run('sudo chown -R $(whoami) $(brew --prefix)/*')

    def proxy(self):
        """wsl proxy clash"""
        p = r"""
export hostip=$(cat /etc/resolv.conf |grep -oP '(?<=nameserver\ ).*')
export https_proxy="http://${hostip}:7890"
export http_proxy="http://${hostip}:7890"
        """
        return run(p)

    def unproxy(self):
        """wsl unproxy clash"""
        p = r"""
unset https_proxy"
unset http_proxy"
        """
        return run(p)

    def mysql(self, status):
        """
        MySQL action
        param: start / stop / restart
        """
        return run(f'sudo /usr/local/mysql/support-files/mysql.server {status}')

    def port(self, port):
        """
        port being used
        param: port-number
        """
        return run(f"sudo netstat -tunlp|grep {port}")

    def minecraft(self):
        """Minecraft proxy"""
        return run(f"/Users/ericaaron/Developer/Tools-Library/phantom-macos -bind 0.0.0.0 -bind_port 19132 -server www.louisyoung.site:19132")

    def mc(self):
        """Minecraft proxy (abbr)"""
        self.minecraft()

    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 unproxy(self):
        """unset proxy for shell"""
        env = "unset https_proxy http_proxy all_proxy"
        return run(env)

    def ssl(self):
        """update ssl certs"""
        cmd = "sudo certbot --nginx"
        return run(cmd)

    def color(self):
        """show system color"""
        bar = colorBar()
        print(bar)
        print(frontStyle_0('frontStyle_0()'))
        print(frontStyle_1('frontStyle_1()'))
        print(frontStyle_2('frontStyle_2()'))
        print(frontStyle_3('frontStyle_3()'))
        print(frontStyle_4('frontStyle_4()'))
        print(frontStyle_5('frontStyle_5()'))
        print(frontStyle_6('frontStyle_6()'))
        print(frontStyle_7('frontStyle_7()'))
        print(backStyle_0('backStyle_0()'))
        print(backStyle_1('backStyle_1()'))
        print(backStyle_2('backStyle_2()'))
        print(backStyle_3('backStyle_3()'))
        print(backStyle_4('backStyle_4()'))
        print(backStyle_5('backStyle_5()'))
        print(backStyle_6('backStyle_6()'))
        print(backStyle_7('backStyle_7()'))
        print(displayStyle_0('displayStyle_0()'))
        print(displayStyle_1('displayStyle_1()'))
        print(displayStyle_2('displayStyle_2()'))
        print(displayStyle_3('displayStyle_3()'))
        print(displayStyle_4('displayStyle_4()'))
        print(displayStyle_5('displayStyle_5()'))
        print(mixStyle('mixStyle()'))
        print(bar)

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"])

    x = X_Command()
    fire.Fire(x)
