

package com.github.robozonky.api.remote.enums;

import java.util.Objects;
import java.util.Optional;
import java.util.stream.Stream;

import javax.json.bind.annotation.JsonbTypeDeserializer;

import com.github.robozonky.internal.util.json.PurposeDeserializer;

@JsonbTypeDeserializer(PurposeDeserializer.class)
public enum Purpose implements BaseEnum {

    AUTO_MOTO("auto-moto"),
    EDUCATION("vzdělání"),
    TRAVEL("cestování"),
    ELECTRONICS("elektronika"),
    HEALTH("zdraví"),
    REFINANCING("refinancování půjček"),
    HOUSEHOLD("domácnost"),
    OWN_PROJECT("vlastní projekt"),
    OTHER("jiné");

    private final String code;

    Purpose(final String code) {
        this.code = code;
    }

    private static Optional<Purpose> maybeFindByCode(final String code) {
        return Stream.of(Purpose.values())
            .filter(r -> Objects.equals(r.code, code))
            .findFirst();
    }

    public static Purpose findByCode(final String code) {
        return maybeFindByCode(code)
            .orElseThrow(() -> new IllegalArgumentException("Unknown loan purpose: " + code));
    }

    @Override
    public String getCode() {
        return code;
    }
}
