%%%------------------------------------
%%% @Module  : pp_common_rank
%%% @Author  :  xiaoxiang
%%% @Created :  2016-11-17
%%% @Description: 通用榜单
%%%------------------------------------

-module(pp_common_rank).

-compile(export_all).

-include("def_module.hrl").
-include("common_rank.hrl").
-include("errcode.hrl").
-include("server.hrl").
-include("figure.hrl").
-include("common.hrl").
-include("guild.hrl").
-include("flower.hrl").
-include("fashion.hrl").
-include("def_fun.hrl").
-include("dungeon.hrl").
-include("goods.hrl").
-include("equipment.hrl").
-include("child.hrl").
-include("designation.hrl").
-include ("warsoul.hrl").
-include ("predefine.hrl").
-include ("rec_dress_up.hrl").
-include ("profile_photo.hrl").

handle(22101, Player, [RankType, Start, Len]) ->
    IsLv    = lists:member(RankType, ?RANK_TYPE_ALL_LVS),
    IsEquip = lists:member(RankType, ?RANK_TYPE_ALL_EQUIPS),
    IsFoster = lists:member(RankType, ?RANK_TYPE_FOSTER),
    IsFoster1 = lists:member(RankType, ?RANK_TYPE_FOSTER_1),
    IsFoster2 = lists:member(RankType, ?RANK_TYPE_FOSTER_2),
    if
        IsLv ->   % 等级榜
            SelfValue = Player#player_status.figure#figure.lv,
            SelfSecValue = 0;
        IsEquip ->
            #player_status{equipment = #equipment{total_power = EquipPower}} = Player,
            SelfValue = EquipPower,
            SelfSecValue = 0;
        IsFoster ->
            SelfValue = lib_common_rank:get_foster_cell_power(RankType, Player),
            if 
                IsFoster2 ->
                    SelfSecValue = lib_common_rank:get_foster_cell_lv(RankType, Player);
                IsFoster1 ->
                    SelfSecValue = lib_common_rank:get_foster_cell_grade(RankType, Player);
                true ->
                    SelfSecValue = 0
            end;
        RankType == ?RANK_TYPE_FASHION ->  % 时装榜
            #player_status{role_fashion = #role_fashion{power1 = Power}} = Player,
            SelfValue = Power,
            SelfSecValue = lib_fashion:get_fashion_active_num(Player);
        RankType == ?RANK_TYPE_DESIGNATION ->
            #player_status{dsgt_status = #dsgt_status{power = DsgtPower}} = Player,
            SelfValue = DsgtPower,
            SelfSecValue = lib_designation:get_all_actived_dsgt(Player);
        RankType == ?RANK_TYPE_BABY ->
            #player_status{status_child = #status_child{child_power = ChildPower, extra_power = ChildPower1}} = Player,
            SelfValue = round(ChildPower+ChildPower1),
            SelfSecValue = 0;
        RankType == ?RANK_TYPE_CHARM_WEEK orelse RankType == ?RANK_TYPE_GENEROURS ->
            #player_status{flower = #flower{fame = Fame, charm = Charm}} = Player,
            SelfValue = if RankType == ?RANK_TYPE_CHARM_WEEK -> Charm;
                true -> Fame
            end,
            SelfSecValue = 0;
        RankType == ?RANK_TYPE_WARSOUL ->
            #player_status{role_war_soul = #role_war_soul{total_power = Power}} = Player,
            SelfValue = Power,
            SelfSecValue = 0;
        RankType == ?RANK_TYPE_FOSTERS orelse RankType == ?RANK_TYPE_AIGIRLS ->
            {SubFosterPower, _} = lib_common_rank:get_sub_foster_cell_power(RankType, Player),
            SelfValue = SubFosterPower,
            SelfSecValue = 0;
        RankType == ?RANK_TYPE_DRESS ->
            #player_status{dress_up = DressUp, figure = #figure{lv = Lv}} = Player,
            #status_dress_up{enabled_map = EnabledMap, power = Power} = DressUp,
            {ColorList, _}  = lib_dress_up:calc_all_dress_up(Lv, 1, EnabledMap),
            {ColorList1, _} = lib_dress_up:calc_all_dress_up(Lv, 2, EnabledMap),
            F = fun({_, Num}, TSumNum) ->
                Num + TSumNum
                end,
            SumNum = lists:foldl(F, 0, ColorList ++ ColorList1),
            PhotoNum = lib_common_rank_api:get_role_photo_num(Player),
            SelfValue = SumNum + PhotoNum,
            SelfSecValue = Power;
        true ->
            SelfValue = Player#player_status.combat_power,
            SelfSecValue = 0
    end,
    mod_common_rank:send_rank_list(RankType, Start, Len, Player#player_status.id, SelfValue, SelfSecValue);

handle(22102, Player, [RankType, Start, Len])->
    %?PRINT(" 22101 RankType:~p Start:~p , Len:~p ~n", [RankType,Start, Len]),
    case lists:member(RankType,?RANK_GUILD_LIST) of
        true->
            #player_status{id = RoleId, figure = Figure} = Player,
            mod_common_rank:send_rank_list(RankType, Start, Len, RoleId, Figure#figure.guild_id, 0);
        _->
            {ok,Player}
    end;

%% 0未点赞或还有点赞次数
%% 1已点赞且无点赞次数
handle(22103, Player, [RankType]) ->
    #player_status{sid = Sid, id = RoleId, role_rank_praise = RolePraise} = Player,
    Value = lib_daily:get_count(RoleId, ?MOD_RANK, ?RANK_DAILY_PRAISE, RankType),
    RestNum = max(?MAX_PRAISE_NUM - Value, 0),
    State = if RestNum > 0 -> 0;
        true   -> 1
    end,
    case lists:keyfind(RankType, 1, RolePraise) of
        {_, PraiseId, _} -> PraiseId;
        _  -> PraiseId = 0
    end,
    %?INFO_TTY("~p, ~p ~n", [Value, Num]),
    {ok, BinData} = pt_221:write(22103, [?SUCCESS, RankType, PraiseId, State]),
    lib_server_send:send_to_sid(Sid, BinData);

handle(22104, Player, [OtRoleId, RankType]) ->
%PRINT(" 22101 RankType:~p Start:~p , Len:~p ~n", []),
    #player_status{id = RoleId, sid = Sid} = Player,
    Value = lib_daily:get_count(RoleId, ?MOD_RANK, ?RANK_DAILY_PRAISE, RankType),
    RestNum = max(?MAX_PRAISE_NUM - Value, 0),
    if
        RestNum =< 0 ->
            lib_server_send:send_to_sid(Sid, pt_221, 22104, [?ERRCODE(err211_praise_num_limit), RankType, OtRoleId, RestNum]);
        true ->
            mod_common_rank:rank_praise(RoleId, OtRoleId, RankType)
    end;

handle(22105, Player, [RankType, Start, Len]) ->
    #player_status{sid = Sid} = Player,
    mod_common_rank:get_top_rank(RankType, Start, Len, Sid);

handle(22106, Player, [RankType]) ->
    #player_status{sid = Sid, role_first_top = RoleFirstTop} = Player,
    RewardedTarget = lists:foldl(fun({RankTypeX, Target}, Acc) ->
        if 
            RankType == RankTypeX -> [Target|Acc];
            true -> Acc
        end 
    end, [], RoleFirstTop),
    mod_common_rank:get_first_top_info(Sid, RankType, RewardedTarget);

handle(22107, Player, [RankType, Target]) ->
    #player_status{id = RoleId} = Player,
    mod_common_rank:get_first_top_reward(RoleId, RankType, Target);

handle(22108, Player, [RankType, StartIndex, Len]) ->
    #player_status{id = RoleId, combat_power = Power, figure = #figure{lv = RoleLv}} = Player,
    IsPowerType = lists:member(RankType, ?RANK_TYPE_ALL_COMBATS),
    IsLvType    = lists:member(RankType, ?RANK_TYPE_ALL_LVS),
    if 
        IsPowerType ->
            Val = Power, SecVal = 0;
        IsLvType ->
            Val = RoleLv, SecVal = 0;
        true ->
            Val = Power, SecVal = 0
    end,
    cross_rank_local:get_zone_rank({RoleId, Val, SecVal, RankType, StartIndex, Len});

%% TODO 查询CD
handle(22109, Player, [TarRoleId]) ->
    #player_status{sid = Sid} = Player,
    cross_rank_local:get_rank_role_figure(Sid, TarRoleId);

handle(22111, Player, [RankType]) ->
    #player_status{sid = Sid} = Player,
    mod_common_rank:get_top_rank_list(Sid, RankType);

handle(22112, Player, [TarRoleId]) ->
    #player_status{id = RoleId, sid = Sid} = Player,
    if RoleId == TarRoleId ->
            lib_common_rank:get_role_warsoul_info(Player, Sid);
        true ->
            lib_player:apply_cast(TarRoleId, ?APPLY_CAST_STATUS, ?HAND_OFFLINE, lib_common_rank, get_role_warsoul_info, [Sid])
    end,
    ok;

handle(_Cmd, _Player, _Data) ->
    % ?PRINT(" Data:~p ~n", [_Data]),
    {error, "pp_common_rank no match~n"}.
