% date  :   2021-7-27
% desc  :   货币祈愿协议入口
% author:   JiangXun

-module(pp_currencywish).

-include("server.hrl").
-include("currencywish.hrl").
-include("errcode.hrl").
-include("def_module.hrl").

-export([handle/3]).

% 查看信息
handle(34001, #player_status{id = RoleId} = PlayerState, []) -> 
    % 豪华祈愿
    LuxuryCount = lib_currencywish:cal_costcount(RoleId, ?MOD_CURRENCYWISH, ?COUNTER_LUXURY_DAILY_TIMES),
    KeyConvert1 = list_to_atom(lists:concat([?EXCHANGE_TIMES, ?LUXURY_WISH])),
    [MaxLuxuryC] = data_currencywish:get_currencywish_kv(KeyConvert1),
    LuxuryResC = MaxLuxuryC - LuxuryCount,
    
    {_, LuxuryCDRest} = lib_currencywish:check({cdlimit, ?LUXURY_WISH, PlayerState}), 

    % 经验祈愿
    ExperienceCount = lib_currencywish:cal_costcount(RoleId, ?MOD_CURRENCYWISH, ?COUNTER_EXPERIENCE_DAILY_TIMES),
    KeyConvert2 = list_to_atom(lists:concat([?EXCHANGE_TIMES, ?EXPERIENCE_WISH])),
    [MaxExoerienceC] = data_currencywish:get_currencywish_kv(KeyConvert2),
    ExperienceResC = MaxExoerienceC - ExperienceCount,

    % {_, ExperienceCDRest} = lib_currencywish:check({cdlimit, ?EXPERIENCE_WISH, PlayerState}), 
    {ok, Bin} = pt_340:write(34001, [LuxuryResC, LuxuryCDRest, ExperienceResC]),
    lib_server_send:send_to_uid(RoleId, Bin),
    ok;




% 祈愿
handle(34002, #player_status{id = RoleId} = PlayerState, [WishType]) when is_integer(WishType) ->

    CKList = [{upper_limit, WishType, PlayerState}],
    {Res, NPS} = 
    case lib_currencywish:check_list(CKList) of 
        false ->
            {?ERRCODE(err340_check_currencywish_fail), PlayerState};
        true ->
            case lib_currencywish:do_consumegoods_to_wish(WishType, PlayerState) of
                {false, ERR} ->
                    {?ERRCODE(ERR), PlayerState};
                {false, ERR, _, _} ->
                    {?ERRCODE(ERR), PlayerState};
                {ok, PS1, CritRate, GetNum} ->
                    % 把数据发回给客户端
                    {ok, Bin} = pt_340:write(34002,[CritRate, GetNum, WishType]),
                    lib_server_send:send_to_uid(RoleId, Bin),
                    {?SUCCESS, PS1}
            end
                    
    end,
    {ok, Bin1} = pt_340:write(34000, [Res]),
    lib_server_send:send_to_uid(RoleId, Bin1),
    {ok, NPS};



handle(_Code, _PlayerState, _Arg) ->
    {error, "pp_currencywish no match"}.
