#ifndef __OTX_FLASH_ParallelECUFlasher_HPP__
#define __OTX_FLASH_ParallelECUFlasher_HPP__

#include "MultipleECUFlasher.hpp"
#include "taskpool/TaskPool.hpp"
#include <atomic>

namespace otx {
namespace flash {

class ParallelECUFlasher;
class FlashTaskArg {
public:
  ParallelECUFlasher *mFlasher;
  ECU *mEcu;
};

class ParallelECUFlasher : virtual public MultipleECUFlasher {
public:
  ParallelECUFlasher(TaskPool *pool);
  ParallelECUFlasher(const ParallelECUFlasher &right) = default;
  ParallelECUFlasher &operator=(const ParallelECUFlasher &right) = default;
  virtual ~ParallelECUFlasher() =default;

private:
  TaskPool *mPool;
  std::atomic<int> mCompletedECUs; // 已完成刷写的ECU数量。

public:
  virtual bool download() override;

private:
	bool ecuReady(ECU* mEcu);
	void delay(int ms);

	virtual void downloadECU(ECU *ecu);
	bool reset(ECU* ecu);
	bool exitFota(int iLevel);
	bool securityAccess(int iLevel);

private:
  static bool FinishProgram(ECU* mEcu);
  static void taskWork(Task *task);
  static void taskCancel(Task *task);
};
} // namespace flash
} // namespace otx

#endif // __OTX_FLASH_ParallelECUFlasher_HPP__