package com.atlassian.bitbucket.internal.key.ssh.dao;

import com.google.common.base.Objects;
import org.hamcrest.Description;
import org.hamcrest.Factory;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;

public class SshKeyMatcher extends TypeSafeMatcher<AoSshKey> {

    private final String keyText;
    private final String md5Hash;
    private final String label;
    private final Integer creatorId;
    private final Integer userId;

    SshKeyMatcher(String keyText, String md5Hash, String label, Integer creatorId, Integer userId) {
        this.keyText = keyText;
        this.md5Hash = md5Hash;
        this.label = label;
        this.creatorId = creatorId;
        this.userId = userId;
    }

    public void describeTo(Description description) {
        description.appendText("{")
                .appendText("keyText: ").appendValue(keyText)
                .appendText(", md5: ").appendValue(md5Hash)
                .appendText(", label: ").appendValue(label)
                .appendText(", userId: ").appendValue(userId)
                .appendText(", creatorId: ").appendValue(creatorId)
                .appendText("}");
    }

    @Override
    protected void describeMismatchSafely(AoSshKey mismatch, Description description) {
        if (mismatch == null) {
            description.appendText("{null}");
            return;
        }

        description.appendText("was {")
                .appendText("keyText: ").appendValue(mismatch.getText())
                .appendText(", md5: ").appendValue(mismatch.getMD5())
                .appendText(", label: ").appendValue(mismatch.getLabel())
                .appendText(", userId: ").appendValue(mismatch.getUserId())
                .appendText("}");
    }

    @Override
    public boolean matchesSafely(AoSshKey key) {
        return Objects.equal(keyText, key.getText()) &&
               Objects.equal(md5Hash, key.getMD5()) &&
               Objects.equal(label, key.getLabel()) &&
               Objects.equal(userId, key.getUserId());
    }

    @Factory
    public static Matcher<AoSshKey> key(String keyText, String md5Hash, String label,
                                        Integer userId, Integer creatorId) {
        return new SshKeyMatcher(keyText, md5Hash, label, creatorId, userId);
    }
}

