#include "pin_1.h"

// Author: Steve Talbot

pin_1:: pin_1(){
}

pin_1:: ~pin_1(){
}

bool pin_1:: pin( int p_, Data* data_, MDA_EFSM* M_ ){

	bool isActive = false;

	bool isAboveMinBalance = true;

	bool attemptsRemain = true;

	// processing of “input” to determine if it is a suitable “pin” here ..

	if ( ( data_->getTempId() >= 0 ) &&
		 ( data_->isPinCorrect(  p_, data_->getTempId() ) ) ) {

		// if the "current account info" was just loaded in from file,
		// need to add it to "data"'s collection ..
		if( data_->getIsCurrentAccountFromFile() == true ) {

			// if "account info" is already in collection,
			// remove the existing collection copy (older) ..
			if ( ( data_->hasAccountInfo( data_->getTempId() ) ) == true ){

				data_->removeAccountInfo( data_->getTempId() );

			}

			// set the "current account" pointer to be the one loaded in from file ..
			data_->setCurrentAccountInfo( data_->getAccountFile()->getAccountInfo() );

			// add the latest file copy (newer) to the collection ..
			data_->addAccountInfo( data_->getAccountFile()->getAccountInfo() );

			// reset "isCurrentAccountFromFile" ..
			data_->setIsCurrentAccountFromFile( false );

		} // end "if()" ..

		// reset “attempts” ..
		data_->setAttempts( 0 );

		// reset "tempId" ..
		data_->setTempId( -1 );

		// track whether or not the account is “overdrawn” ..
		if( data_->getCurrentAccountInfo()->getIsOverdrawn() == true )
			isAboveMinBalance = false;

		// track whether or not the account is “active” ..
		if( data_->getCurrentAccountInfo()->getIsActive() == true )
			isActive = true;

		return M_->correct_pin( isAboveMinBalance, isActive );

	} else {

		// increment “attempts” ..
		data_->setAttempts( ( data_->getAttempts() + 1 ) );

		// track whether or not the account has pin “attempts” remaining ..
		if( data_->getAttempts() >= data_->getMaxAttempts() )
			attemptsRemain = false;

		// set "tempPin" ..
		data_->setTempPin( p_ );

		return M_->incorrect_pin( attemptsRemain );

	} // end “if” ..


}
