%%%--------------------------------------
%%% @Module  : lib_rename
%%% @Author  : zengzy
%%% @Created : 2017-06-03
%%% @Description:  角色名相关函数
%%%--------------------------------------
-module(lib_rename).
-compile(export_all).
-export([

    ]).
-include("server.hrl").
-include("figure.hrl").
-include("relationship.hrl").
-include("rename.hrl").
-include("errcode.hrl").
-include("predefine.hrl").
-include("common.hrl").
-include("def_goods.hrl").
-include("scene.hrl").
-include("def_module.hrl").
-include("def_daily.hrl").
-include("attr.hrl").
-include("def_event.hrl").
-include("rec_event.hrl").
-include("language.hrl").

get_old_name(RoleId) ->
    case get({?MODULE, player_old_name}) of
        undefined ->
            NameList = init_old_name(RoleId),
            set_old_name(NameList),
            NameList;
        NameList ->
            NameList
    end.

set_old_name(NameList) ->
    put({?MODULE, player_old_name}, NameList),
    ok.

%%登录获取曾用名
% login(PS)->
%   #player_status{id = RoleId} = PS,

init_old_name(_RoleId) ->
    % Sql = io_lib:format(?SQL_SELECT_PLAYER_RENAME, [RoleId]),
    % case db:get_all(Sql) of
    %   []->
    %       [];
    %   ReNameList ->
    %       F = fun([Name,Time],Rlist)->
    %           [{Name,Time}|Rlist]
    %       end,
    %       Rlists = lists:foldr(F,[],ReNameList),
    %       Rlists
    %       % set_old_name(Rlists)
    % end.
    [].

%%离线登录
offline_login(_Id) ->
    % Sql = io_lib:format(?SQL_SELECT_PLAYER_RENAME, [Id]),
    % case db:get_all(Sql) of
    %   []->
    %       [];
    %   ReNameList ->
    %       F = fun([Name,Time],Rlist)->
    %           [{Name,Time}|Rlist]
    %       end,
    %       lists:foldr(F,[],ReNameList)
    % end.
    [].

%% 第一次改名免费！
% judge_goods(#player_status{c_rename_time = 0}, _) ->
%     [];
% judge_goods(#player_status{c_rename = 1}, _) ->
%     [];

%%获取消耗物品
judge_goods(PS,Type) ->
    #player_status{figure = Figure, c_rename_time = RenameTime} = PS,
    case Type of
        ?FREE->
            #figure{lv = Lev} = Figure,
            case data_rename:get_cfg(?LV_TYPE) of
                {MinLev, MaxLev} when MinLev =< Lev andalso Lev =< MaxLev -> %% 免费改名
                    case RenameTime of
                        0 -> 
                            [];
                        _ -> %% 已经改过名
                            {false,?ERRCODE(not_have_free_card)}
                    end;
                _ ->
                    {false,?ERRCODE(not_have_free_card)}
            end;
        ?GOLD_TYPE->
            %% GoldSum = data_rename:get_cfg(?GOLD_NUM_TYPE),
            GoodsTypeId = data_rename:get_cfg(?CARD_TYPE),
            CostList = [{?TYPE_GOODS, GoodsTypeId, 1}],
            case lib_goods_api:check_object_list_with_auto_buy(PS, CostList) of
                {false, _} ->
                    {false,?ERRCODE(gold_not_enough)};
                true -> 
                    CostList
            end;
        ?CHANGE_CARD_TYPE->
            Good_id = data_rename:get_cfg(?CARD_TYPE),
            [{_,Num}] = lib_goods_api:get_goods_num(PS,[Good_id]),
            case Num >0 of
                true->
                    [{?TYPE_GOODS,Good_id,1}];
                false->
                    {false,?ERRCODE(not_have_free_card)}
            end;
        _ ->
            {false, ?FAIL}
    end.

%%修改角色名
change_name(RoleId,Name, _Figure) ->
    % OldName = Figure#figure.name,
    Now = utime:unixtime(),
    NewNameSql  = io_lib:format(?SQL_UPDATE_PLAYER_LOW_NAME, [Name, Now, RoleId]),
    % OldNameSql = io_lib:format(?SQL_INSERT_PLAYER_RENAME, [RoleId,OldName,Now]),
    F = fun() ->
        db:execute(NewNameSql),
        % db:execute(OldNameSql),
        true
    end,
    db_execute(F).

%%增加曾用名
add_old_name(PS, _Name)->
    #player_status{id=RoleId, figure=Figure} = PS,
    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)).

%%改名消耗物品
use_good(PS,GList) ->
    ?PRINT("~p~n",[GList]),
    case GList =:= [] of
        true->
            PS;
        false->
            case lib_goods_api:cost_objects_with_auto_buy(PS, GList, change_name, "change_name") of
                {true, NewPs, _RealCostList} ->
                    NewPs;
                {false, _, _} ->
                    PS
            end
            % F = fun({Type,Gid,Num},TempPS)->
            %   %%判断类型
            %   case Type of
            %       ?TYPE_GOODS->
            %           lib_goods_api:cost_object_list(PS, [{?TYPE_GOODS, Gid, 1}], change_name, "change_name"),
            %           TempPS;
            %       ?TYPE_GOLD->
            %           case lib_goods_api:cost_object_list(PS, [{?TYPE_GOLD, 0, Num}], change_name, "change_name") of
            %               {true, NewPs} ->
            %                   NewPs;
            %               {false,_,_}->
            %                   TempPS
            %           end;
            %       ?TYPE_BGOLD->
            %           case lib_goods_api:cost_object_list(PS, [{?TYPE_BGOLD, 0, Num}], change_name, "change_name") of
            %               {true, NewPs} ->
            %                   NewPs;
            %               {false,_,_}->
            %                   TempPS
            %           end;
            %       _->
            %           TempPS
            %   end
            % end,
            % lists:foldl(F,PS,GList)
    end.

%%修改ps状态
change_ps(PS,Name) ->
    OldFigure = PS#player_status.figure,
    NewPS = PS#player_status{figure=OldFigure#figure{name=Name}, c_rename = 0, c_rename_time = utime:unixtime()},
    NewPS.

%%发送邮件
send_email(RList,Tid,Cid,Name,OldName)->
    Title = ?LAN_MSG(Tid),
    Content = uio:format(?LAN_MSG(Cid), [OldName, Name]),
    F = fun(#rela{other_rid=To_id},Rids)->
        [To_id|Rids]
    end,
    RIdLists = lists:foldl(F,[],RList),
    lib_mail_api:send_sys_mail(RIdLists, ?MOD_PLAYER, Title, Content, []).

%%db操作
db_execute(F)->
    case db:transaction(F) of
        true  ->
            1;
        Error ->?PRINT("error~p~n",[Error]),
            ?ERR("~p ~p Error:~p~n", [?MODULE, ?LINE, Error]),
            0
    end.

%%倒排序
sort_reverse(List, N) ->
    sort_reverse(List, N, []).

sort_reverse([], _, List)->
    List;
sort_reverse(L, N, List) ->
    case length(List) < N of
        true->
            sort_reverse(lists:droplast(L), N, List++[lists:last(L)]);
        false->
            List
    end.

