#include "SequenceECUFlasher.hpp"
#include "TimeUtils.h"

namespace otx {
	namespace flash {

		bool SequenceECUFlasher::download() {
			// 执行ecu的刷写，按顺序刷写其中的文件。
			// 该方法是同步方法，所有文件刷写完成后返回。

			int size = this->mECUs.size();
			this->mStartTime = getTimeStamp();

			bool isSucceed = true;
			for (int i = 0; i < size; i++) {
				ECU* ecu = this->mECUs[i];

				isSucceed = downloadECU(ecu);
			}
			this->mDownloadedSize = this->mTotalSize;

			return isSucceed;
		}

		bool SequenceECUFlasher::downloadECU(ECU* ecu) {

			otx::flash::ECUFlasherECUEventArg arg(this, ecu);
			postECUDownloadStart(arg);

			std::vector<otx::flash::File>* files = ecu->getFiles();
			int size = files->size();

			bool isSucceed = true;
			for (int i = 0; i < size; i++) {
				File ecuFile = files->at(i);
				isSucceed = downloadFile(ecu, &ecuFile);
				if (!isSucceed)
					break;
			}

			postECUDownloadEnd(arg);
			return isSucceed;
		}

	} // namespace flash
} // namespace otx