-module(pp_rename).

-export([handle/3]).

-include("server.hrl").
-include("rename.hrl").
-include("errcode.hrl").
-include("figure.hrl").
-include("predefine.hrl").
-include("common.hrl").
-include("def_module.hrl").
-include("def_event.hrl").
-include("relationship.hrl").
-include("language.hrl").


%%改角色名
handle(42601, PlayerStatus, [Name,Type]) when is_list(Name) ->
    #player_status{id=RoleId, sid = Sid, figure=Figure, scene = SceneId, scene_pool_id=PoolId,copy_id = CopyId,x = X,y = Y} = PlayerStatus,
    case check_rename(PlayerStatus, Name) of  %% 角色名合法性检测
        {false, Msg} ->
            {ok, BinData} = pt_426:write(42601, [Msg]),
            lib_server_send:send_to_sid(Sid, BinData),
            {ok, PlayerStatus};
        true ->
            case lib_rename:judge_goods(PlayerStatus,Type) of
                {false,Msg}->
                    {ok, BinData} = pt_426:write(42601, [Msg]),
                    lib_server_send:send_to_sid(Sid, BinData),
                    {ok, PlayerStatus};
                GList->
                    case lib_rename:change_name(RoleId, Name, Figure) of
                        1->
                            NameBin = util:make_sure_binary(Name),
                            %%消耗物品
                            TempPs = lib_rename:use_good(PlayerStatus, GList),
                            %%增加曾用名并通知玩家关系
                            % NewPS = lib_rename:add_old_name(TempPs, Name),
                            _OldName = Figure#figure.name,
                            % Now = utime:unixtime(),
                            % ReNameList = get_old_name(RoleId),
                            % NewNameList = [{OldName,Now}|ReNameList],
                            % SortList = lists:keysort(2,NewNameList),
                            % set_old_name(sort_reverse(SortList,?MAX_NAME)),
                            %%修改ps的figure的name
                            NewPS = lib_rename:change_ps(TempPs,NameBin),
                            %%通知场景
                            mod_scene_agent:update(SceneId,PoolId,RoleId,[{name,NameBin}]),
                            %%给场景九宫格发送
                            {ok, BinData} = pt_120:write(12086, [RoleId,NameBin]),
                            lib_server_send:send_to_area_scene(SceneId, PoolId, CopyId, X, Y, BinData),
                            %%派发事件
                            lib_player_event:dispatch(NewPS, ?EVENT_RENAME, NameBin),
                            %%通知好友
                            %Friends = lib_relationship:get_relas_by_types(RoleId,?RELA_TYPE_FRIEND),
                            %lib_rename:send_email(Friends, ?LAN_TITLE_RENAME, ?LAN_CONTENT_FRIEND_RENAME, Name, OldName),
                            %%通知仇人
                            % Enemy =  lib_relationship:get_relas_by_types(RoleId,?RELA_TYPE_ENEMY),
                            % lib_rename:send_email(Enemy, ?LAN_TITLE_RENAME, ?LAN_CONTENT_ENEMY_RENAME, Name, OldName),
                            {ok, BinData1} = pt_426:write(42601, [1]),
                            lib_server_send:send_to_sid(Sid, BinData1),
                            lib_chat:send_TV({all}, ?MOD_PLAYER, 1, [Figure#figure.name, RoleId, NameBin]),
                            %%加日志
                            lib_log_api:log_rename(RoleId,Figure#figure.name,NameBin,Figure#figure.lv,GList),
                            {ok, NewPS};
                        0->
                            {ok, BinData} = pt_426:write(42601, [0]),
                            lib_server_send:send_to_sid(Sid, BinData),
                            {ok, PlayerStatus}
                    end
            end
    end;

%%是否免费改名
handle(42602, PlayerStatus, [])->
    #player_status{sid = Sid} = PlayerStatus,
    IsFree = case lib_rename:judge_goods(PlayerStatus, ?FREE) of
        [] -> ?FREE;
        _ -> ?NOT_FREE
    end,
    {ok, BinData} = pt_426:write(42602, [IsFree]),
    lib_server_send:send_to_sid(Sid, BinData);

%%查看曾用名
% handle(42603, PlayerStatus, [Type,Id])->
%     ?PRINT("42603 is ~p ~p~n",[Type,Id]),
%     #player_status{sid = Sid} = PlayerStatus,
%     case Type of
%         %%查看自身
%         1 ->
%             ReNameList = lib_rename:get_old_name(Id),
%             ?PRINT("~p~n",[ReNameList]),
%             {ok, BinData} = pt_426:write(42603, [ReNameList]),
%             lib_server_send:send_to_sid(Sid, BinData);
%         %%查看别的玩家
%         2 ->
%             case lib_player:is_online_global(Id) of
%                 true -> ReNameList = lib_player:apply_call(Id, ?APPLY_CALL, lib_rename, get_old_name, [Id], 3000);
%                 false -> ReNameList = case lib_offline_api:get_player_info(Id, #player_status.rename) of
%                                             not_exist->
%                                                 [];
%                                             List->
%                                                 List
%                                       end
%             end,
%             ?PRINT("~p~n",[ReNameList]),
%             {ok, BinData} = pt_426:write(42603, [ReNameList]),
%             lib_server_send:send_to_sid(Sid, BinData)
%     end,
%     {ok, PlayerStatus}.

handle(_Cmd, _, _Args) ->
    ?ERR("pp_rename nomatch ~p ~p~n", [_Cmd, _Args]).

%%% ------------ 私有函数 --------------

%% 改名检查
check_rename(Role, Name) ->
    #player_status{c_rename_time = CRenameTime} = Role,
    RenameCd = case data_rename:get_cfg(?RENAME_CD) of
        Cd when is_integer(Cd) -> Cd;
        _ -> 600
    end,
    if
        CRenameTime =:= 0 ->
            validate_name(Name);
        true ->
            Now = utime:unixtime(),
            case Now - CRenameTime >= RenameCd of
                true ->
                    validate_name(Name);
                false ->
                    {false, ?ERRCODE(err610_is_cd)}
            end
    end.

%% 角色名合法性检测
validate_name(Name) ->
    case lib_word:validate_role_name(Name) of
        true ->
            true;
        {false, width} ->
            {false, ?ERRCODE(not_enough_length)};
        {false, invalid_utf8} ->
            {false, ?ERRCODE(illegal_character)};
        {false, role_exist} ->
            {false, ?ERRCODE(name_exist)};
        {false, keyword} ->
            {false, ?ERRCODE(err145_2_word_is_sensitive)};
        _ ->
            {false, 0}
    end.

