/*
 * This file is part of Bisq.
 *
 * Bisq is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or (at
 * your option) any later version.
 *
 * Bisq is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
 * License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with Bisq. If not, see <http://www.gnu.org/licenses/>.
 */

package bisq.core.api;

import javax.inject.Inject;
import javax.inject.Singleton;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import lombok.extern.slf4j.Slf4j;

import static java.io.File.separator;
import static java.lang.String.format;
import static java.lang.System.out;

@Singleton
@Slf4j
class CoreHelpService {

    @Inject
    public CoreHelpService() {
    }

    public String getMethodHelp(String methodName) {
        switch (methodName) {
            case "createoffer":
            case "getfundingaddresses":
            case "getpaymentaccts":
            case "getpaymentmethods":
            case "gettxfeerate":
            case "getunusedbsqaddress":
            case "getversion":
            case "lockwallet":
            case "takeoffer":
            case "unsettxfeerate":
                return getHelpText(methodName);
            default:
                throw new IllegalStateException("no help found for " + methodName);
        }
    }

    private String getHelpText(String methodName) {
        String resourceFile = "/help" + separator + methodName + "-" + "help.txt";
        try {
            return readHelpFile(resourceFile);
        } catch (NullPointerException ex) {
            log.error("", ex);
            throw new IllegalStateException(format("could not find %s help doc", methodName));
        } catch (IOException ex) {
            log.error("", ex);
            throw new IllegalStateException(format("could not read %s help doc", methodName));
        }
    }

    private String readHelpFile(String resourceFile) throws NullPointerException, IOException {
        // The deployed text file is in the core.jar file, so use
        // Class.getResourceAsStream to read it.
        InputStream is = getClass().getResourceAsStream(resourceFile);
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String line;
        StringBuilder builder = new StringBuilder();
        while ((line = br.readLine()) != null)
            builder.append(line).append("\n");

        return builder.toString();
    }

    // Main method for devs to view help text without running the server.
    @SuppressWarnings("CommentedOutCode")
    public static void main(String[] args) {
        CoreHelpService coreHelpService = new CoreHelpService();
        // out.println(coreHelpService.getMethodHelp("getversion"));
        // out.println(coreHelpService.getMethodHelp("getfundingaddresses"));
        // out.println(coreHelpService.getMethodHelp("getfundingaddresses"));
        // out.println(coreHelpService.getMethodHelp("getunusedbsqaddress"));
        // out.println(coreHelpService.getMethodHelp("unsettxfeerate"));
        // out.println(coreHelpService.getMethodHelp("getpaymentmethods"));
        // out.println(coreHelpService.getMethodHelp("getpaymentaccts"));
        // out.println(coreHelpService.getMethodHelp("lockwallet"));
        // out.println(coreHelpService.getMethodHelp("gettxfeerate"));
        out.println(coreHelpService.getMethodHelp("createoffer"));
        // out.println(coreHelpService.getMethodHelp("takeoffer"));
        // out.println(coreHelpService.getMethodHelp("garbage"));
    }
}

@@ -1188,538 +1188,39 @@
     
-switch (methodName) %7B%0A            case %22createoffer%22:%0A            case %22getfundingaddresses%22:%0A            case %22getpaymentaccts%22:%0A            case %22getpaymentmethods%22:%0A            case %22gettxfeerate%22:%0A            case %22getunusedbsqaddress%22:%0A            case %22getversion%22:%0A            case %22lockwallet%22:%0A            case %22takeoffer%22:%0A            case %22unsettxfeerate%22:%0A                return getHelpText(methodName);%0A            default:%0A                throw new IllegalStateException(%22no help found for %22 + methodName);%0A        %7D
+return getHelpText(methodName);
 %0A   
@@ -1550,34 +1550,39 @@
 at(%22
-could not find %25s help doc
+no help found for api method %25s
 %22, m
@@ -2565,35 +2565,32 @@
 rvice();%0A       
- //
  out.println(cor
@@ -3228,32 +3228,35 @@
 rate%22));%0A       
+ //
  out.println(cor
@@ -3429,20 +3429,140 @@
 garbage%22));%0A
+        // out.println(coreHelpService.getMethodHelp(%22%22));%0A        // out.println(coreHelpService.getMethodHelp(null));%0A
     %7D%0A%7D%0A

Remove switch(hardcoded enum.name)