%%%-------------------------------------------------------------------
%%% @author aresei
%%% @copyright (C) 2023, <COMPANY>
%%% @doc
%%% @end
%%%-------------------------------------------------------------------
-module(iot_host_sup).
-include("iot.hrl").

-behaviour(supervisor).

-export([start_link/0, init/1, delete_host/1, ensured_host_started/1]).

start_link() ->
    supervisor:start_link({local, ?MODULE}, ?MODULE, []).

init([]) ->
    Specs = lists:map(fun child_spec/1, host_bo:get_all_hosts()),

    {ok, {#{strategy => one_for_one, intensity => 1000, period => 3600}, Specs}}.

-spec ensured_host_started(UUID :: binary()) -> {ok, Pid :: pid()} | {error, Reason :: any()}.
ensured_host_started(UUID) when is_binary(UUID) ->
    case iot_host:get_pid(UUID) of
        undefined ->
            case supervisor:start_child(?MODULE, child_spec(UUID)) of
                {ok, Pid} when is_pid(Pid) ->
                    {ok, Pid};
                {error, {'already_started', Pid}} when is_pid(Pid) ->
                    {ok, Pid};
                {error, Error} ->
                    {error, Error}
            end;
        Pid when is_pid(Pid) ->
            {ok, Pid}
    end.

delete_host(UUID) when is_binary(UUID) ->
    Id = iot_host:get_name(UUID),
    ok = supervisor:terminate_child(?MODULE, Id),
    supervisor:delete_child(?MODULE, Id).

child_spec(UUID) ->
    Id = iot_host:get_name(UUID),
    #{id => Id,
        start => {iot_host, start_link, [Id, UUID]},
        restart => permanent,
        shutdown => 2000,
        type => worker,
        modules => ['iot_host']}.