import os
import shutil
import enum
import psutil

origin_library_path =   "R:\\Games\\Origin\\"
config_path =           "C:\\Users\\Dunestorm\\Documents\\BioWare\\"
me3_vanilla_dir =       "Mass Effect 3.vanilla"
me3_mod_dir =           "Mass Effect 3.mod"
me3_symlink_dir =       "Mass Effect 3"

print("Mass Effect 3 Switcher v0.9\n")

class ActiveGame(enum.Enum):
    Vanilla = 0
    Mod = 1

def CreateSymlink(root_path, symlink_rel_path, src_rel_path):
    full_symlink_path = os.path.join(root_path, symlink_rel_path)
    full_src_path = os.path.join(root_path, src_rel_path)

    if os.path.exists(full_symlink_path):
        os.unlink(full_symlink_path)
    os.symlink(full_src_path, full_symlink_path, True)

def MigrateSaves(full_src_path, full_dst_path):
    backup_ext = ".backup"
    _full_backup_path = full_dst_path + backup_ext

    if os.path.exists(_full_backup_path):
        shutil.rmtree(_full_backup_path)
    shutil.copytree(full_dst_path, _full_backup_path, False)

    if os.path.exists(full_dst_path):
        shutil.rmtree(full_dst_path)
    shutil.copytree(full_src_path, full_dst_path, False)

def IdentifyInstallation(path):
    active_game = ActiveGame.Vanilla

    if os.path.exists(os.path.join(path, "cmm_vanilla")):
        active_game = ActiveGame.Vanilla
    else:
        active_game = ActiveGame.Mod

    return active_game

def IsExecutableBlocking():
    result = False

    while result == False:
        for p in psutil.process_iter():
            if p.name() == "Origin.exe":
                result = True
                break
            if p.name() == "MassEffect3.exe":
                result = True
                break
        break
    return result

if IsExecutableBlocking():
    print("Please exit Origin and Mass Effect 3 before running this script!")
    input("Press the enter key to exit...")
    exit()

if os.path.islink(os.path.join(origin_library_path, me3_symlink_dir)) == False:
    CreateSymlink(origin_library_path, me3_symlink_dir, me3_vanilla_dir)
    CreateSymlink(config_path, me3_symlink_dir, me3_vanilla_dir)
    print("First time setup complete")

active_game = IdentifyInstallation(os.path.join(origin_library_path, me3_symlink_dir))

user_selection = None
if active_game == ActiveGame.Mod:
    user_selection = input("Modded game is currently active, would you like to switch to vanilla? [y/N]\n")
else:
    user_selection = input("Vanilla game is currently active, would you like to switch to modded? [y/N]\n")

if user_selection.lower() == "y":
    if active_game == ActiveGame.Mod:
        CreateSymlink(origin_library_path, me3_symlink_dir, me3_vanilla_dir)
        CreateSymlink(config_path, me3_symlink_dir, me3_vanilla_dir)
        print("Identified modded ME3 as active game, switched to vanilla")

        MigrateSaves(os.path.join(config_path, me3_mod_dir, "Save"),
                os.path.join(config_path, me3_vanilla_dir, "Save"))
        print("Save games have been migrated from modded ME3 to vanilla")
    else:
        CreateSymlink(origin_library_path, me3_symlink_dir, me3_mod_dir)
        CreateSymlink(config_path, me3_symlink_dir, me3_mod_dir)
        print("Identified vanilla ME3 as active game, switched to modded")

        MigrateSaves(os.path.join(config_path, me3_vanilla_dir, "Save"),
                os.path.join(config_path, me3_mod_dir, "Save"))
        print("Save games have been migrated from vanilla ME3 to modded")

input("Press the enter key to exit...")