package no.stelar7.api.l4j8.impl.lcu;

import no.stelar7.api.l4j8.basic.utils.*;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.stream.Collectors;

public class LCUConnection
{
    public static Pair<String, String> getConnectionData()
    {
        boolean      isWindows      = System.getProperty("os.name").toLowerCase(Locale.ENGLISH).contains("win");
        List<String> windowsCommand = Arrays.asList("WMIC", "process", "where", "name='LeagueClientUx.exe'", "get", "commandLine");
        List<String> macCommand     = Arrays.asList("ps", "x", "|", "grep", "'LeagueClientUx.exe'");
        
        String passwordRegex = "--remoting-auth-token=(?<password>[^ \\\"]+)";
        String portRegex     = "--app-port=(?<port>\\d+)";
        
        try
        {
            ProcessBuilder builder = new ProcessBuilder(isWindows ? windowsCommand : macCommand);
            Process        process = builder.start();
            BufferedReader br      = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String         line    = br.lines().collect(Collectors.joining());
            
            String password = Utils.extractRegex(line, passwordRegex, "password");
            String port     = Utils.extractRegex(line, portRegex, "port");
            
            if (password == null || port == null)
            {
                System.err.println("The league client does not appear to be running!");
                return null;
            }
            
            return new Pair<>(password, port);
            
        } catch (Exception e)
        {
            e.printStackTrace();
            return null;
        }
    }
    
    public static String getConnectionString()
    {
        Pair<String, String> info = getConnectionData();
        return String.format("https://@127.0.0.1:%s/", info.getValue());
    }
    
    private static Pair<String, String> headerMap;
    
    public static Pair<String, String> getAuthorizationHeader()
    {
        if (headerMap != null)
        {
            return headerMap;
        }
        
        
        Pair<String, String> info    = getConnectionData();
        String               val     = "riot:" + info.getKey();
        String               encoded = new String(Base64.getEncoder().encode(val.getBytes(StandardCharsets.UTF_8)), StandardCharsets.UTF_8);
        
        return (headerMap = new Pair<>("Authorization", "Basic " + encoded));
    }
}

@@ -1714,24 +1714,71 @@
 %0A    %7D%0A    %0A
+    %0A    private static String urlString;%0A    %0A
     public s
@@ -1810,32 +1810,122 @@
 nString()%0A    %7B%0A
+        if (urlString != null)%0A        %7B%0A            return urlString;%0A        %7D%0A        %0A
         Pair%3CStr
@@ -1979,16 +1979,29 @@
   return
+ (urlString =
  String.
@@ -2020,9 +2020,8 @@
 s://
-@
 127.
@@ -2048,16 +2048,17 @@
 Value())
+)
 ;%0A    %7D%0A
@@ -2278,25 +2278,16 @@
        %0A
-        %0A
         
@@ -2312,19 +2312,16 @@
 g%3E info 
-   
 = getCon
@@ -2347,39 +2347,35 @@
     
-String               val  
+%0A        byte%5B%5D login
    = 
+(
 %22rio
@@ -2397,236 +2397,370 @@
 ey()
-;%0A        String               encoded = new String(Base64.getEncoder().encode(val.getBytes(StandardCharsets.UTF_8)), StandardCharsets.UTF_8);%0A        %0A        return (headerMap = new Pair%3C%3E(%22Authorization%22, %22Basic %22 + encoded))
+).getBytes(StandardCharsets.UTF_8);%0A        byte%5B%5D base64  = Base64.getEncoder().encode(login);%0A        String encoded = new String(base64, StandardCharsets.UTF_8);%0A        %0A        return (headerMap = new Pair%3C%3E(%22Authorization%22, %22Basic %22 + encoded));%0A    %7D%0A    %0A    public static void refetchConnection()%0A    %7B%0A        headerMap = null;%0A        urlString = null
 ;%0A  

Add cache to the connection url aswell