%%%-------------------------------------------------------------------
%%% @author anlicheng
%%% @copyright (C) 2025, <COMPANY>
%%% @doc
%%%
%%% @end
%%% Created : 17. 11月 2025 16:48
%%%-------------------------------------------------------------------
-module(endpoint_mqtt_subscriber).
-author("anlicheng").

-author("aresei").
-include("iot.hrl").

-behaviour(gen_server).

%% API
-export([start_link/0]).

%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).

-define(SERVER, ?MODULE).

%% 需要订阅的主题信息
-define(Topics,[
    {<<"/dhlr/data">>, 0}
]).

-record(state, {
    conn_pid :: pid()
}).

%%%===================================================================
%%% API
%%%===================================================================

%% @doc Spawns the server and registers the local name (unique)
-spec(start_link() ->
    {ok, Pid :: pid()} | ignore | {error, Reason :: term()}).
start_link() ->
    gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).

%%%===================================================================
%%% gen_server callbacks
%%%===================================================================

%% @private
%% @doc Initializes the server
-spec(init(Args :: term()) ->
    {ok, State :: #state{}} | {ok, State :: #state{}, timeout() | hibernate} |
    {stop, Reason :: term()} | ignore).
init([]) ->
    %% 建立到emqx服务器的连接
    ClientId = <<"mqtt-client-test-host-subscriber">>,
    Opts = [
        {clientid, ClientId},
        {host, "118.178.229.213"},
        {port, 1883},
        {owner, self()},
        {tcp_opts, []},
        {username, "admin"},
        {password, "admin"},
        {keepalive, 86400},
        {auto_ack, true},
        {proto_ver, v5},
        {retry_interval, 5}
    ],

    logger:debug("[opts] is: ~p", [Opts]),
    case emqtt:start_link(Opts) of
        {ok, ConnPid} ->
            %% 监听和host相关的全部事件
            logger:debug("[iot_mqtt_subscriber] start conntecting, pid: ~p", [ConnPid]),
            {ok, _} = emqtt:connect(ConnPid),
            logger:debug("[iot_mqtt_subscriber] connect success, pid: ~p", [ConnPid]),
            SubscribeResult = emqtt:subscribe(ConnPid, ?Topics),

            logger:debug("[iot_mqtt_subscriber] subscribe topics: ~p, result is: ~p", [?Topics, SubscribeResult]),

            {ok, #state{conn_pid = ConnPid}};
        ignore ->
            logger:debug("[iot_mqtt_subscriber] connect emqx get ignore"),
            {stop, ignore};
        {error, Reason} ->
            logger:debug("[iot_mqtt_subscriber] connect emqx get error: ~p", [Reason]),
            {stop, Reason}
    end.

%% @private
%% @doc Handling call messages
-spec(handle_call(Request :: term(), From :: {pid(), Tag :: term()},
    State :: #state{}) ->
    {reply, Reply :: term(), NewState :: #state{}} |
    {reply, Reply :: term(), NewState :: #state{}, timeout() | hibernate} |
    {noreply, NewState :: #state{}} |
    {noreply, NewState :: #state{}, timeout() | hibernate} |
    {stop, Reason :: term(), Reply :: term(), NewState :: #state{}} |
    {stop, Reason :: term(), NewState :: #state{}}).
handle_call(_Info, _From, State = #state{conn_pid = _ConnPid}) ->
    {reply, ok, State}.

%% @private
%% @doc Handling cast messages
-spec(handle_cast(Request :: term(), State :: #state{}) ->
    {noreply, NewState :: #state{}} |
    {noreply, NewState :: #state{}, timeout() | hibernate} |
    {stop, Reason :: term(), NewState :: #state{}}).
handle_cast(_Request, State = #state{}) ->
    {noreply, State}.

%% @private
%% @doc Handling all non call/cast messages
-spec(handle_info(Info :: timeout() | term(), State :: #state{}) ->
    {noreply, NewState :: #state{}} |
    {noreply, NewState :: #state{}, timeout() | hibernate} |
    {stop, Reason :: term(), NewState :: #state{}}).
handle_info({disconnect, ReasonCode, Properties}, State = #state{}) ->
    logger:debug("[iot_mqtt_subscriber] Recv a DISONNECT packet - ReasonCode: ~p, Properties: ~p", [ReasonCode, Properties]),
    {stop, disconnected, State};
%% 必须要做到消息的快速分发，数据的json反序列需要在host进程进行
handle_info({publish, #{packet_id := _PacketId, payload := Payload, qos := Qos, topic := Topic}}, State = #state{conn_pid = _ConnPid}) ->
    logger:debug("[iot_mqtt_subscriber] Recv a topic: ~p, publish packet: ~p, qos: ~p", [Topic, Payload, Qos]),
    %% 将消息分发到对应的host进程去处理
    {noreply, State};
handle_info({puback, Packet = #{packet_id := _PacketId}}, State = #state{}) ->
    logger:debug("[iot_mqtt_subscriber] receive puback packet: ~p", [Packet]),
    {noreply, State};

handle_info(Info, State = #state{}) ->
    logger:debug("[iot_mqtt_subscriber] get info: ~p", [Info]),
    {noreply, State}.

%% @private
%% @doc This function is called by a gen_server when it is about to
%% terminate. It should be the opposite of Module:init/1 and do any
%% necessary cleaning up. When it returns, the gen_server terminates
%% with Reason. The return value is ignored.
-spec(terminate(Reason :: (normal | shutdown | {shutdown, term()} | term()),
    State :: #state{}) -> term()).
terminate(Reason, _State = #state{conn_pid = ConnPid}) when is_pid(ConnPid) ->
    %% 取消topic的订阅
    TopicNames = lists:map(fun({Name, _}) -> Name end, ?Topics),
    {ok, _Props, _ReasonCode} = emqtt:unsubscribe(ConnPid, #{}, TopicNames),

    ok = emqtt:disconnect(ConnPid),
    logger:debug("[iot_mqtt_subscriber] terminate with reason: ~p", [Reason]),
    ok;
terminate(Reason, _State) ->
    logger:debug("[iot_mqtt_subscriber] terminate with reason: ~p", [Reason]),
    ok.

%% @private
%% @doc Convert process state when code is changed
-spec(code_change(OldVsn :: term() | {down, term()}, State :: #state{},
    Extra :: term()) ->
    {ok, NewState :: #state{}} | {error, Reason :: term()}).
code_change(_OldVsn, State = #state{}, _Extra) ->
    {ok, State}.

%%%===================================================================
%%% Internal functions
%%%===================================================================