package com.larscheidschmitzhermes.nexus3.github.oauth.plugin.configuration;

import com.google.inject.Singleton;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.PostConstruct;
import javax.inject.Named;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.time.Duration;
import java.util.Properties;

@Singleton
@Named
public class GithubOauthConfiguration {
    private static final String CONFIG_FILE = "githuboauth.properties";

    private static final String GITHUB_USER_PATH = "/user";

    private static final String GITHUB_ORGS_PATH = "/user/orgs";

    private static final String GITHUB_TEAMS_IN_ORG_PATH = "/teams";

    private static final Duration DEFAULT_PRINCIPAL_CACHE_TTL = Duration.ofMinutes(1);

    private static final Logger LOGGER = LoggerFactory.getLogger(GithubOauthConfiguration.class);

    private Properties configuration;


    @PostConstruct
    public void init() {
        configuration = new Properties();
        try {
            configuration.load(Files.newInputStream(Paths.get(".", "etc", CONFIG_FILE)));
        } catch (IOException e) {
            LOGGER.error("Error reading github oauth properties", e);
        }
    }

    public String getGithubApiUrl() {
        return configuration.getProperty("github.api.url");

    }

    public String getGithubTeamsInOrgPath() {
        return GITHUB_TEAMS_IN_ORG_PATH;
    }

    public String getGithubOrgsUri() {
       return getGithubApiUrl() + GITHUB_ORGS_PATH;
    }

    public String getGithubUserUri() {
        return getGithubApiUrl() + GITHUB_USER_PATH;
    }

    public Duration getPrincipalCacheTtl() {
        String override = configuration.getProperty("github.principal.cache.ttl");
        if (override != null) {
            return Duration.parse(override);
        } else {
            return DEFAULT_PRINCIPAL_CACHE_TTL;
        }
    }
}

@@ -82,92 +82,139 @@
 ort 
-com.google.inject.Singleton;%0Aimport org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory
+java.io.IOException;%0Aimport java.nio.file.Files;%0Aimport java.nio.file.Paths;%0Aimport java.time.Duration;%0Aimport java.util.Properties
 ;%0A%0Ai
@@ -282,146 +282,101 @@
 ed;%0A
+%0A
 import 
-java.io.IOException;%0Aimport java.nio.file.Files;%0Aimport java.nio.file.Paths;%0Aimport java.time.Duration;%0Aimport java.util.Properties
+org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0A%0Aimport com.google.inject.Singleton
 ;%0A%0A@
@@ -786,24 +786,272 @@
 inutes(1);%0A%0A
+    private static final String DEFAULT_GITHUB_URL = %22https://api.github.com%22;%0A%0A    private static final String GITHUB_API_URL_KEY = %22github.api.url%22;%0A%0A    private static final String GITHUB_PRINCIPAL_CACHE_TTL_KEY = %22github.principal.cache.ttl%22;%0A%0A
     private 
@@ -1176,17 +1176,16 @@
 ation;%0A%0A
-%0A
     @Pos
@@ -1262,16 +1262,17 @@
 ties();%0A
+%0A
         
@@ -1571,28 +1571,49 @@
 rty(
-%22github.api.url%22
+GITHUB_API_URL_KEY, DEFAULT_GITHUB_URL
 );%0A
-%0A
     
@@ -1963,26 +1963,30 @@
     
-String override = 
+return Duration.parse(
 conf
@@ -2011,151 +2011,39 @@
 rty(
-%22github.principal.cache.ttl%22);%0A        if (override != null) %7B%0A            return Duration.parse(override);%0A        %7D else %7B%0A            return
+GITHUB_PRINCIPAL_CACHE_TTL_KEY,
  DEF
@@ -2066,27 +2066,30 @@
 ACHE_TTL
-;%0A        %7D
+.toString()));
 %0A    %7D%0A%7D

add default for github api url