package seedu.todo.controllers;

import java.io.IOException;
import java.util.List;

import seedu.todo.MainApp;
import seedu.todo.commons.core.Config;
import seedu.todo.commons.exceptions.CannotConfigureException;
import seedu.todo.commons.util.ConfigUtil;
import seedu.todo.controllers.concerns.Renderer;
import seedu.todo.models.TodoListDB;
import seedu.todo.ui.UiManager;

/**
 * Controller to declare aliases
 * 
 * @author louietyj
 *
 */
public class AliasController implements Controller {
    
    private static final String NAME = "Alias";
    private static final String DESCRIPTION = "Shows current aliases or updates them.";
    private static final String COMMAND_SYNTAX = "alias [<setting> <value>]";
    
    private static final String SPACE = " ";
    private static final int ARGS_LENGTH = 2;
    private static final String INVALID_NUM_PARAMS = "Seems like you have provided an invalid number of parameters!";
    private static final String MESSAGE_INVALID_INPUT = "Invalid alias parameters! Alias inputs must consist solely "
                                                      + "of alphabetical characters.";
    
    private static CommandDefinition commandDefinition =
            new CommandDefinition(NAME, DESCRIPTION, COMMAND_SYNTAX); 

    public static CommandDefinition getCommandDefinition() {
        return commandDefinition;
    }
    
    @Override
    public float inputConfidence(String input) {
        // TODO
        return input.startsWith("alias") ? 1 : 0;
    }

    @Override
    public void process(String input) {
        String params = input.replaceFirst("alias", "").trim();

        if (params.length() <= 0) {
            // TODO: Render aliases
        } else {
            String[] args = params.split(SPACE, ARGS_LENGTH);
            
            String aliasKey = null;
            String aliasValue = null;
            
            // Best-effort matching, disambiguate if wrong.
            validate: {
                switch (args.length) {
                    case 0:
                        break;
                    case 1:
                        aliasKey = args[0];
                        break;
                    case 2: // All good!
                        aliasKey = args[0];
                        aliasValue = args[1];
                        break validate; 
                    default:
                        aliasKey = args[0];
                        aliasValue = args[0];
                }
                renderDisambiguation(aliasKey, aliasValue, INVALID_NUM_PARAMS);
            }
            
            if (!validateAlias(aliasKey) || !validateAlias(aliasValue)) {
                renderDisambiguation(aliasKey, aliasValue, MESSAGE_INVALID_INPUT);
            }
        }
    }
    
    /**
     * Validates that string is sanitized and safe for aliasing.
     * 
     * @param alias     string to check
     * @return          true if string is sanitized, false otherwise
     */
    private static boolean validateAlias(String alias) {
        return alias.chars().allMatch(Character::isLetter);
    }
    
    /**
     * Makes a best effort to sanitize input string.
     * 
     * @param alias     string to sanitize
     * @return          sanitized string
     */
    private static String sanitize(String alias) {
        return (alias == null) ? null : alias.replaceAll("[^A-Za-z]+", "");
    }
    
    private static void renderDisambiguation(String aliasKey, String aliasValue, String message) {
        String sanitizedAliasKey = sanitize(aliasKey);
        if (sanitizedAliasKey == null || sanitizedAliasKey.length() == 0) {
            sanitizedAliasKey = "<alias key>";
        }
        String sanitizedAliasValue = sanitize(aliasValue);
        if (sanitizedAliasValue == null || sanitizedAliasValue.length() == 0) {
            sanitizedAliasValue = "<alias value>";
        }
        Renderer.renderDisambiguation(String.format("alias \"%s\" \"%s\"",
                sanitizedAliasKey, sanitizedAliasValue), message);
    }

}

@@ -1707,21 +1707,39 @@
     
-%7D else %7B%0A    
+    return;%0A        %7D%0A        %0A
     
@@ -1796,33 +1796,25 @@
 H);%0A        
-    %0A    
+%0A
         Stri
@@ -1837,28 +1837,24 @@
 ll;%0A        
-    
 String alias
@@ -1875,30 +1875,25 @@
         
-    %0A 
+%0A
         
    // Be
@@ -1884,19 +1884,16 @@
         
-   
 // Best-
@@ -1940,20 +1940,16 @@
         
-    
 validate
@@ -1964,20 +1964,16 @@
         
-  
-  
 switch (
@@ -1999,28 +1999,24 @@
             
-    
 case 0:%0A    
@@ -2027,39 +2027,31 @@
             
-   
- 
 break;%0A
-    
             
@@ -2074,36 +2074,32 @@
                 
-   
- 
 aliasKey = args%5B
@@ -2118,39 +2118,31 @@
             
-    
 break;%0A
-    
             
@@ -2178,36 +2178,32 @@
                 
-    
 aliasKey = args%5B
@@ -2198,36 +2198,32 @@
 sKey = args%5B0%5D;%0A
-    
                 
@@ -2264,28 +2264,24 @@
             
- 
-   
 break valida
@@ -2301,20 +2301,16 @@
         
-    
 default:
@@ -2322,36 +2322,32 @@
                 
-  
-  
 aliasKey = args%5B
@@ -2342,36 +2342,32 @@
 sKey = args%5B0%5D;%0A
-    
                 
@@ -2400,34 +2400,26 @@
             
-   
- 
 %7D%0A
-    
             
@@ -2482,20 +2482,16 @@
 ARAMS);%0A
-    
         
@@ -2496,33 +2496,25 @@
   %7D%0A        
-    %0A    
+%0A
         if (
@@ -2575,36 +2575,32 @@
 ) %7B%0A            
- 
-   
 renderDisambigua
@@ -2650,30 +2650,16 @@
 INPUT);%0A
-            %7D%0A
         
@@ -3846,21 +3846,13 @@
 ias 
-%5C%22%25s%5C%22 %5C%22%25s%5C%22
+%25s %25s
 %22,%0A 

Simplify program flow