#include "login_1.h"

// Author: Steve Talbot

login_1:: login_1(){
}

login_1:: ~login_1(){
}

bool login_1:: login( int y_, Data* data_, MDA_EFSM* M_ ){

	if ( data_->hasAccountInfo( y_ ) == true ){

		// NOTE: this application assumes 1 user has access to file
		//       at a time (no concurrent access).  Therefore,
		//       a "local copy" memory of a record will be as valid
		//       a "file copy".  In addition, the "local copy"
		//       requires less overhead to retrieve than the "file copy" ..

		// set the "current account" pointer in preparation / anticipation
		// of the next step yielding a correct "pin" ..
		data_->setCurrentAccountInfo( data_->getAccountInfo( y_ ) );

		// indicate that the "current account info" is NOT coming from the file ..
		data_->setIsCurrentAccountFromFile( false );

		data_->setTempId( y_ );

		bool result = M_->correct_login();

		return result;

	} else {

		// should handle loading in the correct “accountInfo”
		// from file if necessary, HERE ..

		bool fileFailed = false;

		bool isFileLoaded = data_->loadAccountInfoFromFile( y_ );

		if( isFileLoaded == true ) {

			// indicate that the "current account info" is coming from the file ..
			// in preparation / anticipation of the next step yielding a correct "pin" ..
			data_->setIsCurrentAccountFromFile( true );

			data_->setTempId( y_ );

			bool result = M_->correct_login();

			return result;

		} else {

			// don't bother with error messages .. system failure == bad login!
			fileFailed = true;

		} // end "if()" ..

		if ( fileFailed ) {

			// indicate that the "current account info" is NOT coming from the file ..
			data_->setIsCurrentAccountFromFile( false );

			bool result = M_->incorrect_login();

			return result;

		}

	} // end "if()" ..


	// default (shouldn't reach this point) ..
	bool result = M_->incorrect_login();
	return result;

} // end "login()" ..

