%%%-------------------------------------------------------------------
%% @doc iot top level supervisor.
%% @end
%%%-------------------------------------------------------------------

-module(iot_sup).

-behaviour(supervisor).

-export([start_link/0]).

-export([init/1]).

-define(SERVER, ?MODULE).

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

%% sup_flags() = #{strategy => strategy(),         % optional
%%                 intensity => non_neg_integer(), % optional
%%                 period => pos_integer()}        % optional
%% child_spec() = #{id => child_id(),       % mandatory
%%                  start => mfargs(),      % mandatory
%%                  restart => restart(),   % optional
%%                  shutdown => shutdown(), % optional
%%                  type => worker(),       % optional
%%                  modules => modules()}   % optional
init([]) ->
    SupFlags = #{strategy => one_for_one, intensity => 1000, period => 3600},
    Specs = [
        #{
            id => 'iot_task',
            start => {'iot_task', start_link, []},
            restart => permanent,
            shutdown => 2000,
            type => worker,
            modules => ['iot_task']
        },

        #{
            id => 'iot_event_period_settings',
            start => {'iot_event_period_settings', start_link, []},
            restart => permanent,
            shutdown => 2000,
            type => worker,
            modules => ['iot_event_period_settings']
        },

        #{
            id => 'iot_device_sup',
            start => {'iot_device_sup', start_link, []},
            restart => permanent,
            shutdown => 2000,
            type => supervisor,
            modules => ['iot_device_sup']
        },

        #{
            id => 'iot_host_sup',
            start => {'iot_host_sup', start_link, []},
            restart => permanent,
            shutdown => 2000,
            type => supervisor,
            modules => ['iot_host_sup']
        },

        #{
            id => 'iot_zd_endpoint',
            start => {'iot_zd_endpoint', start_link, []},
            restart => permanent,
            shutdown => 2000,
            type => worker,
            modules => ['iot_zd_endpoint']
        },

        #{
            id => 'iot_zd_consumer',
            start => {'iot_zd_consumer', start_link, []},
            restart => permanent,
            shutdown => 2000,
            type => worker,
            modules => ['iot_zd_consumer']
        },

        #{
            id => 'iot_jinzhi_endpoint',
            start => {'iot_jinzhi_endpoint', start_link, []},
            restart => permanent,
            shutdown => 2000,
            type => worker,
            modules => ['iot_jinzhi_endpoint']
        }
    ],

    {ok, {SupFlags,  pools() ++ Specs}}.

%% internal functions

pools() ->
    {ok, Pools} = application:get_env(iot, pools),
    lists:map(fun({Name, PoolArgs, WorkerArgs}) ->
            poolboy:child_spec(Name, [{name, {local, Name}}|PoolArgs], WorkerArgs)
        end, Pools).