%%% -------------------------------------------------------
%%% @author huangyongxing@yeah
%%% @doc
%%% 珍宝阁（留存抽奖）
%%% @end
%%% -------------------------------------------------------
-module(pp_treasure_house).
-export([
        handle/3
    ]).
-include("server.hrl").
-include("common.hrl").
-include("errcode.hrl").

%% 珍宝阁综合信息查询
handle(10400 = Cmd, Player, []) ->
    handle_general_result(Cmd, Player, [], fun treasure_house:query_all_info/1);

%% 抽取奖励
handle(10401 = Cmd, Player, [Count]) ->
    handle_general_result(Cmd, Player, Count, fun treasure_house:draw/2);

%% 领取次数保底奖励
handle(10402 = Cmd, Player, [Count]) ->
    handle_general_result(Cmd, Player, Count, fun treasure_house:recv_count_award/2);

%% 抽奖券任务进度
handle(10403 = Cmd, Player, []) ->
    handle_general_result(Cmd, Player, [], fun treasure_house:query_task_progress/1);

%% 抽奖券任务领取奖励
handle(10404 = Cmd, Player, [TaskId]) ->
    handle_general_result(Cmd, Player, TaskId, fun treasure_house:recv_task_award/2);

%% 抽奖券购买
handle(10405 = Cmd, Player, [TaskId]) ->
    handle_general_result(Cmd, Player, TaskId, fun treasure_house:buy_ticket/2);

%% 抽奖传闻记录查询
handle(10406, #player_status{sid = RoleSid}, []) ->
    treasure_house_mgr:query_award_record(RoleSid),
    ok;

handle(_Cmd, _Player, _Data) ->
    {error, {badmatch, _Cmd}}.

handle_general_result(Cmd, Player = #player_status{sid = Sid}, Params, Fun) ->
    Result =
    if
        is_function(Fun, 1) -> Fun(Player);
        is_function(Fun, 2) -> Fun(Player, Params);
        true -> error
    end,
    case Result of
        {ok, Data, NewPlayer} ->
            {ok, BinData} = pt_104:write(Cmd, Data),
            % ?INFO("Data:~w, send ~w", [Data, BinData]),
            lib_server_send:send_to_sid(Sid, BinData),
            {ok, NewPlayer};
        {ok, Data} ->
            {ok, BinData} = pt_104:write(Cmd, Data),
            % ?INFO("Data:~w, send ~w", [Data, BinData]),
            lib_server_send:send_to_sid(Sid, BinData),
            ok;
        _Other ->
            ignore
    end.

