%%-----------------------------------------------------------------------------
%% @Module  :       pp_red_envelopes
%% @Author  :       Czc
%% @Email   :       389853407@qq.com
%% @Created :       2017-09-20
%% @Description:    公会红包
%%-----------------------------------------------------------------------------
-module(pp_red_envelopes).

-include("server.hrl").
-include("errcode.hrl").
-include("red_envelopes.hrl").
-include("common.hrl").
-include("guild.hrl").
-include("figure.hrl").
-include("def_module.hrl").
-include("def_fun.hrl").

-compile([export_all]).

%% 公会红包列表
handle(33901, Player, []) ->
    #player_status{sid = Sid, id = RoleId, guild = GuildStatus} = Player,
    #status_guild{id = GuildId} = GuildStatus,
    case GuildId =/= 0 of
        true ->
            mod_red_envelopes:send_red_envelopes_list(?GUILD_RED_ENVELOPES, GuildId, RoleId);
        _ ->
            lib_red_envelopes:send_error_code(Sid, ?ERRCODE(err400_not_join_guild))
    end;

%% 打开红包
handle(33902, Player, [RedEnvelopesId]) ->
    #player_status{sid = Sid, id = RoleId, guild = GuildStatus} = Player,
    #status_guild{id = GuildId, red_packet=RedPacket, red_gold_packet = GoldRedPacket} = GuildStatus,
    case GuildId =/= 0 of
        true ->
            mod_red_envelopes:open_red_envelopes(?GUILD_RED_ENVELOPES, GuildId, RoleId, RedEnvelopesId, {RedPacket, GoldRedPacket});
        _ ->
            lib_red_envelopes:send_error_code(Sid, ?ERRCODE(err400_not_join_guild))
    end;


%% vip发送红包
handle(33903, Player, [PacketId, Greet]) ->
    #player_status{sid = Sid, id = RoleId, figure = Figure, guild = GuildStatus, gold = Gold} = Player,
    #status_guild{id = GuildId, lv = GuildLv} = GuildStatus,
    #figure{vip = VipLv} = Figure,
    Cfg = data_red_envelopes:get_vip(PacketId),
    EnvelopesCfg = ?IF(is_record(Cfg, base_vip_red_envelopes_cfg), Cfg, #base_vip_red_envelopes_cfg{}),
    #base_vip_red_envelopes_cfg{money = Money, num = SplitNum, min_vip = MinVip} = EnvelopesCfg,
    %%敏感词
    {MinL, MaxL} = data_guild:get(red_packet_len),
    LengthCheck = util:check_length(Greet, MinL, MaxL),
    KeyWordCheck = lib_word:check_keyword(Greet),
    if
        GuildId == 0 -> NewPlayer = Player, ErrCode = ?ERRCODE(err339_not_join_guild);
        is_record(Cfg, base_vip_red_envelopes_cfg) == false -> NewPlayer = Player, ErrCode = ?ERRCODE(err339_not_exist);
        MinVip > VipLv -> NewPlayer = Player, ErrCode = ?ERRCODE(err339_vip_lv_not_enough); %%vip等级不足
        Money > Gold ->  NewPlayer = Player, ErrCode = ?ERRCODE(gold_not_enough);
        LengthCheck == false -> NewPlayer = Player, ErrCode = ?ERRCODE(err339_length_not_match);
        KeyWordCheck -> NewPlayer = Player, ErrCode = ?ERRCODE(err145_2_word_is_sensitive);
        true ->
            case lib_goods_api:cost_money(Player, gold, Money, red_envelopes, "vip_red_envelopes") of
                {1, NewPlayer} ->
                    ErrCode = ?SUCCESS,
                    MoneyList = lib_red_envelopes:get_rand_money_list(Money, SplitNum, ?FULL_MONEY_TYPE),
                    GreetBin = util:make_sure_binary(Greet),
                    Args = [?RED_ENVELOPES_TYPE_VIP_PLAYER_SEND, ?GUILD_RED_ENVELOPES, GuildId, RoleId,
                    ?HAS_SEND, Money, MoneyList, SplitNum, PacketId, GuildLv, GreetBin, []],
                    mod_red_envelopes:add_red_envelopes(Args);
                {_, NewPlayer} -> ErrCode = ?GOLD_NOT_ENOUGH
            end
    end,
    {ok, BinData} = pt_339:write(33903, [ErrCode]),
    lib_server_send:send_to_sid(Sid, BinData),
    {ok, NewPlayer};

% 使用红包道具发送红包
handle(33905, Player, [GoodsId, GoodsTypeId]) ->
    #player_status{sid = Sid, id = RoleId, guild = GuildStatus} = Player,
    #status_guild{id = GuildId, lv = GuildLv} = GuildStatus,
    Cfg = data_red_envelopes:get_goods_cfg(GoodsTypeId),
    HasTimes = lib_red_envelopes:check_red_envelopes_times(?GOODS_RED_ENVELOPES, ?MOD_RED_ENVELOPES_GOODS, RoleId, GoodsTypeId),
    if
        GuildId == 0 -> ErrCode = ?ERRCODE(err339_not_join_guild);
        is_record(Cfg, red_envelopes_goods_cfg) == false -> ErrCode = ?ERRCODE(missing_config);
        HasTimes == false -> ErrCode = ?ERRCODE(err339_daily_times_no_enough);
        true ->
            #red_envelopes_goods_cfg{money = Money, min_num = SplitNum} = Cfg,
            case catch lib_goods_api:delete_one_by_id(Player, GoodsId, GoodsTypeId, 1, send_goods_red_envelopes) of
                1 ->
                    ErrCode = ?SUCCESS,
                    MoneyList = lib_red_envelopes:get_rand_money_list(Money, SplitNum, ?FULL_MONEY_TYPE),
                    Args = [?RED_ENVELOPES_TYPE_GOODS, ?GUILD_RED_ENVELOPES, GuildId, RoleId,
                    ?HAS_SEND, Money, MoneyList, SplitNum, GoodsTypeId, GuildLv, "", []],
                    lib_daily:increment(RoleId, ?MOD_RED_ENVELOPES, ?MOD_RED_ENVELOPES_GOODS, GoodsTypeId),
                    mod_red_envelopes:add_red_envelopes(Args);
                _Reason ->
                    ?ERR("send_goods_red_envelopes error:~p", [_Reason]),
                    ErrCode = ?FAIL
            end
    end,
    {ok, BinData} = pt_339:write(33905, [ErrCode]),
    lib_server_send:send_to_sid(Sid, BinData),
    ok;

%% 获取当前公会在线人数
handle(33906, Player, []) ->
    #player_status{guild = #status_guild{id = GuildId}} = Player,
    case GuildId > 0 of
        true -> lib_red_envelopes:get_guild_online_num(Player, GuildId);
        false -> skip
    end;

%% 获取已领取红包金额
handle(33907, Player, []) ->
    lib_red_envelopes:send_got_num(Player),
    ok;

%%
handle(33908,Player,[])->
    #player_status{id = RoleId, guild = GuildStatus} = Player,
    #status_guild{red_packet=RedPacket, red_gold_packet = GoldRedPacket} = GuildStatus,
    NowTimes = lib_daily:get_count(RoleId, ?MOD_RED_ENVELOPES, 1),
    Limit = lib_daily:get_count_limit(?MOD_RED_ENVELOPES, 1),
    {ok,Bin} = pt_339:write(33908,[NowTimes,Limit,GoldRedPacket,RedPacket]),
    lib_server_send:send_to_uid(RoleId,Bin),
    {ok,Player};

handle(33909,Player,[RedEnvelopesId])->
    #player_status{
        sid = Sid,
        id = RoleID,
        guild = GuildStatus
    } = Player,
    #status_guild{id = GuildId} = GuildStatus,
    case GuildId =/= 0 of
        true ->
            mod_red_envelopes:handout_red_envelopes(?GUILD_RED_ENVELOPES, GuildId, RoleID, RedEnvelopesId);
        _ ->
            lib_red_envelopes:send_error_code(Sid, ?ERRCODE(err400_not_join_guild))
    end;


handle(_Cmd, _Player, _Data) ->
    {error, "pp_red_envelopes no match~n"}.


