%%%-------------------------------------------------------------------
%%% @author liushl
%%% @doc
%%%     交友大厅
%%% @end
%%%-------------------------------------------------------------------
-module(mate_hall).
-author("liushl").

-export([handle/2]).

-include("mate.hrl").
-include("role.hrl").
-include("figure.hrl").

%% 查看交友大厅
handle({ask_friendship_hall, RoleId}, State) ->
    List = pack_hall_role(State#mate_mgr.mate_hall, RoleId),
    lib_server_send:send_to_uid(RoleId, pt_149, 14942, [List]),
    State;
%% 发布交友信息
handle({release, RoleInfo}, State) ->
    #mate_mgr{mate_hall = Hall, mate_hall_ref = OldTimer} = State,
    NewHall = case lists:keyfind(RoleInfo#mate_hall_role.id, #mate_hall_role.id, Hall) of
                  false ->
                      [RoleInfo|Hall];
                  _OldOne ->
                      [RoleInfo|lists:keydelete(RoleInfo#mate_hall_role.id, #mate_hall_role.id, Hall)]
              end,
    db_save_release(RoleInfo),
    ShowTime = data_mate:get_mate_key(hall_show_time),
    NewTimer =
        case is_reference(OldTimer) of
            true ->
                OldTimer;
            false ->
                erlang:send_after(ShowTime*1000, self(), {mate_hall, check_overdue})
        end,
    State#mate_mgr{mate_hall = NewHall, mate_hall_ref = NewTimer};
%% 交友信息发布过期
handle(check_overdue, State) ->
    #mate_mgr{mate_hall = Hall, mate_hall_ref = OldTimer} = State,
    util:cancel_timer(OldTimer),
    NowTime = utime:unixtime(),
    ShowTime = data_mate:get_mate_key(hall_show_time),
    OkTime = NowTime - ShowTime,
    %% 按照时间从小到达排序, 前面的为过期的，后面的是OK的
    SortHall = lists:keysort(#mate_hall_role.time, Hall),
    FilterOverdue = filter_overdue(SortHall, OkTime),
    FilterOverdue =/= SortHall andalso db_del_release(OkTime),
    case FilterOverdue of
        [#mate_hall_role{time = UpTime} | _] ->
            TimeOut = UpTime + ShowTime - NowTime,
            Timer = erlang:send_after(TimeOut*1000, self(), {mate_hall, check_overdue}),
            State#mate_mgr{
                mate_hall = FilterOverdue,
                mate_hall_ref = Timer
            };
        _ ->
            State#mate_mgr{mate_hall = [], mate_hall_ref = undefined}
    end;
%% 移除发布的信息
handle({reomve_release, RoleId}, State) ->
    #mate_mgr{mate_hall = Hall} = State,
    NewHall = lists:keydelete(RoleId, #mate_hall_role.id, Hall),
    db:execute(io_lib:format(?SQL_MATE_REMOVE_HALL, [RoleId])),
    State#mate_mgr{mate_hall = NewHall};

handle(_Msg, State) ->
    State.

%% 打包交友大厅数据
pack_hall_role(Hall, RoleId) ->
    Max = 100,
    %% 既然限制了长度，就把自己拎出来
    case lists:keytake(RoleId, #mate_hall_role.id, Hall) of
        {value, Value, Left} ->
            pack_hall_role([Value|Left], Max, []);
        false ->
            pack_hall_role(Hall, Max, [])
    end.
pack_hall_role([], _, List) -> List;
pack_hall_role(_Hall, 0, List) -> List;
pack_hall_role([One|T], Index, List) ->
    #mate_hall_role{id = RoleId, declaration = Msg, type = Type, time = Time} = One,
    #ets_role_show{figure = Figure, mate_title = MateTitle} = lib_role:get_role_show(RoleId),
    Picture = lib_player:trans_to_picture(Figure),
    MateNum = erlang:length(MateTitle),
    NewList = [{Picture, [], Msg, MateNum, Type, Time}|List],
    pack_hall_role(T, Index - 1, NewList).

%% 过滤掉过期的数据
filter_overdue([One|T], CheckTime) ->
    case One#mate_hall_role.time =< CheckTime of
        true  ->
            filter_overdue(T, CheckTime);
        false ->
            [One|T]
    end;
filter_overdue([], _) -> [].

db_save_release(RoleInfo) ->
    SQL = io_lib:format(?SQL_MATE_SAVE_HALL, [RoleInfo#mate_hall_role.id, RoleInfo#mate_hall_role.type, RoleInfo#mate_hall_role.declaration, RoleInfo#mate_hall_role.time]),
    db:execute(SQL),
    ok.

db_del_release(Time) ->
    catch db:execute(io_lib:format(?SQL_MATE_DEL_HALL, [Time])),
    ok.