%% ---------------------------------------------------------------------------
%% @doc mod_pushmail
%% @author xiaoxiang
%% @since  2017-03-01
%% @deprecated  pushmail
%% ---------------------------------------------------------------------------
-module(mod_pushmail).

-behaviour(gen_statem).

%% gen_statem callbacks
-export([
        init/1,
        callback_mode/0,
        terminate/3,
        code_change/4,

        % StateName
        wait/3
    ]).
%% API
-export([
        start_link/0
        ,stop/0
        ,timer_check/0
        ,open_day/0
        ,open_day/1
        ,merge_day/0
        ,merge_day/1

        ,send_push_mail_pro/1
    ]).
% -compile(export_all).

-include("common.hrl").
-include("def_event.hrl").
-include("server.hrl").
-include("pushmail.hrl").
-include("predefine.hrl").

start_link() ->
    gen_statem:start_link({global, ?MODULE}, ?MODULE, [], []).

stop() ->
    gen_statem:cast(misc:get_global_pid(?MODULE), stop).

timer_check() ->
    misc:get_global_pid(?MODULE) ! 'timer_check'.
open_day() ->
    misc:get_global_pid(?MODULE) ! 'open_day'.

merge_day() ->
    misc:get_global_pid(?MODULE) ! 'marge_day'.

open_day(Delay) ->
    misc:get_global_pid(?MODULE) ! {'open_day', Delay}.

merge_day(Delay) ->
    misc:get_global_pid(?MODULE) ! {'marge_day', Delay}.


%% --------------------------------------------------------
%% gen_statem callbacks
%% --------------------------------------------------------
init([]) ->
    {ok, wait, #push_state{}}.

callback_mode() ->
    [state_functions].

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

terminate(_Any, _StateName, _Opts) ->
    ok.

wait(info, 'timer_check', State) ->
    NewState = do_timer_check(State),
    {next_state, wait, NewState};

wait(info, 'open_day', State) ->
    NewState = do_open_day(State),
    {next_state, wait, NewState};

wait(info, {'open_day', Delay}, State) ->
    spawn(fun() -> util:multiserver_delay(Delay, ?MODULE, open_day, []) end),
    {next_state, wait, State};

wait(info, {'marge_day', Delay}, State) ->
    spawn(fun() -> util:multiserver_delay(Delay, ?MODULE, marge_day, []) end),
    {next_state, wait, State};

wait(info, Event, State) ->
    case Event of
        'time' ->
            NewState = do_timer_check(State),
            {next_state, wait, NewState};
        'server' ->
            NewState = do_open_day(State),
            {next_state, wait, NewState};
        'marge' ->
            NewState = do_merge_day(State),
            {next_state, wait, NewState};
        _ ->
            {next_state, wait, State}
    end;

wait(cast, stop, State) ->
    {stop, normal, State};

wait(_EventType, _EventContent, State) ->
    {next_state, wait, State}.


do_timer_check(State) ->
    {{NowY, NowM, NowD}, {NowH, NowMin, _}} = calendar:local_time(),
    IdList = data_pushmail:get_id_list(),
    F = fun(Id) ->
        #base_pushmail{time = Time} = data_pushmail:get_pushmail_config(Id),
        case Time of
            [{Y, M, D}, {H, Min}] ->
                case Y==NowY andalso M == NowM andalso D==NowD andalso H==NowH andalso Min==NowMin of
                    true ->
                        ?PRINT("------------------Id:~p ~n", [Id]),
                        RefreshList = ets:tab2list(?ETS_ONLINE),
                        RoleIdList = [PlayerId||#ets_online{id = PlayerId}<-RefreshList],
                        send_push_mail(RoleIdList);
                    _ ->
                        skip
                end;
            _ ->
                false
        end
    end,
    lists:foreach(F, IdList),
    State.

do_open_day(State) ->
    Now = util:get_open_day(),
    IdList = data_pushmail:get_id_list(),
    F = fun(Id) ->
        #base_pushmail{open_day = OpenDay} = data_pushmail:get_pushmail_config(Id),
        case Now == OpenDay of
            true ->
                RefreshList = ets:tab2list(?ETS_ONLINE),
                RoleIdList = [PlayerId||#ets_online{id = PlayerId}<-RefreshList],
                send_push_mail(RoleIdList);
            _ ->
                false
        end
    end,
    lists:foreach(F, IdList),
    State.


do_merge_day(State) ->
    Now = util:get_merge_day(),
    IdList = data_pushmail:get_id_list(),
    F = fun(Id) ->
        #base_pushmail{merge_day = MargeDay} = data_pushmail:get_pushmail_config(Id),
        case Now == MargeDay of
            true ->
                RefreshList = ets:tab2list(?ETS_ONLINE),
                RoleIdList = [PlayerId||#ets_online{id = PlayerId}<-RefreshList],
                send_push_mail(RoleIdList);
            _ ->
                false
        end
    end,
    lists:foreach(F, IdList),
    State.

send_push_mail(RoleIdList) ->
    spawn(?MODULE, send_push_mail_pro, [RoleIdList]).

send_push_mail_pro(RoleIdList) ->
    [begin
        lib_player:apply_cast(RoleId, ?APPLY_CAST_SAVE, lib_pushmail, pushmail, []),
        timer:sleep(50)
    end||RoleId<-RoleIdList].
