%%-----------------------------------------------------------------------------
%% @Module  :       pp_investment.erl
%% @Author  :       J
%% @Email   :       j-som@foxmail.com
%% @Created :       2017-12-18
%% @Description:    投资活动
%%-----------------------------------------------------------------------------

-module (pp_investment).
-export ([handle/3]).
-include ("server.hrl").
-include ("investment.hrl").
-include ("common.hrl").
-include ("errcode.hrl").
-include ("figure.hrl").
-include ("def_module.hrl").
-include ("red_envelopes.hrl").

%% 投资信息
handle(42001, PS, []) ->
    Info = lib_investment:get_investment_info(PS),
    {ok, BinData} = pt_420:write(42001, [Info]),
    lib_server_send:send_to_sid(PS#player_status.sid, BinData),
    ok;

%% 投资
handle(42002, Player, [Type]) ->
    case lib_investment:investment(Player, Type) of
        {false, Error} ->
            send_error(Player#player_status.sid, Error),
            ok;
        {true, NewPlayer} ->
            handle(42001, NewPlayer, []),
            {true, NewPlayer}
    end;

%% 投资领奖
handle(42003, PS, [Type, RewardId]) ->
    case lib_investment:send_reward(PS, Type, RewardId) of
        {false, Error} ->
            send_error(PS#player_status.sid, Error),
            ok;
        {true, NewPlayer, Update} ->
            {ok, Bin} = pt_420:write(42003, [Type, RewardId]),
            lib_server_send:send_to_sid(PS#player_status.sid, Bin),
            Update andalso handle(42001, NewPlayer, []),
            {true, NewPlayer}
    end;

handle(CMD, _PS, _Args) ->
    ?ERR("pp_investment no match ~p, ~p~n", [CMD, _Args]).

send_error(Sid, Code) ->
    {CodeInt, CodeArgs} = util:parse_error_code(Code),
    {ok, BinData} = pt_420:write(42000, [CodeInt, CodeArgs]),
    lib_server_send:send_to_sid(Sid, BinData).
