package me.teridax.jcash.banking.accounts;

import me.teridax.jcash.Logging;
import me.teridax.jcash.decode.StringDecoder;

import java.util.Objects;

/**
 * Represents a person owning an account.
 */
@SuppressWarnings("unused")
public final class Owner {
    /**
     * unique identifier
     */
    private final int uid;
    private final String familyName;
    private final String name;
    private final String street;
    /**
     * postal code
     */
    private final int zip;
    private final String city;

    private Owner(int uid, String familyName, String name, int zip, String city, String street) {
        this.uid = uid;
        this.familyName = familyName;
        this.name = name;
        this.zip = zip;
        this.city = city;
        this.street = street;
    }

    /**
     * Create a new instance of this class parsed from the columns.
     *
     * @param columns the fields of this class as strings
     * @return an instance of this class
     * @throws IllegalArgumentException if the supplied columns is invalid
     * @throws NullPointerException     if columns is null
     */
    public static Owner fromColumns(String... columns) throws IllegalArgumentException, NullPointerException {
        Objects.requireNonNull(columns);
        Logging.LOGGER.finer("parsing owner from columns");

        if (columns.length != 6)
            throw new IllegalArgumentException("Invalid number of columns: " + columns.length);

        Logging.LOGGER.finer("Decoding owner fields");
        // decode fields
        var uid = StringDecoder.decodeUniqueIdentificationNumber(columns[0]);
        var familyName = StringDecoder.decodeName(columns[1]);
        var name = StringDecoder.decodeName(columns[2]);
        var street = StringDecoder.decodeStreet(columns[3]);
        var zip = StringDecoder.decodeUniqueIdentificationNumber(columns[4]);
        var city = StringDecoder.decodeName(columns[5]);

        return new Owner(uid, familyName, name, zip, city, street);
    }

    public long getUid() {
        return uid;
    }

    public String getFamilyName() {
        return familyName;
    }

    public String getName() {
        return name;
    }

    public int getZip() {
        return zip;
    }

    public String getCity() {
        return city;
    }

    public String getStreet() {
        return street;
    }

    @Override
    public int hashCode() {
        return Objects.hash(uid);
    }

    @Override
    public boolean equals(Object obj) {
        if (obj instanceof Owner)
            return this.uid == ((Owner) obj).getUid();

        return false;
    }

    @Override
    public String toString() {
        return String.format("@Owner[uid=%08x, familyName=%s, name=%s, zip=%s, city=%s, street=%s]", uid, familyName, name, zip, city, street);
    }
}
