package org.iri_research.renkan.models;

import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.codec.binary.Hex;
import org.iri_research.renkan.Constants;
import org.iri_research.renkan.RenkanException;
import org.iri_research.renkan.RenkanRuntimeException;

public abstract class AbstractRenkanModel<ID extends Serializable> implements
        IRenkanModel<ID> {

    public AbstractRenkanModel(ID id, String title, String description,
            String uri, String color) {
        super();
        this.id = id;
        this.title = title;
        this.description = description;
        this.uri = uri;
        this.color = color;
    }

    protected AbstractRenkanModel() {
    }

    protected ID id;
    protected String title;
    protected String description;
    protected String uri;
    protected String color;

    @Override
    public String getTitle() {
        return this.title;
    }

    @Override
    public String getDescription() {
        return this.description;
    }

    @Override
    public String getUri() {
        return this.uri;
    }

    @Override
    public String getColor() {
        return this.color;
    }

    @Override
    public ID getId() {
        return this.id;
    }

    @Override
    public void setId(ID id) throws RenkanRuntimeException {
        if (this.id != null) {
            throw new RenkanRuntimeException(
                    "Current id is not null, can not change object id");
        }
        this.id = id;
    }

    @Override
    public void setTitle(String title) {
        this.title = title;
    }

    @Override
    public void setDescription(String description) {
        this.description = description;
    }

    @Override
    public void setUri(String uri) {
        this.uri = uri;
    }

    @Override
    public void setColor(String color) {
        this.color = color;
    }

    abstract protected String getRawKeyPart();

    private String getRawKey(String salt) {
        StringBuffer key = new StringBuffer(salt != null ? salt + "|" : "");
        key.append(this.getId());
        key.append('|');
        key.append(this.getRawKeyPart());
        return key.toString();
    }

    public String getKey(String salt) throws RenkanException {

        String rawKey = this.getRawKey(salt);

        MessageDigest md;
        try {
            md = MessageDigest.getInstance("SHA-256");
        } catch (NoSuchAlgorithmException e) {
            throw new RenkanException("NoSuchAlgorithmException digest: "
                    + e.getMessage(), e);
        }
        String key;
        final SecretKeySpec secret_key = new SecretKeySpec(
                Constants.KEYHEX.getBytes(), "HmacSHA256");
        md.update(secret_key.getEncoded());
        try {
            key = Hex.encodeHexString(md.digest(rawKey.getBytes("UTF-8")));
        } catch (UnsupportedEncodingException e) {
            throw new RenkanException("UnsupportedEncodingException digest: "
                    + e.getMessage(), e);
        }

        return key;
    }

    public boolean checkKey(String key, String salt) throws RenkanException {

        if (key == null || key.isEmpty()) {
            return false;
        }

        String signature = key;

        String new_key = this.getKey(salt);

        return new_key.equals(signature);
    }

}
