/*
 * Copyright (C) 2014 Tim Bray <tbray@textuality.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.textuality.keybase.lib;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class Match {
    private final JSONObject mComponents;

    public Match(JSONObject json) throws KeybaseException {
        try {
            mComponents = JWalk.getObject(json, "components");
        } catch (JSONException e) {
            throw KeybaseException.keybaseScrewup(e);
        }
    }
    public boolean hasKey() throws KeybaseException {
        try {
            return (JWalk.optObject(mComponents, "key_fingerprint") != null);
        } catch (JSONException e) {
            throw KeybaseException.keybaseScrewup(e);
        }
    }
    public String getKeyID() throws KeybaseException {
        String fingerprint = getFingerprint();
        if (fingerprint.length() > 16) {
            fingerprint = fingerprint.substring(fingerprint.length() - 16);
        }

        return fingerprint.replace(" ", "");
    }
    public String getUsername() throws KeybaseException{
        try {
            return JWalk.getString(mComponents, "username", "val");
        } catch (JSONException e) {
            throw KeybaseException.keybaseScrewup(e);
        }
    }
    public String getFullName() throws KeybaseException {
        try {
            return JWalk.getString(mComponents, "full_name", "val");
        } catch (JSONException e) {
            return null;
        }
    }
    public String getFingerprint() throws KeybaseException {
        try {
            return JWalk.getString(mComponents, "key_fingerprint", "val");
        } catch (JSONException e) {
            throw KeybaseException.keybaseScrewup(e);
        }
    }
    public int getAlgorithmId() throws KeybaseException {
        try {
            return JWalk.getInt(mComponents, "key_fingerprint", "algo");
        } catch (JSONException e) {
            throw KeybaseException.keybaseScrewup(e);
        }
    }
    public int getBitStrength() throws KeybaseException {
        try {
            return JWalk.getInt(mComponents, "key_fingerprint", "nbits");
        } catch (JSONException e) {
            throw KeybaseException.keybaseScrewup(e);
        }
    }
    public List<String> getProofLabels() {
        ArrayList<String> labels = new ArrayList<String>();
        try {
            labels.add("twitter.com/" + JWalk.getString(mComponents, "twitter", "val"));
        } catch (JSONException e) {
            // s'OK
        }
        try {
            labels.add("github.com/" + JWalk.getString(mComponents, "github", "val"));
        } catch (JSONException e) {
            // s'OK
        }
        try {
            JSONArray sites = JWalk.getArray(mComponents, "websites");
            labels.add(JWalk.getString(sites.getJSONObject(0), "val"));
        } catch (JSONException e) {
            // s'OK
        }
        return labels;
    }
}

@@ -872,19 +872,69 @@
 va.util.
-Lis
+Hashtable;%0Aimport java.util.List;%0Aimport java.util.Se
 t;%0A%0Apubl
@@ -3136,20 +3136,18 @@
 dd(%22
-t
+T
 witter
-.com/
+: @
 %22 + 
@@ -3303,244 +3303,912 @@
 dd(%22
-github.com/%22 + JWalk.getString(mComponents, %22github%22, %22val%22));%0A        %7D catch (JSONException e) %7B%0A            // s'OK%0A        %7D%0A        try %7B%0A            JSONArray sites = JWalk.getArray(mComponents, %22websites%22);%0A            labels.add
+GitHub: %22 + JWalk.getString(mComponents, %22github%22, %22val%22));%0A        %7D catch (JSONException e) %7B%0A            // s'OK%0A        %7D%0A        try %7B%0A            labels.add(%22Reddit: %22 + JWalk.getString(mComponents, %22reddit%22, %22val%22));%0A        %7D catch (JSONException e) %7B%0A            // s'OK%0A        %7D%0A        try %7B%0A            labels.add(%22Hacker News: %22 + JWalk.getString(mComponents, %22hackernews%22, %22val%22));%0A        %7D catch (JSONException e) %7B%0A            // s'OK%0A        %7D%0A        try %7B%0A            labels.add(%22Coinbase: %22 + JWalk.getString(mComponents, %22coinbase%22, %22val%22));%0A        %7D catch (JSONException e) %7B%0A            // s'OK%0A        %7D%0A        try %7B%0A            JSONArray sites = JWalk.getArray(mComponents, %22websites%22);%0A            Hashtable%3CString, Integer%3E uniqueNames = new Hashtable%3CString, Integer%3E();%0A            int i;%0A            for (i = 0; i %3C sites.length(); i++) %7B%0A                uniqueNames.put
 (JWa
@@ -4240,17 +4240,17 @@
 NObject(
-0
+i
 ), %22val%22
@@ -4242,32 +4242,439 @@
 bject(i), %22val%22)
+, 1);%0A            %7D%0A            Set%3CString%3E names = uniqueNames.keySet();%0A            StringBuilder label = new StringBuilder(%22Web: %22);%0A            i = 0;%0A            for (String name : names) %7B%0A                label.append(name);%0A                if (i %3C names.size() - 1) %7B%0A                    label.append(%22, %22);%0A                %7D%0A                i++;%0A            %7D%0A            labels.add(label.toString()
 );%0A        %7D cat

Improve proofLabels to handle more than twitter/github and the only the first (!) listed site