#include "logout_1.h"

// Author: Steve Talbot

logout_1:: logout_1(){
}

logout_1:: ~logout_1(){
}

bool logout_1:: logout( Data* data_, MDA_EFSM* M_ ){

	// must write "new" and "modified" account info objects to file ..
	// (persistent storage of accounts across login / logout operations)

	AccountInfo* accountInfo = NULL;

	bool needWriteToFile = false;

	int size = data_->getAccounts()->size();

	for(int i=0; i<size; i++){

		// get an "account info" object from the collection ..
		accountInfo = data_->getAccounts()->at( i );

		// check to see if this "account info" object has been changed
		// and therefore needs to be saved to file ..
		// (new objects in "open_account_a" are saved to file immediately) ..
		needWriteToFile = data_->getNeedsToBeFlushedToFile( accountInfo->getID() );

		// write the "account info" object to file if necessary,
		// passing in the index position where the object resides in the "accounts" collection ..
		if( needWriteToFile == true ) {

			data_->writeAccountInfoToFile( i );

			// reset this flag, now that the object is written to file ..
			data_->setNeedsToBeFlushedToFile( accountInfo->getID(), false );

		}

	}

	M_->logout();

	return true;

}
