/****************************************************************************
 * Copyright (C) 2019 ecsec GmbH.
 * All rights reserved.
 * Contact: ecsec GmbH (info@ecsec.de)
 *
 * This file may be used in accordance with the terms and conditions
 * contained in a signed written agreement between you and ecsec GmbH.
 *
 ***************************************************************************/

package org.openecard.sal.protocol.eac.gui;


/**
 *
 * @author Tobias Wich
 */
public class PinState {

    private int attempts;
    private boolean requestCan;
    private boolean blocked;
    private boolean deactivated;

    private EacPinStatus state;

    public PinState() {
	state = EacPinStatus.RC3;
	attempts = 2;
	requestCan = false;
	blocked = false;
	deactivated = false;
    }

    public void update(EacPinStatus status) {
	if (status == null) {
	    status = EacPinStatus.UNKNOWN;
	}
	state = status;
	switch (status) {
	    case RC3:
		attempts = 2;
		requestCan = false;
		blocked = false;
		deactivated = false;
		break;
	    case RC2:
		attempts = 1;
		requestCan = false;
		blocked = false;
		deactivated = false;
		break;
	    case RC1:
		attempts = 0;
		requestCan = true;
		blocked = false;
		deactivated = false;
		break;
	    case BLOCKED:
		attempts = 0;
		requestCan = false;
		blocked = true;
		deactivated = false;
		break;
	    case DEACTIVATED:
		attempts = 0;
		requestCan = false;
		blocked = false;
		deactivated = true;
		break;
	    case UNKNOWN:
		attempts = 0;
		requestCan = false;
		blocked = false;
		deactivated = false;
	}

    }

    public EacPinStatus getState() {
	return state;
    }

    public int getAttempts() {
	switch (state) {
	    case RC3:
		return 2;
	    case RC2:
		return 1;
	    case RC1:
		return 0;
	    default:
		return 0;
	}
    }

    public boolean isRequestCan() {
	return state == EacPinStatus.RC1;
    }

    public boolean isBlocked() {
	return state == EacPinStatus.BLOCKED;
    }

    public boolean isDeactivated() {
	return state == EacPinStatus.DEACTIVATED;
    }

    public boolean isOperational() {
	return ! isBlocked() && ! isDeactivated();
    }

    public boolean isUnknown() {
	return state == EacPinStatus.UNKNOWN;
    }

}

@@ -1701,17 +1701,17 @@
 %09return 
-2
+3
 ;%0A%09    c
@@ -1728,17 +1728,17 @@
 %09return 
-1
+2
 ;%0A%09    c
@@ -1755,17 +1755,17 @@
 %09return 
-0
+1
 ;%0A%09    d

Increment attempt count to match testsuite expactation