%%%-------------------------------------------------------------------
%%% @author liushl
%%% @doc
%%%     离线托管
%%% @end
%%%-------------------------------------------------------------------
-module(pp_offline_hosting).
-author("liushl").

-include("server.hrl").
-include("offline_hosting.hrl").

%% API
-export([handle/3]).

%% 离线托管: 信息
handle(Cmd = 11300, Player, []) ->
    #player_status{
        sid = Sid,
        id = RoleId
    } = Player,
    #ets_offline_hosting{
        value = Value,
        close_list = CloseList
    } = offline_hosting_data:host_setting_lookup(RoleId),
    DailyValue = offline_hosting_data:host_daily_value_lookup(RoleId),
    {ok, Bin} = pt_113:write(Cmd, [CloseList, Value, DailyValue]),
    lib_server_send:send_to_sid(Sid, Bin),
    ok;

%% 离线托管: 托管日志
handle(Cmd = 11301, Player, []) ->
    #player_status{
        sid = Sid,
        offline_hosting = #offline_hosting{
            log = Log
        }
    } = Player,
    Info =
    [
        {Time, Duration, Mod, Sub, CostValue}
        ||#offline_hosting_log{
                key = {Mod, Sub}
                , duration = Duration
                , cost_value = CostValue
                , time = Time
            } <- Log
    ],
    {ok, Bin} = pt_113:write(Cmd, [Info]),
    lib_server_send:send_to_sid(Sid, Bin),
    ok;

%% 离线托管: 上线推送托管记录
handle(Cmd = 11302, Player, []) ->
    #player_status{
        sid = Sid,
        last_logout_time = LastLogoutTime,
        offline_hosting = #offline_hosting{
            log = Log,
            ask_log_time = AskTime
        } = Offline
    } = Player,
    case AskTime > 0 of
        true  ->
            %% 当次在线期间请求过一次，就不在发送了
            ok;
        false ->
            Info =
                [
                    {Time, Duration, Mod, Sub, CostValue, Award}
                    ||#offline_hosting_log{
                            key = {Mod, Sub}
                            , duration = Duration
                            , cost_value = CostValue
                            , time = Time
                            , award = Award
                        } <- divide_by_time(Log, LastLogoutTime, [])
                ],
            {ok, Bin} = pt_113:write(Cmd, [Info]),
            lib_server_send:send_to_sid(Sid, Bin),
            NewOffline = Offline#offline_hosting{ask_log_time = utime:unixtime()},
            {ok, Player#player_status{offline_hosting = NewOffline}}
    end;

%% 离线托管: 开启关闭
handle(Cmd = 11303, Player, [Mod, Sub, Status]) when Status == 1 orelse Status == 0 ->
    #player_status{
        sid = Sid,
        id = RoleId
    } = Player,
    Cfg = data_offline_hosting:get_offline_hosting_value(Mod, Sub),
    Key = {Mod, Sub},
    #ets_offline_hosting{close_list = CloseList} =
        Setting = offline_hosting_data:host_setting_lookup(RoleId),
    IsSetClose = lists:member(Key, CloseList),
    if
        Cfg == 0 -> ok;
        Status =:= 0 andalso IsSetClose -> ok;
        Status =:= 1 andalso not IsSetClose -> ok;
        true ->
            case Status of
                1  -> NewSetting = Setting#ets_offline_hosting{close_list = CloseList -- [Key]};
                0  -> NewSetting = Setting#ets_offline_hosting{close_list = [Key|CloseList]}
            end,
            offline_hosting_data:host_save_setting(NewSetting),
            {ok, Bin} = pt_113:write(Cmd, [Mod, Sub, Status]),
            lib_server_send:send_to_sid(Sid, Bin),
            ok
    end;

handle(Cmd, _Player, Data) ->
    erlang:display({"[LIUSHL]", erlang:localtime(), ?MODULE, ?LINE, Cmd, Data}),
    ok.

divide_by_time([H|Log], Time, Return) ->
    case H#offline_hosting_log.time > Time of
        true  -> divide_by_time(Log, Time, [H|Return]);
        false -> Return
    end;
divide_by_time([], _Time, Return) -> Return.