"""
This module is work-in-progress and not used by IMM currently
Assumes to be called from IMM root directory
"""
from platform import machine as HardwareArchitecture
import subprocess
from sys import platform

from ..GeneralFunctions.Network import Download
from ..GeneralFunctions.InputOutput import WriteBinaryToFile
 
 # FIXME change path slash for windows
VirtualEnv="Code/Dependencies/"
VirtualEnvBinDir=VirtualEnv+"bin/"
VirtualEnvPip=VirtualEnvBinDir+"pip"

def CreateVirtualEnvrionment():
    print(
        "Creating virtual envrionment for dependecies in "+VirtualEnv+
        " sub-directory")
    subprocess.run(
        "python3", "-m", "venv", "--copies", VirtualEnv)

def InstallPythonPackage(PackageName):
    print("Installing "+PackageName+" from Python Package Index (PyPI)")
    subprocess.run(VirtualEnvPip, "install ", PackageName, "--upgrade")

def SetupFFmpeg():
    # check if FFmpeg already exists
    OperatingSystem = GetOperatingSystemName()
    # check OS & Architecture and choose appropriate url & extraction command, alternative function name as for GNU/Linux, Windows (MacOS also?)
    # For now download URLs are static for WIndows release & bumped periodically
    # GNU/Linux - https://johnvansickle.com/ffmpeg/
    # MacOS - https://evermeet.cx/ffmpeg/ , alternatively https://ffmpeg.zeranoe.com/builds/macos64/static/
    # Widnows - https://ffmpeg.zeranoe.com/builds/win64/static/ & https://ffmpeg.zeranoe.com/builds/win32/static/?C=M&O=D
    Architecture = HardwareArchitecture()
    CannotInstallFFmpeg = (
        "Do not have a recipie for automatically fetching ffmpeg "
        "for "+Architecture+" architecture on "+OperatingSystem+
        " operating system. Please install manually, else IMM "
        "will run without it with limited capabilities.")
    if len(Architecture) == 0:
        print(
            "Could not determine system architecture. IMM will work "
            "without ffmpeg")
        return False
    if OperatingSystem == "GNU/Linux":
        if Architecture == "x86_64":
                DownloadParameter = "amd64"
        elif Architecture == "i386" or Architecture == "i686" :
                DownloadParameter = "i686"
        elif Architecture == "armv71" or Architecture == "armhf":
                DownloadParameter = "armhf"
        elif Architecture == "armel" :
                DownloadParameter = "armel"
        elif (
            Architecture == "aarch64_be" or
            Architecture == "aarch64" or
            Architecture == "armv8b" or
            Architecture == "armv8l" or
            Architecture == "arm64"):
                DownloadParameter = "arm64"
        else:
            print(CannotInstallFFmpeg)
        DownloadURL = (
            "https://johnvansickle.com/ffmpeg/releases/ffmpeg-release-"
            +DownloadParameter+"-static.tar.xz")
        print("Downloading ffmpeg from "+DownloadURL)
        Data = Download(DownloadURL)
        WriteBinaryToFile(VirtualEnv+"ffmpeg.tar.xz", Data)
        print(
            "Extracting file than moving ffmpeg binaries to virtual "
            "envrionment bin directory and finally cleaning up")
        subprocess.run(
            "tar xvf ffmpeg.tar.xz && mv ffmpeg-*/ff* ./bin/ && rm -R ffmpeg*" ,
            shell=True,
            stdout=subprocess.PIPE,
            cwd=VirtualEnv)
    else:
        print(CannotInstallFFmpeg)

def GetOperatingSystemName():
    if platform == "linux":
        return "GNU/Linux"
    elif platform == "win32":
        return "Windows"
    elif platform == "darwin":
        return "Mac OS"
    else:
        return platform

def Setup():
    CreateVirtualEnvrionment()
    InstallPythonPackage("youtube-dl")
    InstallPythonPackage("pyperclip")
    SetupFFmpeg()
    
SetupFFmpeg()
