﻿#ifndef __OTX_FLASH_ECUFLASHERIMPL_HPP__
#define __OTX_FLASH_ECUFLASHERIMPL_HPP__

#include "ECUFlasher.hpp"
#include <vector>
#include <mutex>
#include "MCDFileDownloader.hpp"

namespace otx{
namespace flash{
/**
 * @brief 描述ECU刷写器的基本行为准则。
 *
 */
class ECUFlasherImpl : virtual public ECUFlasher {
friend class MCDFileDownloader;
friend class BinFileDownloader;
friend class VbfFileDownloader;

protected:
  ECUFlasherImpl();
  ECUFlasherImpl(const ECUFlasherImpl &right) = default;
  ECUFlasherImpl &operator=(const ECUFlasherImpl &right) = default;

public:
  virtual ~ECUFlasherImpl() = default;

private:
  // event handler对象不在这里管理，由外部管理。
  std::vector<ECUFlasherEventHandler *> mEventHandlers;

protected:
  size_t mTotalSize;             // 总的文件大小。
  volatile size_t mDownloadedSize;        // 已经下载的大小。
  unsigned long long mStartTime; // 下载开始时间。
  std::mutex mFieldLock;

public:
  virtual void addEventHandler(ECUFlasherEventHandler *eventListener) override;
  virtual void clearEventHandlers() override;
  virtual unsigned long long getRemainTime(ECU* ecu) override;

protected:
  virtual void postECUDownloadStart(ECUFlasherECUEventArg &arg);
  virtual void postECUDownloadEnd(ECUFlasherECUEventArg &arg);

  virtual void postECUResetStart(ECUFlasherECUEventArg& arg);
  virtual void postECUResetEnd(ECUFlasherECUEventArg& arg);

  virtual void postFileDownloadStart(ECUFlasherFileEventArg &arg);
  virtual void postFileDownloadEnd(ECUFlasherFileDownloadEndEventArg &arg);
  virtual void postFileDownloadProgress(ECUFlasherFileDownloadProgressEventArg &arg);

  virtual bool downloadFile(ECU *ecu, File *ecuFile);
};
} // namespace flash
} // namespace otx

#endif // __OTX_FLASH_ECUFLASHERIMPL_HPP__