def add_language(args):
    json_pth = project.get_project_dir(os.getcwd(), ".mcrsrctools/metadata.json")
    json_dta = json.load(open(json_pth))

    real_path = project.get_project_dir(os.getcwd(), "assets/minecraft/lang")

    if not os.path.exists(os.path.join(real_path, args.file)):
        if input_.get_yn(Fore.RED + "Could not find specified lang file. Continue? " + Fore.CYAN) is False:
            return

    if args.remove:
        if args.file in json_dta["langs"]:
            del json_dta["langs"][args.file]
            print Fore.GREEN + "Deleted language " + Fore.WHITE + " " + args.file + Fore.GREEN + " successfully!"
        else:
            print Fore.RED + "Language does not exist"
    else:
        if args.file in json_dta["langs"]:
            print Fore.GREEN + "Collecting required info to modify language " + Fore.WHITE + " " + args.file
            name = args.name if args.name else raw_input(Fore.LIGHTBLUE_EX + "\tFull name of language: " + Fore.CYAN)
            region = args.region if args.region else raw_input(Fore.LIGHTBLUE_EX + "\tRegion of language: " + Fore.CYAN)
            rtl = args.rtl
            json_dta["langs"][args.file] = {"region": region, "rtl": rtl, "name": name}
            print Fore.GREEN + "Modified language " + Fore.WHITE + " " + args.file + Fore.GREEN + " successfully!"
        else:
            print Fore.GREEN + "Collecting required info to add language " + Fore.WHITE + " " + args.file
            name = args.name if args.name else raw_input(Fore.LIGHTBLUE_EX + "\tFull name of language: " + Fore.CYAN)
            region = args.region if args.region else raw_input(Fore.LIGHTBLUE_EX + "\tRegion of language: " + Fore.CYAN)
            rtl = args.rtl
            json_dta["langs"][args.file] = {"region": region, "rtl": rtl, "name": name}
            print Fore.GREEN + "Added language " + Fore.WHITE + " " + args.file + Fore.GREEN + " successfully!"
    json.dump(json_dta, open(json_pth, "w"))
def refresh_sound(file_):
    choice = input_.options_retnum(
            Fore.LIGHTBLUE_EX + "What do you want to do with " + Fore.WHITE + " " + file_ + "? " + Fore.CYAN, [
                "Create a new sound group and add the file to it",
                "Add the file to an existing sound group",
                "Ignore it completely (you will not be able to use it in-game)"
            ])
    if choice == 0:
        group_name = raw_input(Fore.LIGHTBLUE_EX + "What shall I call the new sound group? " + Fore.CYAN)
        add_sound_group(argparse.Namespace(category="", group=group_name, remove=False))
    if choice == 1:
        group_name = raw_input(Fore.LIGHTBLUE_EX + "To which group should I add this sound? " + Fore.CYAN)
    if choice in (0, 1):
        print Fore.LIGHTBLUE_EX + "Leave any option blank for default"
        weight = input_.defaulted_in(Fore.LIGHTBLUE_EX + "Sound weight? " + Fore.CYAN, 1.0, float)
        pitch = input_.defaulted_in(Fore.LIGHTBLUE_EX + "Sound pitch? " + Fore.CYAN, 1.0, float)
        volume = input_.defaulted_in(Fore.LIGHTBLUE_EX + "Sound volume? " + Fore.CYAN, 1.0, float)
        type = input_.options(Fore.LIGHTBLUE_EX + "Sound type? (use 0 for default)", ["sound", "event"])
        stream = input_.get_yn(Fore.LIGHTBLUE_EX + "Stream sound (default is N) ")
        add_to_sound_group(
                argparse.Namespace(group=group_name, weight=weight, pitch=pitch, volume=volume, type=type,
                                   stream=stream,
                                   sound=file_, remove=False))
    elif choice == 2:
        return False
    return True
def refresh_file(file_):
    if input_.get_yn(
            Fore.LIGHTBLUE_EX + "Do you want to add " + Fore.WHITE + " " +
            file_ + Fore.LIGHTBLUE_EX +
            " to the list of languages (It will not be usable in-game unless you do so) "
            + Fore.CYAN):
        add_language(
            argparse.Namespace(remove=False,
                               file=file_,
                               name=None,
                               region=None,
                               rtl=False))
        return True
    return False
def add_language(args):
    json_pth = project.get_project_dir(os.getcwd(),
                                       ".mcrsrctools/metadata.json")
    json_dta = json.load(open(json_pth))

    real_path = project.get_project_dir(os.getcwd(), "assets/minecraft/lang")

    if not os.path.exists(os.path.join(real_path, args.file)):
        if input_.get_yn(Fore.RED +
                         "Could not find specified lang file. Continue? " +
                         Fore.CYAN) is False:
            return

    if args.remove:
        if args.file in json_dta["langs"]:
            del json_dta["langs"][args.file]
            print Fore.GREEN + "Deleted language " + Fore.WHITE + " " + args.file + Fore.GREEN + " successfully!"
        else:
            print Fore.RED + "Language does not exist"
    else:
        if args.file in json_dta["langs"]:
            print Fore.GREEN + "Collecting required info to modify language " + Fore.WHITE + " " + args.file
            name = args.name if args.name else raw_input(
                Fore.LIGHTBLUE_EX + "\tFull name of language: " + Fore.CYAN)
            region = args.region if args.region else raw_input(
                Fore.LIGHTBLUE_EX + "\tRegion of language: " + Fore.CYAN)
            rtl = args.rtl
            json_dta["langs"][args.file] = {
                "region": region,
                "rtl": rtl,
                "name": name
            }
            print Fore.GREEN + "Modified language " + Fore.WHITE + " " + args.file + Fore.GREEN + " successfully!"
        else:
            print Fore.GREEN + "Collecting required info to add language " + Fore.WHITE + " " + args.file
            name = args.name if args.name else raw_input(
                Fore.LIGHTBLUE_EX + "\tFull name of language: " + Fore.CYAN)
            region = args.region if args.region else raw_input(
                Fore.LIGHTBLUE_EX + "\tRegion of language: " + Fore.CYAN)
            rtl = args.rtl
            json_dta["langs"][args.file] = {
                "region": region,
                "rtl": rtl,
                "name": name
            }
            print Fore.GREEN + "Added language " + Fore.WHITE + " " + args.file + Fore.GREEN + " successfully!"
    json.dump(json_dta, open(json_pth, "w"))
def refresh_sound(file_):
    choice = input_.options_retnum(
        Fore.LIGHTBLUE_EX + "What do you want to do with " + Fore.WHITE + " " +
        file_ + "? " + Fore.CYAN, [
            "Create a new sound group and add the file to it",
            "Add the file to an existing sound group",
            "Ignore it completely (you will not be able to use it in-game)"
        ])
    if choice == 0:
        group_name = raw_input(Fore.LIGHTBLUE_EX +
                               "What shall I call the new sound group? " +
                               Fore.CYAN)
        add_sound_group(
            argparse.Namespace(category="", group=group_name, remove=False))
    if choice == 1:
        group_name = raw_input(Fore.LIGHTBLUE_EX +
                               "To which group should I add this sound? " +
                               Fore.CYAN)
    if choice in (0, 1):
        print Fore.LIGHTBLUE_EX + "Leave any option blank for default"
        weight = input_.defaulted_in(
            Fore.LIGHTBLUE_EX + "Sound weight? " + Fore.CYAN, 1.0, float)
        pitch = input_.defaulted_in(
            Fore.LIGHTBLUE_EX + "Sound pitch? " + Fore.CYAN, 1.0, float)
        volume = input_.defaulted_in(
            Fore.LIGHTBLUE_EX + "Sound volume? " + Fore.CYAN, 1.0, float)
        type = input_.options(
            Fore.LIGHTBLUE_EX + "Sound type? (use 0 for default)",
            ["sound", "event"])
        stream = input_.get_yn(Fore.LIGHTBLUE_EX +
                               "Stream sound (default is N) ")
        add_to_sound_group(
            argparse.Namespace(group=group_name,
                               weight=weight,
                               pitch=pitch,
                               volume=volume,
                               type=type,
                               stream=stream,
                               sound=file_,
                               remove=False))
    elif choice == 2:
        return False
    return True
def add_to_sound_group(args):
    if project.get_project_dir(os.getcwd()):
        proj_json = json.load(
            open(
                os.path.join(project.get_project_dir(os.getcwd()),
                             "assets/minecraft/sounds.json")))
        if args.group not in proj_json:
            print Fore.RED + "Sound group does not exist! " + Fore.CYAN + "(maybe you wanted mcrsrctool addsoundgroup?)"
        else:
            if not os.path.exists(
                    os.path.join(
                        os.path.join(project.get_project_dir(os.getcwd()),
                                     "assets/minecraft/sounds"), args.sound)):
                if not input_.get_yn(
                        Fore.RED +
                        "I couldn't find the specified sound. Continue anyways? [Y or N] "
                        + Fore.CYAN):
                    return
            args.sound = os.path.splitext(args.sound)[0]
            group = proj_json[args.group]
            new_s = []

            for entry in group["sounds"]:
                # print type(entry)
                if type(entry) == unicode:
                    if entry == args.sound:
                        ##        print "conting"
                        continue
                else:
                    if entry["name"] == args.sound:
                        ##          print "conting"
                        continue
                new_s.append(entry)
            ##print new_s
            data = {"name": args.sound}
            us = True
            if args.type == "event":
                data["type"] = "event"
                us = False
            if args.volume != 1.0:
                data["volume"] = args.volume
                us = False
            if args.pitch != 1.0:
                data["pitch"] = args.pitch
                us = False
            if args.weight != 1.0:
                data["weight"] = args.weight
                us = False
            if args.stream:
                data["stream"] = True
                us = False
            if us:
                data = args.sound
            if not args.remove: new_s.append(data)
            proj_json[args.group]["sounds"] = new_s
            json.dump(proj_json,
                      open(
                          os.path.join(project.get_project_dir(os.getcwd()),
                                       "assets/minecraft/sounds.json"), "w"),
                      indent=4)
            if not args.remove:
                print Fore.GREEN + "Added sound " + Fore.WHITE + args.sound + Fore.GREEN + " to group " + Fore.WHITE \
                      + args.group + Fore.GREEN + " successfully!"
            else:
                print Fore.GREEN + "Removed sound " + Fore.WHITE + args.sound + Fore.GREEN + " from group " + Fore.WHITE \
                      + args.group + Fore.GREEN + " successfully!"

    else:
        print Fore.RED + "Could not find project"
def refresh_file(file_):
    if input_.get_yn(Fore.LIGHTBLUE_EX + "Do you want to add " + Fore.WHITE + " " + file_ + Fore.LIGHTBLUE_EX + " to the list of languages (It will not be usable in-game unless you do so) " + Fore.CYAN):
        add_language(argparse.Namespace(remove=False, file=file_, name=None, region=None, rtl=False))
        return True
    return False
def create_project(args):
    dir_ = args.path
    print Fore.GREEN + "Creating new project in", Fore.WHITE + dir_
    path_to_info_dir = os.path.join(dir_, ".mcrsrctools")
    os.makedirs(path_to_info_dir)
    if os.name == "nt":
        ctypes.windll.kernel32.SetFileAttributesW(unicode(os.path.abspath(path_to_info_dir)), 0x02)
    print Fore.GREEN + "Collecting info for resource pack"
    print Fore.LIGHTBLUE_EX + "\tPack name:" + Fore.CYAN,
    name = raw_input()
    print Fore.LIGHTBLUE_EX + "\tPack description:" + Fore.CYAN,
    desc = raw_input()
    mc_versions = download.get_mc_versions()
    print Fore.LIGHTBLUE_EX + "\tMC version options:\n"
    print Fore.RED + "[0]", Fore.LIGHTBLUE_EX + "Use latest release\n"
    print Fore.RED + "[1]", Fore.LIGHTBLUE_EX + "Use latest snapshot\n"
    print Fore.RED + "[2]", Fore.LIGHTBLUE_EX + "Let me pick from the list\n"
    pick = -1
    while pick == -1:
        try:
            print Fore.LIGHTBLUE_EX + "Please select an option: " + Fore.CYAN,
            pick = int(raw_input())
            if pick < 0 or pick > 2:
                print Fore.RED + "Invalid option"
                pick = -1
        except:
            pick = -1
            print Fore.RED + "Not a number"
    version = None
    if pick == 0:
        version = mc_versions[download.get_latest_mc_version("release")]
    elif pick == 1:
        version = mc_versions[download.get_latest_mc_version("snapshot")]
    else:
        print Fore.LIGHTBLUE_EX + "Available versions:"
        for i in mc_versions.keys():
            print Fore.WHITE + i
        vv = None
        while vv is None:
            print Fore.LIGHTBLUE_EX + "Version: " + Fore.CYAN,
            v = raw_input()
            if v in mc_versions:
                vv = mc_versions[v]
            else:
                print Fore.RED + "Invalid version"
        version = vv
    i_ = ""
    while i_ == "":
        print Fore.LIGHTBLUE_EX + "Do you want to download default minecraft assets (you need to do this once before you pack your resource pack) [Y or N]:" + Fore.CYAN,
        a = raw_input().lower()
        if a not in ["y", "n"]:
            continue
        i_ = a

    assetsdownloaded = i_ == "y"
    if i_ == "y":
        kr = get_yn(Fore.LIGHTBLUE_EX + "Do you need realms assets [Y or N]: " + Fore.CYAN)
        ki = get_yn(Fore.LIGHTBLUE_EX + "Do you want icons [Y or N]: " + Fore.CYAN)
        version.get_all(dir_, os.path.join(path_to_info_dir, "default_files"))
        version.filter_unused(dir_, os.path.join(path_to_info_dir, "default_files"), kr, ki)
    store = {
        "name": name,
        "desc": desc,
        "mc_version": version.ver,
        "assets_grabbed": assetsdownloaded,
        "has_realms": kr,
        "has_icons": ki,
        "langs": {}
    }
    json.dump(store, open(os.path.join(path_to_info_dir, "metadata.json"), "w"))
def add_to_sound_group(args):
    if project.get_project_dir(os.getcwd()):
        proj_json = json.load(open(os.path.join(project.get_project_dir(os.getcwd()), "assets/minecraft/sounds.json")))
        if args.group not in proj_json:
            print Fore.RED + "Sound group does not exist! " + Fore.CYAN + "(maybe you wanted mcrsrctool addsoundgroup?)"
        else:
            if not os.path.exists(
                    os.path.join(os.path.join(project.get_project_dir(os.getcwd()), "assets/minecraft/sounds"),
                                 args.sound)):
                if not input_.get_yn(
                                        Fore.RED + "I couldn't find the specified sound. Continue anyways? [Y or N] " + Fore.CYAN):
                    return
            args.sound = os.path.splitext(args.sound)[0]
            group = proj_json[args.group]
            new_s = []

            for entry in group["sounds"]:
                # print type(entry)
                if type(entry) == unicode:
                    if entry == args.sound:
                        ##        print "conting"
                        continue
                else:
                    if entry["name"] == args.sound:
                        ##          print "conting"
                        continue
                new_s.append(entry)
            ##print new_s
            data = {"name": args.sound}
            us = True
            if args.type == "event":
                data["type"] = "event"
                us = False
            if args.volume != 1.0:
                data["volume"] = args.volume
                us = False
            if args.pitch != 1.0:
                data["pitch"] = args.pitch
                us = False
            if args.weight != 1.0:
                data["weight"] = args.weight
                us = False
            if args.stream:
                data["stream"] = True
                us = False
            if us:
                data = args.sound
            if not args.remove: new_s.append(data)
            proj_json[args.group]["sounds"] = new_s
            json.dump(proj_json,
                      open(os.path.join(project.get_project_dir(os.getcwd()), "assets/minecraft/sounds.json"), "w"),
                      indent=4)
            if not args.remove:
                print Fore.GREEN + "Added sound " + Fore.WHITE + args.sound + Fore.GREEN + " to group " + Fore.WHITE \
                      + args.group + Fore.GREEN + " successfully!"
            else:
                print Fore.GREEN + "Removed sound " + Fore.WHITE + args.sound + Fore.GREEN + " from group " + Fore.WHITE \
                      + args.group + Fore.GREEN + " successfully!"

    else:
        print Fore.RED + "Could not find project"
def create_project(args):
    dir_ = args.path
    print Fore.GREEN + "Creating new project in", Fore.WHITE + dir_
    path_to_info_dir = os.path.join(dir_, ".mcrsrctools")
    os.makedirs(path_to_info_dir)
    if os.name == "nt":
        ctypes.windll.kernel32.SetFileAttributesW(
            unicode(os.path.abspath(path_to_info_dir)), 0x02)
    print Fore.GREEN + "Collecting info for resource pack"
    print Fore.LIGHTBLUE_EX + "\tPack name:" + Fore.CYAN,
    name = raw_input()
    print Fore.LIGHTBLUE_EX + "\tPack description:" + Fore.CYAN,
    desc = raw_input()
    mc_versions = download.get_mc_versions()
    print Fore.LIGHTBLUE_EX + "\tMC version options:\n"
    print Fore.RED + "[0]", Fore.LIGHTBLUE_EX + "Use latest release\n"
    print Fore.RED + "[1]", Fore.LIGHTBLUE_EX + "Use latest snapshot\n"
    print Fore.RED + "[2]", Fore.LIGHTBLUE_EX + "Let me pick from the list\n"
    pick = -1
    while pick == -1:
        try:
            print Fore.LIGHTBLUE_EX + "Please select an option: " + Fore.CYAN,
            pick = int(raw_input())
            if pick < 0 or pick > 2:
                print Fore.RED + "Invalid option"
                pick = -1
        except:
            pick = -1
            print Fore.RED + "Not a number"
    version = None
    if pick == 0:
        version = mc_versions[download.get_latest_mc_version("release")]
    elif pick == 1:
        version = mc_versions[download.get_latest_mc_version("snapshot")]
    else:
        print Fore.LIGHTBLUE_EX + "Available versions:"
        for i in mc_versions.keys():
            print Fore.WHITE + i
        vv = None
        while vv is None:
            print Fore.LIGHTBLUE_EX + "Version: " + Fore.CYAN,
            v = raw_input()
            if v in mc_versions:
                vv = mc_versions[v]
            else:
                print Fore.RED + "Invalid version"
        version = vv
    i_ = ""
    while i_ == "":
        print Fore.LIGHTBLUE_EX + "Do you want to download default minecraft assets (you need to do this once before you pack your resource pack) [Y or N]:" + Fore.CYAN,
        a = raw_input().lower()
        if a not in ["y", "n"]:
            continue
        i_ = a

    assetsdownloaded = i_ == "y"
    if i_ == "y":
        kr = get_yn(Fore.LIGHTBLUE_EX +
                    "Do you need realms assets [Y or N]: " + Fore.CYAN)
        ki = get_yn(Fore.LIGHTBLUE_EX + "Do you want icons [Y or N]: " +
                    Fore.CYAN)
        version.get_all(dir_, os.path.join(path_to_info_dir, "default_files"))
        version.filter_unused(dir_,
                              os.path.join(path_to_info_dir, "default_files"),
                              kr, ki)
    store = {
        "name": name,
        "desc": desc,
        "mc_version": version.ver,
        "assets_grabbed": assetsdownloaded,
        "has_realms": kr,
        "has_icons": ki,
        "langs": {}
    }
    json.dump(store, open(os.path.join(path_to_info_dir, "metadata.json"),
                          "w"))