package com.atlassian.stash.internal.help;

import com.atlassian.bitbucket.help.HelpPathService;
import com.atlassian.bitbucket.util.UrlUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.List;
import java.util.Map;

/**
 * This class contains the general HelpPathService logic for help paths, and only {@link #getProperty(String)} needs
 * to be implemented, which may defer to a properties file or i18nService.
 */
public abstract class AbstractHelpPathService implements HelpPathService {

    protected abstract String getProperty(String key);

    @Override
    public String getPageAlt(String topicKey) {
        return topicKey == null ? null : getProperty(topicKey + ".alt");
    }

    @Override
    public String getPageKey(String topicKey) {
        return topicKey == null ? null : getProperty(topicKey);
    }

    @Override
    public String getPageTitle(String topicKey) {
        return topicKey == null ? null : getProperty(topicKey + ".title");
    }

    @Override
    public String getPageUrl(String topicKey) {
        String helpPrefix = getPrefix(topicKey);
        String helpPageKey = getPageKey(topicKey);
        if (helpPrefix == null || helpPageKey == null) {
            return null;
        }

        StringBuilder query = new StringBuilder();
        String[] urlParts = helpPageKey.split("\\?|#", 2);
        if (urlParts.length > 1) {
            if (helpPageKey.contains("?")) {
                String[] queryParts = urlParts[1].split("#");
                query.append(getQueryParams(queryParts[0]));
                if (queryParts.length > 1) {
                    query.append('#');
                    query.append(queryParts[1]);
                }
            } else {
                query.append(getQueryParams(""));
                query.append('#');
                query.append(urlParts[1]);
            }
        } else {
            query.append(getQueryParams(""));
        }

        return String.format("%s/%s%s", helpPrefix, urlParts[0], query.toString());
    }

    @Override
    public String getPrefix() {
        return getProperty("bitbucket.help.prefix");
    }

    private String getPrefix(String topicKey) {
        if (StringUtils.startsWith(topicKey, "bitbucket.kb.")) {
            return getProperty("bitbucket.kb.prefix");
        } else if (StringUtils.startsWith(topicKey, "bitbucket.wac.")) {
            return getProperty("bitbucket.wac.prefix");
        } else if (StringUtils.startsWith(topicKey, "bitbucket.go.")) {
            return getProperty("bitbucket.go.prefix");
        } else if (StringUtils.startsWith(topicKey, "bitbucket.dac.")) {
            return getProperty("bitbucket.dac.prefix");
        } else if (StringUtils.startsWith(topicKey, "embedded.crowd.support.")) {
            return getProperty("embedded.crowd.support.prefix");
        }
        return getPrefix();
    }

    /**
     * @param helpQueryParams the string of query params that come from the help path
     * @return the final string of query params that merges those given with the defaults
     */
    private String getQueryParams(String helpQueryParams) {
        String queryString = getProperty("bitbucket.help.analytics.tracking");
        Map<String, List<Object>> params = UrlUtils.splitQuery(queryString);
        return "?" + UrlUtils.setQueryParams(helpQueryParams, params);
    }
}