%% ---------------------------------------------------------------------------
%% @doc lib_guild_util.erl

%% @author  hjh
%% @email  hejiahua@163.com
%% @since  2016-12-15
%% @deprecated 辅助函数
%% ---------------------------------------------------------------------------
-module(lib_guild_util).
-export([
        calc_page_cache/3
        , is_need_refresh/1
    ]).

-include("guild.hrl").
-include("figure.hrl").
-include("common.hrl").
-include("def_fun.hrl").

-compile(export_all).

%% 计算分页
%% RecordTotal:总长度 PageSize:页大小 PageNo:第几页
%% @return {总长度, 开始位置, 本页大小}
calc_page_cache(RecordTotal, PageSize, PageNo) when RecordTotal > 0, PageSize > 0, PageNo > 0 ->
    PageTotal = (RecordTotal+PageSize-1) div PageSize,
    StartPos = (PageNo - 1) * PageSize + 1,
    if
        ((PageNo > PageTotal) or (PageNo < 1)) ->
            {PageTotal, 1, 0};
        true ->
            if
                PageNo*PageSize > RecordTotal ->
                    {PageTotal, StartPos, RecordTotal-(PageNo-1) * PageSize};
                true ->
                    {PageTotal, StartPos, PageSize}
            end
    end;
calc_page_cache(_RecordTotal, _PageSize, _PageNo) ->
    {0, 1, 0}.

%% 是否需要刷新(三点判断)
is_need_refresh(RefreshTime) ->
    T1 = utime:unixdate() + 10800,
    T2 = utime:unixdate() - 75600,
    NowTime = utime:unixtime(),
    case NowTime < T1 of
        true -> RefreshTime < T2;
        false -> RefreshTime < T1
    end.

%% 记录公会成员日志
log_guild_member(#guild{id = GuildId, name = GuildName}, EventType, EventParam) ->
    {NewRoleId, NewEventParam} = case EventType of
        ?GLOG_CREATE ->
            [#guild_member{id = RoleId, figure = #figure{name = RoleName}} = GuildMember] = EventParam,
            PositionList = get_pos_str(GuildMember),
            Str = lists:concat([uio:thing_to_string(RoleName), ?SEPARATOR_STRING, PositionList]),
            {RoleId, Str};
        ?GLOG_JOIN ->
            [#guild_member{id = RoleId, figure = #figure{name = RoleName}} = GuildMember] = EventParam,
            PositionList = get_pos_str(GuildMember),
            Str = lists:concat([uio:thing_to_string(RoleName), ?SEPARATOR_STRING, PositionList]),
            {RoleId, Str};
        ?GLOG_KICK_OUT ->
            [#guild_member{id = RoleId, figure = #figure{name = RoleName}} = GuildMember] = EventParam,
            PositionList = get_pos_str(GuildMember),
            Str = lists:concat([uio:thing_to_string(RoleName), ?SEPARATOR_STRING, PositionList]),
            {RoleId, Str};
        ?GLOG_POS_CHANGE ->
            [OldGuildMember, GuildMember] = EventParam,
            #guild_member{id = RoleId, figure = #figure{name = RoleName}} = OldGuildMember,
            OldPositionList = get_pos_str(OldGuildMember),
            PositionList = get_pos_str(GuildMember),
            Str = lists:concat([uio:thing_to_string(RoleName), ?SEPARATOR_STRING, OldPositionList, ?SEPARATOR_STRING, PositionList]),
            {RoleId, Str};
        % LeaderGuildMember :: #guild_member{} | []
        ?GLOG_DISBAND ->
            [LeaderGuildMember] = EventParam,
            case LeaderGuildMember of
                [] -> {0, ""};
                #guild_member{id = RoleId, figure = #figure{name = RoleName}} ->
                    PositionList = get_pos_str(LeaderGuildMember),
                    Str = lists:concat([uio:thing_to_string(RoleName), ?SEPARATOR_STRING, PositionList]),
                    {RoleId, Str}
            end;
        ?GLOG_QUIT ->
            [#guild_member{id = RoleId, figure = #figure{name = RoleName}} = GuildMember] = EventParam,
            PositionList = get_pos_str(GuildMember),
            Str = lists:concat([uio:thing_to_string(RoleName), ?SEPARATOR_STRING, PositionList]),
            {RoleId, Str};
        ?GLOG_AUTO_CHIEF ->
            [OldGuildMember, GuildMember] = EventParam,
            #guild_member{id = RoleId, figure = #figure{name = RoleName}} = OldGuildMember,
            OldPositionList = get_pos_str(OldGuildMember),
            PositionList = get_pos_str(GuildMember),
            Str = lists:concat([uio:thing_to_string(RoleName), ?SEPARATOR_STRING, OldPositionList, ?SEPARATOR_STRING, PositionList]),
            {RoleId, Str};
        ?GLOG_MERGE->
            [OldGuildMember, _GuildMember] = EventParam,
            OGName = OldGuildMember#guild_member.guild_name,
            #guild_member{id = RoleId, figure = #figure{name = RoleName}} = OldGuildMember,
            Str = lists:concat([uio:thing_to_string(RoleName), uio:thing_to_string(OGName),uio:thing_to_string(GuildName)]),
            {RoleId, Str};
        _ ->
            {0,""}
    end,
    lib_log_api:log_guild_member(GuildId, GuildName, EventType, NewRoleId, NewEventParam, utime:unixtime()),
    ok.

%% 获得职位的字符串
get_pos_str(GuildMember) ->
    #guild_member{position = Position} = GuildMember,
    PositionName = get_pos_name(Position),
    lists:concat(["(", uio:thing_to_string(PositionName), ")"]).

%% 获得职位名字
get_pos_name(Position) ->
    case data_guild:get_guild_pos(Position) of
        [] -> "null";
        #base_guild_pos{name = Name} -> util:make_sure_binary(Name)
    end.

%% 计算公会战力
count_guild_combat_power(GuildId, NowTime) ->
    GuildMemberMap = lib_guild_data:get_guild_member_map(GuildId),
    F2 = fun(_RoleId, #guild_member{online_flag = OnlineFlag, last_logout_time = LastLogoutTime, combat_power = CombatPower, h_combat_power = HCombatPower}, Acc2) ->
        RealCountPower = ?IF(CombatPower > 0, CombatPower, HCombatPower),
        %% 离线时间超过一定时间的成员不参与公会战力计算
        case OnlineFlag == ?ONLINE_ON orelse NowTime - LastLogoutTime < ?CAL_GUILD_POWER_VAILD_TIME of
            true -> Acc2 + RealCountPower;
            false -> Acc2 + RealCountPower
        end
    end,
    maps:fold(F2, 0, GuildMemberMap).