%% ---------------------------------------------------------------------------
%% @doc 队伍检查
%% @author hek
%% @since  2016-11-14
%% @deprecated
%% ---------------------------------------------------------------------------
-module(lib_team_check).
-include("common.hrl").
-include("server.hrl").
-include("scene.hrl").
-include("team.hrl").
-include("figure.hrl").
-include("dungeon.hrl").
-include("errcode.hrl").
-include("predefine.hrl").
-include("def_fun.hrl").
-include("dungeon_multi.hrl").
-export([
    %% 创建队伍检查
    check_create_team/3,
    %% 通用条件检查
    check/2
]).

check_create_team(_PS, _ArgMaps, []) -> true;
check_create_team(PS, ArgMaps, [H|T]) ->
    case do_check_create_team(PS, ArgMaps, H) of
        true -> check_create_team(PS, ArgMaps, T);
        {false, Res} -> {false, Res}
    end.

do_check_create_team(_PS, ArgMaps, can_set_lv_power) ->
    do_check(ArgMaps, can_set_lv_power);
do_check_create_team(PS, ArgMaps, common_check) ->
    CreateTime = utime:unixtime(),
    #{activity_id := ActivityId, sub_type_id := SubTypeId} = ArgMaps,
    #player_status{
        figure = #figure{lv = Lv},
        team = #status_team{team_id=OldTeamId, cls_create_time = LastClsCreateTime},
        dun_multi = #role_dun_multi{team_id = MulTeamId}
    } = PS,
    CommonCheck = lib_team:check_for_create_team(PS, ActivityId, SubTypeId),
    if
        OldTeamId /= 0 orelse MulTeamId /= 0->
            {false, ?ERRCODE(err240_in_team)};
        Lv =< ?TEAM_NEED_LV ->
            {false, ?ERRCODE(err240_not_enough_lv_to_create_team)};
        CreateTime - LastClsCreateTime < 5 ->
            {false, ?ERRCODE(err240_team_create_unfinished)};
        CommonCheck =/= true ->
            {false, _OtherErrorCode, MyErrorCode} = CommonCheck,
            {ErrorCodeInt, ErrorCodeArgs} = util:parse_error_code(MyErrorCode),
            TransErrorCodeInt = lib_team:transfer_create_team_errcode(ErrorCodeInt),
            {false, {TransErrorCodeInt, ErrorCodeArgs}};
        true -> true
    end;
do_check_create_team(PS, _ArgMaps, _T) ->
    ?ERR("do_check_create_team:~p~n", [_T]),
    {false, ?FAIL, PS}.

%% 通用条件检查
check(_ArgMaps, []) -> true;
check(ArgMaps, [H|T]) ->
    case do_check(ArgMaps, H) of
        true -> check(ArgMaps, T);
        {false, Res} -> {false, Res}
    end.

do_check(ArgMaps, can_set_lv_power) ->
    #{
        activity_id := ActivityId, sub_type_id := SubTypeId,
        lv_min := LvMin, lv_max := LvMax, power := Power
    } = ArgMaps,
    EnListCfg = data_team_ui:get(ActivityId, SubTypeId),
    IsEnListCfgRecord = is_record(EnListCfg, team_enlist_cfg),
    NewEnListCfg = ?IF(IsEnListCfgRecord, EnListCfg, #team_enlist_cfg{}),
    #team_enlist_cfg{
        default_lv_min = LMin, default_lv_max = LMax,
        can_set_lv_limit = CanSetLvLimit, can_set_power_limit = CanSetPowerLimit
    } = NewEnListCfg,
    if
        %% 不能手动设置等级限制，只能按配置默认
        CanSetLvLimit == 0 andalso (LvMin =/= LMin orelse LvMax =/= LMax) ->
            ChooseLvError = ?ERRCODE(err240_cannot_choose_lv);
        CanSetLvLimit == 1 andalso (LMin > LvMin orelse LvMax < LvMin) ->
            ChooseLvError = ?ERRCODE(err240_choose_lv_error);
        CanSetLvLimit == 1 andalso (LMax > 0 andalso LvMax > LMax) ->
            ChooseLvError = ?ERRCODE(err240_choose_lv_error);
        true ->
            ChooseLvError = true
    end,
    if
        CanSetPowerLimit ==0 andalso Power =/=0 ->
            ChoosePowerError = ?ERRCODE(err240_cannot_choose_power);
        true ->
            ChoosePowerError = true
    end,
    if
        %% ActivityId == 0表示全部，只用作显示筛选
        %% 但不可作为队伍创建目标
        % ActivityId == ?TEAM_ENLIST_ACT_ALL orelse IsEnListCfgRecord =/= true  ->
        ActivityId == 0 orelse IsEnListCfgRecord =/= true  ->
            {false, ?ERRCODE(err240_team_target_not_exist)};
        ChooseLvError =/= true -> {false, ChooseLvError};
        ChoosePowerError =/= true -> {false, ChoosePowerError};
        true -> true
    end;
do_check(_ArgMaps, CheckType) ->
    ?ERR("unkown do_check:~p~n", [CheckType]),
    {false, ?FAIL}.
