package info.knacki.prometheusandroidexporter;

import androidx.annotation.NonNull;

import java.util.HashMap;
import java.util.Map;

public class CollectorType {
    public final String fName;
    public final String fHelp;
    public final Type fType;

    public static class Type {
        public final String fName;
        private Type(String name) { this.fName = name; }

        @Override
        public String toString() {
            return fName;
        }

        public final static Type COUNTER = new Type("counter");
        public final static Type GAUGE = new Type("gauge");
        public final static Type SUMMARY = new Type("summary");
    }

    public CollectorType(String name, String help, Type type) {
        fName = "android_" + name;
        fHelp = help;
        fType = type;
    }

    public class CollectorValue {
        private String valueAsString;
        private Map<String, String> fParameters = new HashMap<>();

        public String GetKey() {
            StringBuilder result = new StringBuilder(fName);
            if (!fParameters.isEmpty()) {
                result.append("{");
                int count = fParameters.size();
                for (Map.Entry<String, String> i : fParameters.entrySet()) {
                    result.append(i.getKey()).append("=\"").append(i.getValue()).append("\"");
                    if (--count != 0)
                        result.append(",");
                }
                result.append("}");
            }
            return result.toString();
        }

        public CollectorType GetType() {
            return CollectorType.this;
        }

        public CollectorValue SetValue(double value) {
            valueAsString = Double.toString(value);
            return this;
        }
        public CollectorValue SetValue(int value) {
            valueAsString = Integer.toString(value);
            return this;
        }

        public void AddParameter(String key, String val) {
            fParameters.put(key, val);
        }

        @NonNull
        @Override
        public String toString() {
            return valueAsString;
        }
    }
}
