%%-----------------------------------------------------------------------------
%% module  :       mod_lucky_bag_ctrl
%% @author  :      zmh
%% Created :       2020-10-19
%% description:    福袋红包传闻用
%%-----------------------------------------------------------------------------
-module(mod_lucky_bag_ctrl).

-include("errcode.hrl").
-include("common.hrl").
-include("def_module.hrl").
-include("predefine.hrl").
-include("custom_act.hrl").

-export ([init/1, handle_call/3, handle_cast/2, handle_info/2, code_change/3, terminate/2]).
-export ([
    start_link/0,
    reload_config/0
]).

%% API
start_link() ->
    gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).

reload_config()->
    gen_server:cast(?MODULE,{reload_config}).

init([]) ->
    Types = [{?CUSTOM_ACT_TYPE_LUCKY_BAG1,1},{?CUSTOM_ACT_TYPE_LUCKY_BAG2,1}],
    Now = utime:unixtime(),
    UnixDate = utime:unixdate(Now),
    RefMap = calc_act_ref(Types,Now,UnixDate,#{}),
    {ok,RefMap}.

handle_call(Msg, From, State) ->
    case catch do_handle_call(Msg, From, State) of
        {'EXIT', Error} ->
            ?ERR("handle_call error: ~p~nMsg=~p~n", [Error, Msg]),
            {reply, error, State};
        Return  ->
            Return
    end.

handle_cast(Msg, State) ->
    case catch do_handle_cast(Msg, State) of
        {'EXIT', Error} ->
            ?ERR("handle_cast error: ~p~nMsg=~p~n", [Error, Msg]),
            {noreply, State};
        Return  ->
            Return
    end.

handle_info(Info, State) ->
    case catch do_handle_info(Info, State) of
        {'EXIT', Error} ->
            ?ERR("handle_info error: ~p~nInfo=~p~n", [Error, Info]),
            {noreply, State};
        Return  ->
            Return
    end.

code_change(_OldVsn, State, _Extra) ->
    {ok, State}.

terminate(_Reason, _State) ->
    ?ERR("~p is terminate:~p", [?MODULE, _Reason]),
    ok.

do_handle_call(_Msg, _From, State) ->
    {reply, ok, State}.


%%配置重新加载
do_handle_cast({reload_config},State)->
    F = fun(_, Ref, _Sum) -> util:cancel_timer(Ref) end,
    maps:fold(F, 0, State),
    Types = [{?CUSTOM_ACT_TYPE_LUCKY_BAG1,1},{?CUSTOM_ACT_TYPE_LUCKY_BAG2,1}],
    Now = utime:unixtime(),
    UnixDate = utime:unixdate(Now),
    RefMap = calc_act_ref(Types,Now,UnixDate,#{}),
    {noreply,RefMap};

do_handle_cast(_Msg, State) ->
    {noreply, State}.

do_handle_info({Msg,Type,SubType},State)->
    Types = [{Type,SubType}],
    Now = utime:unixtime(),
    UnixDate = utime:unixdate(Now),
    NState = calc_act_ref(Types,Now,UnixDate,State),
    case lib_custom_act_api:is_open_act(Type, SubType) of
        true->
            brocast_tv(Msg,Type,SubType);
        _->
            skip
    end,
    {noreply,NState};

do_handle_info(_Msg, State) ->
    {noreply, State}.


calc_act_ref([],_Now,_UnixDate,RefMap)->
    RefMap;
calc_act_ref([{Type,SubType}|T],Now,UnixDate,RefMap)->
    ActCfg = lib_custom_act_util:get_act_cfg_info(Type,SubType),
    #custom_act_cfg{condition=Condition}=ActCfg,
    case lists:keyfind(open_time,1,Condition) of
        {_,OpTime}->
            skip;
        _->
            OpTime = []
    end,
    case lists:keyfind(last_time,1,Condition) of
        {_,CTime}->
            skip;
        _->
            CTime = 0
    end,
    case lists:keyfind(show_time,1,Condition) of
        {_,PTime}->
            skip;
        _->
            PTime = 0
    end,
    {Msg,Remain} = calc_state(OpTime,UnixDate,CTime,PTime,Now),
    Ref = erlang:send_after(Remain*1000,self(),{Msg,Type,SubType}),
    calc_act_ref(T,Now,UnixDate,maps:put({Type,SubType},Ref,RefMap)).


calc_state([],_,_,_,_)->
    Remain = max(86400-utime:get_seconds_from_midnight(),10),
    {enter_ldle,Remain};
calc_state([{H,M}|T],UnixDate,CTime,PTime,Now)->
    STime = UnixDate+H*3600+60*M,
    PreTime = STime-PTime,
    ETime = STime+CTime,
    case Now>=ETime of
        true->
            calc_state(T,UnixDate,CTime,PTime,Now);
        _->
            case Now>=STime of
                true->
                    {enter_close,ETime-Now};
                _->
                    case Now>=PreTime of
                        true->
                            {enter_start,STime-Now};
                        _->
                            {enter_pre_start,PreTime-Now}
                    end
            end
    end.

brocast_tv(enter_pre_start,?CUSTOM_ACT_TYPE_LUCKY_BAG1,_SubType)->
    lib_chat:send_TV({all}, ?MOD_AC_CUSTOM, 8, []);
brocast_tv(enter_pre_start,?CUSTOM_ACT_TYPE_LUCKY_BAG2,_SubType)->
    lib_chat:send_TV({all}, ?MOD_AC_CUSTOM, 25, []);
brocast_tv(enter_start,?CUSTOM_ACT_TYPE_LUCKY_BAG1,_SubType)->
    lib_chat:send_TV({all}, ?MOD_AC_CUSTOM, 9, []);
brocast_tv(enter_start,?CUSTOM_ACT_TYPE_LUCKY_BAG2,_SubType)->
    lib_chat:send_TV({all}, ?MOD_AC_CUSTOM, 26, []);
brocast_tv(enter_close,?CUSTOM_ACT_TYPE_LUCKY_BAG1,_SubType)->
    lib_chat:send_TV({all}, ?MOD_AC_CUSTOM, 11, []);
brocast_tv(enter_close,?CUSTOM_ACT_TYPE_LUCKY_BAG2,_SubType)->
    lib_chat:send_TV({all}, ?MOD_AC_CUSTOM, 28, []);
brocast_tv(_,_,_)->ok.