package com.robothand.highqualitybot;

import com.robothand.highqualitybot.command.Command;
import com.robothand.highqualitybot.permission.PermissionManager;

import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Scanner;

/**
 * Config.java
 *
 * Reads in properties from a specified configuration file, and provides access to the values.
 */
public class Config {

    public static String TOKEN;
    public static String PREFIX;
    public static String OWNERID;
    public static ArrayList<Command> ALLOWED;
    public static ArrayList<Command> DISALLOWED;
    public static boolean ALLOWED_HAS_PRECEDENCE;
    public static ArrayList<String> PERMGROUPS;

    public static void loadConfig(String path) throws FileNotFoundException {
        // set default values
        PREFIX = ".";
        ALLOWED = new ArrayList<>();
        DISALLOWED = new ArrayList<>();
        ALLOWED_HAS_PRECEDENCE = true;
        PERMGROUPS = new ArrayList<>();

        // read config.cfg
        Hashtable<String,String> table = Utils.readCFG("config.cfg");
        for (String key : table.keySet()) {
            String value = table.get(key);

            // place the property if it is known
            switch (key) {
                case "token":
                    TOKEN = value;
                    break;

                case "prefix":
                    PREFIX = value;
                    break;

                case "ownerid":
                    OWNERID = value;
                    break;

                case "allowed":
                    ALLOWED = PermissionManager.instance().parseCommandList(value);
                    break;

                case "disallowed":
                    DISALLOWED = PermissionManager.instance().parseCommandList(value);
                    break;

                case "allowedHasPrecedence":
                    ALLOWED_HAS_PRECEDENCE = Boolean.parseBoolean(value);
                    break;

                case "permissiongroups":
                    for (String filename : value.split(",")) {
                        PERMGROUPS.add(filename.trim());
                    }
                    break;

                default:
                    System.out.println("Info: Unknown property \"" + key + "\", ignoring.");
            }
        }

        // Check for values that would need user input
        if (TOKEN == null) {
            Scanner c = new Scanner(System.in);

            System.out.println("Warning: OAuth token not set in config. Please enter your token below:");
            System.out.print("> ");
            TOKEN = c.nextLine();

            System.out.println("Token set. Recommend setting in config file for future runs.");
        }
    }
}

@@ -150,16 +150,73 @@
 Manager;
+%0Aimport org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;
 %0A%0Aimport
@@ -473,16 +473,86 @@
 onfig %7B%0A
+    private static Logger log = LoggerFactory.getLogger(Config.class);
 %0A    pub
@@ -2342,64 +2342,367 @@
     
-default:%0A                    System.out.println(%22Info: U
+case %22logLevel%22:%0A                    ch.qos.logback.classic.Logger root =%0A                            (ch.qos.logback.classic.Logger)LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);%0A                    root.setLevel(ch.qos.logback.classic.Level.toLevel(value, ch.qos.logback.classic.Level.INFO));%0A%0A                default:%0A                    log.info(%22%7B%7D: u
 nkno
@@ -2718,22 +2718,13 @@
 ty %5C
-%22%22 + key + %22%5C%22
+'%7B%7D%5C'
 , ig
@@ -2908,37 +2908,19 @@
     
-System.out.println(%22Warning: 
+log.error(%22
 OAut
@@ -2931,18 +2931,22 @@
 ken not 
+pre
 se
+n
 t in con
@@ -2950,16 +2950,69 @@
 config. 
+Querying stdin...%22);%0A            System.out.println(%22
 Please e
@@ -3124,53 +3124,36 @@
     
-System.out.println(%22Token set. Recommend sett
+log.error(%22Token set. Provid
 ing 
@@ -3166,13 +3166,8 @@
 fig 
-file 
 for 
@@ -3177,16 +3177,38 @@
 ure runs
+ is highly recommended
 .%22);%0A   

add loglevel option to config.cfg