package pro.zhumin.health.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import pro.zhumin.health.model.BloodPressureRecord;
import pro.zhumin.health.service.dao.BloodPressureRecordDao;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

@Service
public class RecordServiceImpl implements RecordService<BloodPressureRecord>{

  private final
  BloodPressureRecordDao recordDao;

  @Autowired
  public RecordServiceImpl(BloodPressureRecordDao recordDao) {
    this.recordDao = recordDao;
  }

  @Override
  public BloodPressureRecord getRecord(String id) {
    return recordDao.findById(id);
  }

  @Override
  public List<BloodPressureRecord> getRecords() {
    ArrayList<BloodPressureRecord> records = new ArrayList<>(recordDao.findAll());
    records.sort((o1, o2) -> {
      if (o1.getRecordTime().isBefore(o2.getRecordTime())){return 1;}else {return -1;}
    });
    return records;
  }

  @Override
  public BloodPressureRecord editRecord(BloodPressureRecord record) {
    return recordDao.save(record);
  }

  @Override
  public BloodPressureRecord addRecord(BloodPressureRecord record) {
    BloodPressureRecord bloodPressureRecord =  new BloodPressureRecord(record.getRecordTime(),
            record.getHighPressure(),record.getLowPressure(),record.getHeartRate());
    bloodPressureRecord.setRemark(record.getRemark());
    return recordDao.save(bloodPressureRecord);
  }

  @Override
  public void delRecord(String id) {
    recordDao.deleteById(id);
  }
}
