defmodule Api.Autosender do

  alias Api.StudyMailer
  use GenServer

  def start_link(period_in_millis) do
    GenServer.start_link(__MODULE__, %{period: period_in_millis})
  end

  @impl true
  def init(state) do  
    next_send(state.period)
    {:ok, state}
  end

  def next_send(period_in_millis) do
    Process.send_after(self(), :send, period_in_millis)
  end

  @impl true
  def handle_info(:send, state) do
    StudyMailer.deliver_mail()
    next_send(state.period)
    {:noreply, state}
  end
end
