%%%--------------------------------------
%%% @Module  : pp_relationship
%%% @Author  : zhenghehe
%%% @Created : 2011.12.23
%%% @Description:  管理玩家间的关系
%%%--------------------------------------
-module(pp_relationship).

-export([handle/3]).

-include("server.hrl").
-include("figure.hrl").
-include("errcode.hrl").
-include("relationship.hrl").
-include("predefine.hrl").
-include("common.hrl").
-include("def_event.hrl").
-include("role.hrl").
-include("def_module.hrl").
-include("def_fun.hrl").
-include("chat.hrl").
-include("mate.hrl").

handle(Cmd = 14010, PS, Data) ->
    do_handle(Cmd, PS, Data);
handle(Cmd = 14007, PS, [Type, _BeCtrlId] = Data) when Type == 2 orelse Type == 3 ->
    do_handle(Cmd, PS, Data);
handle(Cmd, PS, Data) ->
    #player_status{figure = Figure} = PS,
    OpenLv = lib_module:get_open_lv(?MOD_RELA),
    case Figure#figure.lv >= OpenLv of
        true  -> do_handle(Cmd, PS, Data);
        false -> skip
    end.

%% 获取关系列表
do_handle(14000, Player, [?RELA_LIST_TYPE_FETTERS]) ->
    #player_status{id = RoleId, sid = Sid, mate = #role_mate{mate_list = MateList}, chat_limit_new = RoleChatLimit} = Player,
    RelaList = lib_relationship:get_mate_list(RoleId, MateList),
    RelaList1 = lib_chat_limit:get_relalist_without_chatlimit(RoleChatLimit, RelaList),
    {ok, BinData} = pt_140:write(14000, [?RELA_LIST_TYPE_FETTERS, RelaList1]),
    lib_server_send:send_to_sid(Sid, BinData);
do_handle(14000, Player, [Type]) ->
    #player_status{id = RoleId, sid = Sid, chat_limit_new = RoleChatLimit} = Player,
    RelaList = lib_relationship:get_rela_list(RoleId, Type),
    RelaList1 = lib_chat_limit:get_relalist_without_chatlimit(RoleChatLimit, RelaList),
    {ok, BinData} = pt_140:write(14000, [Type, RelaList1]),
    lib_server_send:send_to_sid(Sid, BinData);

%% 获取好友推荐
do_handle(Cmd = 14001, Player, [Type]) ->
    #player_status{sid = Sid} = Player,
    {Code, RecommendedList} = lib_relationship:get_recommended_list(Player, Type),
    {ok, BinData} = pt_140:write(Cmd, [Code, RecommendedList]),
    lib_server_send:send_to_sid(Sid, BinData);

%% 通过玩家昵称查找玩家
do_handle(Cmd = 14002, Player, [TargetRoleName]) ->
    #player_status{sid = Sid} = Player,
    NameB = util:make_sure_binary(TargetRoleName),
    Data = case lib_player:get_role_id_by_name(NameB) of
        null -> [?ERRCODE(err140_3_role_no_exist), 0, "", 0, 0, 0, 0, 0, 0, "", 0, 0, 0, 0, 0];
        TargetRoleId ->
            case lib_relationship:get_one_recommended_role_info(TargetRoleId) of
                false ->
                    [?ERRCODE(err140_3_role_no_exist), 0, "", 0, 0, 0, 0, 0, 0, "", 0, 0, 0, 0, 0];
                {TRoleId, TRoleName, TRoleCareer, TRoleSex, TRoleTurn, TRoleLv, TRoleSupVipType, TRoleVip, TRolePic, TRolePicVer, TRoleProfile, TRoleDressId, TOnlineFlag, Power} ->
                    Code = ?IF(TOnlineFlag == 0, ?ERRCODE(err140_14_not_online), ?SUCCESS),
                    [Code, TRoleId, TRoleName, TRoleCareer, TRoleSex, TRoleTurn, TRoleLv, TRoleSupVipType, TRoleVip, TRolePic, TRolePicVer, TRoleProfile, TRoleDressId, TOnlineFlag, Power]
            end
    end,
    {ok, BinData} = pt_140:write(Cmd, Data),
    lib_server_send:send_to_sid(Sid, BinData);

%% 发送好友申请
do_handle(_Cmd = 14003, Player, [BeAskId]) when is_integer(BeAskId) ->
    #player_status{sid = Sid, server_id = ServerId, id = RoleId} = Player,
    IsBlock = lib_chat_limit:is_blocked(Player, BeAskId),
    %% 初步判断:不是自己且对方没有被自己屏蔽
    case RoleId =/= BeAskId andalso IsBlock == false of
        true  ->
	        CheckList = [{add_self, RoleId, BeAskId}, {friend_num_limit, RoleId}, {cross_role, ServerId, BeAskId}
                %,{check_rela_type, RoleId, BeAskId}
		        %, {other_side_online, BeAskId}
	        ],
	        case lib_relationship:check_list(CheckList) of
		        true ->
                    %% 判断与对方的好友关系
                    RelaList = lib_relationship:get_rela_on_dict(RoleId),
                    case lists:keyfind(BeAskId, #rela.other_rid, RelaList) of
                        #rela{rela_type = ?RELA_TYPE_FRIEND} ->
                            %% 单向好友可以一直发送申请给对方直到对方同意(策划大佬需求)
                            lib_relationship:ask_add_friend(Sid, RoleId, BeAskId);
                        #rela{rela_type = ?RELA_TYPE_SINGLE_FRIEND} = PreRela ->  % 单向好友
                            lib_relationship:active_add_single_friend(Player, PreRela);
                        _ ->
                            %% 陌生人关系/黑名单。不管如何,先成为单向好友
                            lib_relationship:add_single_rela_friend(Player, BeAskId),
                            lib_relationship:ask_add_friend(Sid, RoleId, BeAskId)
                    end;
		        {false, Code} ->
			        {ok, BinData} = pt_140:write(14003, [Code]),
			        lib_server_send:send_to_sid(Sid, BinData)
			end;
        false -> skip
    end,
    {ok, Player};

%% 回应添加好友请求(批量)
do_handle(Cmd = 14004, Player, [ResponseType]) ->
    Res = lib_relationship:reply_add_friend_ask(Player, ResponseType),
    case Res of
        friend_num_limit ->  {ok, BinData} = pt_140:write(Cmd, [?ERRCODE(err140_1_myself_friend_num_limit)]);
        _ -> {ok, BinData} = pt_140:write(Cmd, [?SUCCESS])
    end,
    lib_server_send:send_to_sid(Player#player_status.sid, BinData),
    {ok, Player};

%% 回应添加好友请求(单个玩家)
do_handle(_Cmd = 14005, Player, [AskId, ?REPLY_TYPE_PULL_BLACK]) ->
    case lib_chat_limit:is_blocked(Player, AskId) of
        true -> skip;
        _ ->
            lib_chat_limit:shield_role(?CHAT_SHEILD, AskId, Player)
    end;
do_handle(Cmd = 14005, Player, [AskId, ResponseType]) ->
    #player_status{sid = Sid, id = RoleId, figure = Figure} = Player,
    case lib_chat_limit:is_blocked(Player, AskId) of
        true ->
            {ok, BinData} = pt_140:write(Cmd, [?ERRCODE(err110_have_shield)]),
            lib_server_send:send_to_sid(Sid, BinData),
            ok;
        _ ->
            #figure{name = RoleName} = Figure,
            Code = lib_relationship:reply_one_add_friend_ask(AskId, RoleId, RoleName, ResponseType),
            {ok, BinData} = pt_140:write(Cmd, [Code]),
            lib_server_send:send_to_sid(Sid, BinData),
            {ok, Player}
    end;

%% 获取好友请求列表
do_handle(Cmd = 14006, Player, []) ->
    #player_status{sid = Sid, id = RoleId} = Player,
    FriendAskList = lib_relationship:get_friend_ask_list(RoleId),
    {ok, BinData} = pt_140:write(Cmd, [FriendAskList]),
    lib_server_send:send_to_sid(Sid, BinData);

%% 好友操作
do_handle(Cmd = 14007, Player, [Type, BeCtrlId]) when Player#player_status.id =/= BeCtrlId ->
    #player_status{sid = Sid, id = RoleId} = Player,
    Code = case Type of
        ?RELA_DEL_FRIEND ->
            case mate_api:is_mate(Player, BeCtrlId) of
                false -> lib_relationship:del_friend(RoleId, BeCtrlId);
                true  -> ?ERRCODE(err149_is_mate)
            end;
        _ -> ?FAIL
    end,
    {ok, BinData} = pt_140:write(Cmd, [Code]),
    lib_server_send:send_to_sid(Sid, BinData);

%% 获取玩家交互菜单信息
do_handle(Cmd = 14010, Player, [OtherRid]) when Player#player_status.id =/= OtherRid ->
    #player_status{sid = Sid, id = RoleId} = Player,
    RoleServer = mod_player_create:get_real_serid_by_id(OtherRid),
    ServerId = config:get_server_id(),
    ServerNum = config:get_server_num(),
    case ServerId == RoleServer of
        true ->
            case lib_role:get_role_show(OtherRid) of
                #ets_role_show{figure = Figure, combat_power = Combat} ->
                    case lib_relationship:get_rela_with_other_on_dict(RoleId, OtherRid) of
                        #rela{rela_type = RelaType, intimacy = Intimacy} -> skip;
                        _ -> RelaType = 0, Intimacy = 0
                    end,
                    %% 需要拿对方的队伍id 要cast过去
                    OtherPid = misc:get_player_process(OtherRid),
                    case misc:is_process_alive(OtherPid) of
                        true ->
                            lib_player:apply_cast(OtherRid, ?APPLY_CAST_STATUS, lib_relationship_api, send_rela_view_info, [node(), RoleId, RelaType, Intimacy, 0]);
                        false ->
                            WeekCharm = lib_flower:db_get_role_week_charm(OtherRid),
                            {ok, BinData} = pt_140:write(Cmd, [?SUCCESS, OtherRid, Figure, RelaType, Combat, WeekCharm, Intimacy, 0, 0, ServerNum]),
                            lib_server_send:send_to_sid(Sid, BinData)
                    end;
                _ ->
                    lib_server_send:send_to_sid(Sid, pt_140, 14010, [?FAIL, OtherRid, #figure{}, 0, 0, 0, 0, 0, 0, 0])
            end;
        _ -> %% 跨服玩家
            clusters_node:apply_cast(lib_relationship_api, get_rela_view_info_cls, [node(), RoleId, OtherRid, RoleServer, 0, 0, 1])
    end;

%% 获取玩家对应的社交关系
do_handle(Cmd = 14011, Player, [OtherRid]) when Player#player_status.id =/= OtherRid ->
    #player_status{sid = Sid, id = RoleId} = Player,
    case lib_relationship:get_rela_with_other_on_dict(RoleId, OtherRid) of
        #rela{rela_type = RelaType} -> skip;
        _ -> RelaType = 0
    end,
    {ok, BinData} = pt_140:write(Cmd, [OtherRid, RelaType]),
    lib_server_send:send_to_sid(Sid, BinData);

%% 一键申请好友
do_handle(Cmd = 14015, Player, []) ->
    #player_status{sid = Sid} = Player,
    ResultList = lib_relationship:quickly_apply(Player),
    {ok, BinData} = pt_140:write(Cmd, [ResultList]),
    lib_server_send:send_to_sid(Sid, BinData);

do_handle(14016, Player, []) ->
    relation_coupon:get_role_coupon(Player);

do_handle(14017, Player, [ToRoleId, Type]) ->
    relation_coupon:send_role_coupon(Player, ToRoleId, Type);

do_handle(14018, Player, [FromId, Type]) ->
    relation_coupon:receive_coupon(Player, FromId, Type);

%% 将陌生人加入最近联系人列表
do_handle(14020, Player, [OtherRid]) ->
    #player_status{id = RoleId} = Player,
    relation_last_chat:add_last_rela_list(RoleId, OtherRid),
    ok;

%% 登录查询好友关系列表中己方在对方所处的关系(用于显示/隐藏亲密度)
do_handle(14021, Player, [OtherRidList]) ->
    #player_status{id = RoleId} = Player,
    lib_relationship:get_my_rela_type_from_opp(RoleId, OtherRidList),
    ok;

%% 一键发券
do_handle(14022, Player, [RoleList]) ->
    relation_coupon:batch_send_role_coupon(Player, RoleList);

%% 一键领取
do_handle(14023, Player, _) ->
    relation_coupon:batch_receive_coupon(Player);

%% 查询形象数据
do_handle(14024, Player, [ShowRoleId]) ->
    #player_status{id = RoleId, sid = Sid, figure = RoleFigure} = Player,
    if ShowRoleId == RoleId -> 
            RoleFigure1 = RoleFigure;
        true ->
            RoleFigure1 = lib_role:get_role_figure(ShowRoleId)
    end,
    {ok, BinData} = pt_140:write(14024, [?SUCCESS, RoleFigure1]),
    lib_server_send:send_to_sid(Sid, BinData);

do_handle(_Cmd, _Player, _Data) ->
    {error, atom_to_list(?MODULE) ++ " no match"}.
