// Copyright 2024 The Pigweed Authors
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
// use this file except in compliance with the License. You may obtain a copy of
// the License at
//
//     https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations under
// the License.

#include "modules/board/service.h"

#include "modules/board/board.h"
#include "pw_log/log.h"
#include "pw_status/status.h"
#include "system/system.h"

namespace sense {

BoardService::BoardService()
    : temp_sample_timer_([this](pw::chrono::SystemClock::time_point) {
        TempSampleCallback();
      }) {}

void BoardService::Init(Worker& worker, Board& board) {
  worker_ = &worker;
  board_ = &board;
}

pw::Status BoardService::Reboot(const board_RebootRequest& request,
                                pw_protobuf_Empty& /*response*/) {
  return board_->Reboot(request.reboot_type);
}

pw::Status BoardService::OnboardTemp(const pw_protobuf_Empty& /*request*/,
                                     board_OnboardTempResponse& response) {
  response.temp = board_->ReadInternalTemperature();
  return pw::OkStatus();
}

void BoardService::OnboardTempStream(
    const board_OnboardTempStreamRequest& request,
    ServerWriter<board_OnboardTempResponse>& writer) {
  if (request.sample_interval_ms < 100) {
    if (const auto status = writer.Finish(pw::Status::InvalidArgument());
        !status.ok()) {
      PW_LOG_ERROR("Failed to write response: %s", status.str());
    }
    return;
  }

  temp_sample_interval_ = pw::chrono::SystemClock::for_at_least(
      std::chrono::milliseconds(request.sample_interval_ms));
  temp_sample_writer_ = std::move(writer);

  ScheduleTempSample();
}

void BoardService::TempSampleCallback() {
  float temp = board_->ReadInternalTemperature();

  pw::Status status = temp_sample_writer_.Write({.temp = temp});
  if (status.ok()) {
    ScheduleTempSample();
  } else {
    PW_LOG_INFO("Temperature stream closed; ending periodic sampling");
  }
}

void BoardService::ScheduleTempSample() {
  worker_->RunOnce(
      [this]() { temp_sample_timer_.InvokeAfter(temp_sample_interval_); });
}

}  // namespace sense
