%%-----------------------------------------------------------------------------
%% @Module  :       pp_fishing
%% @Author  :       lhy
%% @Email   :       liuhouying73@qq.com
%% @Created :       2018-09-28
%% @Description:    钓鱼系统
%%-----------------------------------------------------------------------------
-module(pp_fishing).
-export([handle/3]).
-include("fishing.hrl").
-include("server.hrl").
-include("common.hrl").
-include("errcode.hrl").
-include("def_daily.hrl").
-include("def_module.hrl").
-include("figure.hrl").
-include("def_act_label.hrl").

handle(Cmd, PS, Data) ->
    #player_status{figure = Figure, scene = SceneId, fishing = Fishing} = PS,
    if
        Cmd == 17711 orelse Cmd == 17704 orelse Cmd == 17705 orelse Cmd == 17709 orelse Cmd == 17710 ->
            case data_fishing:get_open_lv(SceneId) of
                [] ->
                    lib_fishing:return_error_code(PS, ?ERRCODE(err177_no_in_fishing_scene));
                {RoleLv, FishingLv} ->
                    if
                        Figure#figure.lv >= RoleLv andalso Fishing#fishing.lv >= FishingLv ->
                            do_handle(Cmd, PS, Data);
                        Figure#figure.lv < RoleLv ->
                            lib_fishing:return_error_code(PS, ?ERRCODE(err177_lv_not_reach_the_open_level));
                        Fishing#fishing.lv < FishingLv ->
                            lib_fishing:return_error_code(PS, ?ERRCODE(err177_fishing_lv_not_reach_the_open_level))
                    end
            end;
        true ->
            do_handle(Cmd, PS, Data)
    end.

%% 进入钓鱼模式
do_handle(17711, TmpPS, _) ->
    #player_status{sid = Sid, id = RoleId, scene = SceneId,
                           scene_pool_id = PoolId, copy_id = CopyId, x = X, y = Y} = TmpPS,
    lib_task_api:finish_fish(TmpPS),
    CheckList = [{check_all, {TmpPS, SceneId}}],
    case lib_fishing:check_args(CheckList) of
        true ->
            %% 钓鱼状态标志
            lib_fishing_util:update_fishing_act([RoleId, SceneId, PoolId, CopyId, X, Y], ?ACT_LABEL_FISHING, ?ACT_FISHING),
            lib_server_send:send_to_sid(Sid, pt_177, 17711, [?SUCCESS]),
            {ok, TmpPS};
        {false, ErrCode} ->
            lib_server_send:send_to_sid(TmpPS#player_status.sid, pt_177, 17711, [ErrCode]),
            {ok, TmpPS}
    end;

do_handle(17700, PS, _) ->
    lib_fishing:send_fishing_info(PS);

%% 渔具界面信息
do_handle(17701, PS, _) ->
    lib_fishing:send_fishing_gear_info(PS);

%% 选择渔具
do_handle(17702, PS, [FishingRodId, BaitId]) ->
    {ok, NewPS} = lib_fishing:select_fishing_gear(PS, [FishingRodId, BaitId]),
    {ok, NewPS};

%% 购买渔具
do_handle(17703, PS, [FishingGearId, Num]) ->
    {ok, NewPS} = lib_fishing:buy_fishing_gear(PS, [FishingGearId, Num]),
    {ok, NewPS};

%% 判断道具是否足够  进入钓鱼状态
do_handle(17710, PS, [MaunalOrAuto]) ->
    {ok, NewPS} = lib_fishing:goods_check(PS, MaunalOrAuto),
    {ok, NewPS};

%% 钓鱼
do_handle(17704, PS, [SceneId, SceneType, HomeFishPondLv, WaterArea, MaunalOrAuto]) ->
    {ok, NewPS} = lib_fishing:throw_fishing_gear(PS, [SceneId, SceneType, HomeFishPondLv, WaterArea, MaunalOrAuto]),
    {ok, NewPS};

%% 收杆成功验证
do_handle(17705, PS, [Code]) ->
    {ok, NewPS} = lib_fishing:take_back_fishing_rod(PS, [Code]),
    {ok, NewPS};

%% 鱼类图鉴
do_handle(17706, PS, _) ->
    lib_fishing:send_fish_illu_map(PS);

%% 激活升级鱼鉴
do_handle(17714, PS, [FishIlluId]) ->
    case lib_fishing:upgrade_fish_illu_map(PS, FishIlluId) of
        {ok, NewPS} ->
            {ok, battle_attr, NewPS};
        {false, _, NewPS} ->
            {ok, NewPS}
    end;

%% 钓鱼区跳转
do_handle(17707, PS, [TargetSceneId]) ->
    lib_fishing:transfer_to_other_scene(PS, [TargetSceneId]);

%%获取当前鱼筐记录
do_handle(17708, PS, _) ->
    lib_fishing:send_fish_basket_info(PS);

%% 退出
do_handle(17709, PS, _) ->
    % mod_scene_agent:update(PS, [{pk_protect_time, 0}]),
    % lib_scene:broadcast_player_attr(PS, [{8, 0}]),
    #player_status{sid = Sid, scene = SceneId, id = RoleId, scene_pool_id = PoolId, copy_id = CopyId, x = X, y = Y} = PS,
    %% 退出钓鱼状态标志
    lib_fishing_util:update_fishing_act([RoleId, SceneId, PoolId, CopyId, X, Y], ?ACT_LABEL_FISHING, ?NO_ACT_FISHING),
    {ok, BinData} = pt_177:write(17709, [?SUCCESS]),
    lib_server_send:send_to_sid(Sid, BinData),
    LastPS = lib_fishing_util:initialize_fish_basket(PS), %%初始化鱼筐记录
    {ok, LastPS};

do_handle(17712, PS, [ActId]) ->
    case lists:member(ActId, ?ACT_ID) of
        true ->
            #player_status{id = RoleId, scene = SceneId, x = X, y = Y, sid = _Sid,
                           scene_pool_id = SPid, copy_id = CopyId} = PS,
            mod_scene_agent:update(PS, [{fishing_act, ActId}]),
            lib_fishing_util:update_fishing_act([RoleId, SceneId, SPid, CopyId, X, Y], ?ACT_LABEL_FISHING_ACT, ActId),
            AreaList = data_fishing:get_area_list(SceneId),
            case data_fishing:get_area_list(SceneId) of
                [] ->
                    {ok, PS};
                AreaList ->
                    F = fun({_, {CenterX, CenterY, R}}, TmpCenterList) ->
                        TmpX = abs(X - CenterX),
                        TmpY = abs(Y - CenterY),
                        D = math:sqrt(TmpX *TmpX + TmpY * TmpY),
                        if
                            D =< R + 50 ->
                                [{CenterX, CenterY, R - D + 50} | TmpCenterList];
                            true ->
                                TmpCenterList
                        end
                    end,
                    CenterList = lists:foldl(F, [], AreaList),
                    if
                        CenterList == [] ->
                            {ok, PS};
                        true ->
                            NewCenterList = lists:keysort(3, CenterList),
                            [{ResX, ResY, _} | _T] = NewCenterList,
                            {ok, BinData} = pt_177:write(17712, [RoleId, ActId, ResX, ResY]),
                            lib_server_send:send_to_area_scene(SceneId, SPid, CopyId, X, Y, BinData),
                            {ok, PS}
                    end
            end;
        false ->
            {ok, PS}
    end;

do_handle(_Cmd, _Status, _Data) ->
    {error, "pp_fishing no match"}.