package com.crowdin.cli;

import com.crowdin.cli.commands.RootCommand;
import com.crowdin.cli.commands.parts.HelpCommand;
import com.crowdin.cli.utils.Utils;
import picocli.CommandLine;

public class Cli {

    public static void main(String[] args) {
        try {
            setSystemProperties();
            CommandLine.Help.ColorScheme colorScheme = buildColorScheme();
            CommandLine.IExecutionExceptionHandler executionExceptionHandler = buildExecutionExceptionHandler();
            CommandLine commandLine = new CommandLine(new RootCommand())
                .setExecutionExceptionHandler(executionExceptionHandler)
                .setColorScheme(colorScheme);

            HelpCommand.setOptions(System.out, colorScheme);
            int exitCode = commandLine.execute(args);

            Utils.getNewVersionMassage().ifPresent(System.out::println);

            System.exit(exitCode);
        } catch (Exception e) {
            System.out.println("There is exception:");
            e.printStackTrace();
        }
    }

    private static void setSystemProperties() {
        if (System.getenv("HTTP_PROXY_HOST") != null) {
            System.setProperty("http.proxyHost", System.getenv("HTTP_PROXY_HOST"));
        }
        if (System.getenv("HTTP_PROXY_PORT") != null) {
            System.setProperty("http.proxyPort", System.getenv("HTTP_PROXY_PORT"));
        }
        if (System.getenv("HTTPS_PROXY_HOST") != null) {
            System.setProperty("https.proxyHost", System.getenv("HTTPS_PROXY_HOST"));
        }
        if (System.getenv("HTTPS_PROXY_PORT") != null) {
            System.setProperty("https.proxyPort", System.getenv("HTTPS_PROXY_PORT"));
        }
    }

    private static CommandLine.Help.ColorScheme buildColorScheme() {
        return new CommandLine.Help.ColorScheme.Builder()
                .commands(CommandLine.Help.Ansi.Style.fg_yellow)
                .options(CommandLine.Help.Ansi.Style.fg_yellow)
                .build();
    }

    private static CommandLine.IExecutionExceptionHandler buildExecutionExceptionHandler() {
        return (ex, cmd, pr) -> {
            if (pr.originalArgs().contains("--debug")) {
                ex.printStackTrace();
            } else {
                cmd.getErr().println(ex.getMessage());
                Throwable cause = ex;
                while ((cause = cause.getCause()) != null) {
                    cmd.getErr().println(cause.getMessage());
                }
            }
            return cmd.getExitCodeExceptionMapper() != null
                    ? cmd.getExitCodeExceptionMapper().getExitCode(ex)
                    : cmd.getCommandSpec().exitCodeOnExecutionException();
        };
    }
}

@@ -180,16 +180,88 @@
 dLine;%0A%0A
+import java.net.Authenticator;%0Aimport java.net.PasswordAuthentication;%0A%0A
 public c
@@ -1289,32 +1289,117 @@
 _PROXY_HOST%22));%0A
+            System.setProperty(%22https.proxyHost%22, System.getenv(%22HTTP_PROXY_HOST%22));%0A
         %7D%0A      
@@ -1536,38 +1536,66 @@
 ));%0A        
-%7D%0A        if (
+    System.setProperty(%22https.proxyPort%22, 
 System.geten
@@ -1605,38 +1605,34 @@
 HTTP
-S
 _PROXY_
-HOST%22) != null) %7B%0A
+PORT%22));%0A    
     
+%7D%0A
     
@@ -1640,44 +1640,25 @@
    S
-ystem.setProperty(%22https.proxyHost%22,
+tring proxyUser =
  Sys
@@ -1677,22 +1677,20 @@
 HTTP
-S
 _PROXY_
-HOST%22)
+USER%22
 );%0A 
@@ -1692,38 +1692,47 @@
 %22);%0A        
-%7D%0A        if (
+String proxyPassword = 
 System.geten
@@ -1734,33 +1734,32 @@
 getenv(%22HTTP
-S
 _PROXY_P
 ORT%22) != nul
@@ -1750,82 +1750,614 @@
 XY_P
+ASSW
 OR
-T%22) != null) %7B%0A            System.setProperty(%22https.proxyPort%22,
+D%22);%0A%0A        if (proxyUser != null && proxyPassword != null) %7B%0A            Authenticator.setDefault(%0A                new Authenticator() %7B%0A                    @Override%0A                    public PasswordAuthentication getPasswordAuthentication() %7B%0A                        if (getRequestorType() == RequestorType.PROXY) %7B%0A                            return new PasswordAuthentication(proxyUser, proxyPassword.toCharArray());%0A                        %7D else %7B%0A                            return null;%0A                        %7D%0A                    %7D%0A                %7D%0A            );%0A           
  System.
 gete
@@ -2356,34 +2356,65 @@
 tem.
-getenv(%22HTTPS_PROXY_PORT%22)
+setProperty(%22jdk.http.auth.tunneling.disabledSchemes%22, %22%22
 );%0A 

Improve proxy support