-module(pp_flower).

-export([handle/3]).

-include("server.hrl").
-include("flower.hrl").
-include("def_module.hrl").
-include("figure.hrl").
-include("errcode.hrl").
-include("common.hrl").
-include("predefine.hrl").

handle(Cmd, PS, Data) ->
    #player_status{figure = _Figure} = PS,
    % OpenLv = lib_module:get_open_lv(?MOD_FLOWER, 1),
    % case Figure#figure.lv >= OpenLv of
    %     true ->
            do_handle(Cmd, PS, Data).
        % false -> skip
    % end.

%% 赠送鲜花
do_handle(_Cmd = 22301, #player_status{id = RoleId} = Player, [ReceiverId, GoodsId, Num, IsAnonymous]) when ReceiverId =/= RoleId ->
    NewPlayer = lib_flower:send_gift(Player, ReceiverId, GoodsId, Num, IsAnonymous),
    {ok, battle_attr, NewPlayer};

%% 礼物记录
do_handle(Cmd = 22302, Player, []) ->
    #player_status{sid = Sid, id = RoleId} = Player,
    GiftRecordList = lib_flower:get_flower_gift_record(RoleId),
    {ok, BinData} = pt_223:write(Cmd, [GiftRecordList]),
    lib_server_send:send_to_sid(Sid, BinData);

%% 鲜花相关信息
do_handle(Cmd = 22303, Player, []) ->
    #player_status{sid = Sid, flower = Flower} = Player,
    {ok, BinData} = pt_223:write(Cmd, [Flower#flower.flower_num, Flower#flower.charm, Flower#flower.fame]),
    lib_server_send:send_to_sid(Sid, BinData);

do_handle(Cmd = 22304, Player, []) ->
    #player_status{sid = Sid, id = RoleId, last_logout_time = LastLogoutTime} = Player,
    case lib_flower:check_new_flower_record(RoleId, LastLogoutTime) of
        true ->
            Code = ?RECORD_NEW;
        false ->
            Code = ?RECORD_OLD
    end,
    lib_server_send:send_to_sid(Sid, pt_223, Cmd, [Code]);

%% 砸蛋
do_handle(22305, Player, [Id, EggId]) ->
    #player_status{id = RoleId, sid = Sid, figure = Figure} = Player,
    CheckList = [{'check_cfg', EggId}, {'lv_limit', EggId}, {'vip_limit', EggId}, {'check_enough_times', EggId}, {'check_online', Id}],
    case lib_flower:check(Player, CheckList) of
        true ->
            #flower_egg_cfg{cost = Cost, limit_time = LimitTime} = data_flower:get_egg_cfg(EggId),
            CanUseEgg = if
                LimitTime > 0 ->
                    Count = lib_daily:get_count(Player#player_status.id, ?MOD_FLOWER, EggId),
                    LimitTime > Count;
                true ->
                    Count = 0,
                    true
            end,
            %% 扣钱砸鸡蛋
            if
                CanUseEgg ->
                    case lib_goods_api:cost_object_list_with_check(Player, Cost, throwing_eggs, "") of
                        {true, NewPlayer,_} ->
                            if
                                LimitTime > 0 ->
                                    NewCount = LimitTime - (Count + 1),
                                    lib_daily:increment(RoleId, ?MOD_FLOWER, EggId);
                                true -> NewCount = Count
                            end,
                            lib_player:apply_cast(Id, ?APPLY_CAST_SAVE, ?NOT_HAND_OFFLINE, lib_flower, be_throwed_eggs, [EggId, Figure#figure.name, Sid, NewCount]);
                        {false, ErrorCode, NewPlayer} ->
                            lib_server_send:send_to_sid(Sid, pt_223, 22300, [ErrorCode])
                    end;
                true ->
                    NewPlayer = Player,
                    lib_server_send:send_to_sid(Sid, pt_223, 22300, [?ERRCODE(err223_egg_time_lim)])
            end;
        {false, ErrorCode} ->
            NewPlayer = Player,
            lib_server_send:send_to_sid(Sid, pt_223, 22300, [ErrorCode])
    end,
    {ok, NewPlayer};

%% 砸蛋界面
do_handle(22306, Player, [Id]) ->
    lib_player:apply_cast(Id, ?APPLY_CAST_STATUS, ?HAND_OFFLINE, lib_flower, get_charm, [Player#player_status.sid]);

do_handle(22307, Player, []) ->
    AllEggId = data_flower:get_all_egg_id(),
    Fun = fun(EggId, EggCfgList) ->
        #flower_egg_cfg{limit_time = LimitTime} = data_flower:get_egg_cfg(EggId),
        if
            LimitTime > 0 -> %% 有次数限制
                UseTime = lib_daily:get_count(Player#player_status.id, ?MOD_FLOWER, EggId);
            true ->
                UseTime = 0
        end,
        [{EggId, UseTime, LimitTime}|EggCfgList]
    end,
    EggCfgList = lists:foldl(Fun, [], AllEggId),
    lib_server_send:send_to_sid(Player#player_status.sid, pt_223, 22307, [EggCfgList]),
    {ok, Player};

do_handle(22308, Player, _) ->
    #player_status{id = RoleId, sid = Sid} = Player,
    AllSpGift = data_flower:get_all_sp_gift(),
    CountL = [{?MOD_FLOWER, 1, GoodsId} || GoodsId <- AllSpGift],
    CountL1 = lib_daily:get_count(RoleId, CountL),
    CountL2 = [{GoodsIdX, CountX} || {{_, _, GoodsIdX}, CountX} <- CountL1],
    {ok, BinData} = pt_223:write(22308, [CountL2]),
    lib_server_send:send_to_sid(Sid, BinData),
    ok;

do_handle(22309, Player, [Id]) ->
    lib_flower:thank_flower_sender(Player, Id),
    ok;

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