%% ---------------------------------------------------------------------------
%% @doc lib_skill_api
%% @author ming_up@foxmail.com
%% @since  2017-03-10
%% @deprecated 技能对外接口
%% ---------------------------------------------------------------------------
-module(lib_skill_api).

-include("common.hrl").
-include("predefine.hrl").
-include("skill.hrl").
-include("server.hrl").

-export([
         add_tmp_skill_list/3,   %% 增加一个临时技能
         del_tmp_skill_list/2,   %% 移除一个临时技能
         add_buff/3,
         clean_buff/2,
         get_common_skill/0,
         get_career_all_skill_ids/2,
         get_career_all_skill_ids2/2,
         get_career_skill_ids/2,
         get_career_active_skill_ids/2,
         get_skill_attr2mod/3,
         divide_passive_skill/1,
         get_skill_max_lv/1,
         get_skill_name/2,
         get_skill_power/2,
         is_normal/1,
         get_skill_career/1,
         include_battle_passive_skill/1
        ]).

%% 增加临时技能
add_tmp_skill_list(PlayerId, SkillId, SkillLv) ->
    lib_player:apply_cast(PlayerId, ?APPLY_CAST_SAVE, lib_skill, add_tmp_skill_list, [SkillId, SkillLv]).

%% 移除临时技能
del_tmp_skill_list(PlayerId, SkillId) ->
    lib_player:apply_cast(PlayerId, ?APPLY_CAST_SAVE, lib_skill, del_tmp_skill_list, [SkillId]).

%% 其他进程给玩家身上加buff
add_buff(PlayerId, SkillId, SkillLv)->
    lib_player:apply_cast(PlayerId, ?APPLY_CAST_SAVE, lib_skill_buff, add_buff, [SkillId, SkillLv]).

%% 移除某个技能的buff
clean_buff(PlayerId, SkillId) ->
    lib_player:apply_cast(PlayerId, ?APPLY_CAST_SAVE, lib_skill_buff, clean_buff, [SkillId]).

%% 获取配置中的通用技能
get_common_skill() ->
    data_skill:get_ids(10).

%% 获取职业专属技能id(不包括通用技能)
get_career_skill_ids(Career, Sex) ->
    data_skill:get_ids(Career, Sex).

%% 获取职业的所有技能id(职业专属+通用)
%% 职业专属 :: [含主技能、副技能、符文技能]
%% 通用 :: 通用被动技能
get_career_all_skill_ids(Career, Sex) ->
    get_career_skill_ids(Career, Sex) ++ get_common_skill().
%% 包括七日目标的技能
get_career_all_skill_ids2(Career, Sex) ->
    get_career_skill_ids(Career, Sex) ++ get_common_skill() ++ data_skill:get_ids(?SKILL_SEVEN_DAYS_GOAL).

%% 获取职业主动技能
get_career_active_skill_ids(Career, Sex) ->
    SkillList = get_career_skill_ids(Career, Sex),
    F = fun({SkillId, Lv}) ->
        case data_skill:get(SkillId, Lv) of
            %% 主动技能，排除转生增强技能
            #skill{type = ?SKILL_TYPE_ACTIVE, is_combo = 0, lv_data = LvData} ->
                Condition = LvData#skill_lv.condition,
                Res = lists:keyfind(turn, 1, Condition),
                Res == false;
            _ -> false
        end
    end,
    lists:filter(F, SkillList).

%% 获取技能对模块加成的被动属性列表
get_skill_attr2mod(ModId, SkillIds, RoleLv) when is_list(SkillIds) ->
    AttrL = lists:foldl(fun(T, AccL) ->
        TmpL = case T of
            {SkillId, SkillLv} ->
                get_skill_attr2mod(ModId, SkillId, SkillLv, RoleLv);
            SkillId ->
                get_skill_attr2mod(ModId, SkillId, 1, RoleLv)
        end,
        TmpL ++ AccL
    end, [], SkillIds),
    util:combine_list(AttrL).

get_skill_attr2mod(ModId, SkillId, Lv, RoleLv) ->
    lib_skill_attr:get_skill_attr2mod(ModId, SkillId, Lv, RoleLv).

%% 分离出战斗计算的被动
divide_passive_skill([]) -> [];
divide_passive_skill(SkillIds) ->
    SkillLvList = lists:map(fun(Tmp) ->
                case Tmp of
                    {TmpId, TmpLv} -> {TmpId, TmpLv};
                    _ -> {Tmp, 1}
                end
        end, SkillIds),
    lib_skill:divide_passive_skill(SkillLvList).

%% 获取
get_skill_max_lv(SkillId)->
    case data_skill:get(SkillId, 1) of
        [] -> 0;
        #skill{career = Career} ->
            SkillTypeList = data_skill:get_ids(Career),
            case lists:keyfind(SkillId, 1, SkillTypeList) of
                false -> 0;
                {_, MaxLv} -> MaxLv
            end
    end.

get_skill_name(SkillId, SkillLv) ->
    case data_skill:get(SkillId, SkillLv) of
        #skill{name = Name} ->
            util:make_sure_binary(Name);
        _ -> <<>>
    end.

get_skill_power(SkillId, SkillLv) ->
    case data_skill:get(SkillId, SkillLv) of
        #skill{lv_data=#skill_lv{power = Power}} ->
            Power;
        _ -> 0
    end.

%% 是否普攻
%% @return boolean()
is_normal(SkillId) ->
    case data_skill:get(SkillId, 1) of
        #skill{is_normal=1} -> true;
        _ -> false
    end.

%% 获取技能的职业
get_skill_career(SkillId) ->
    case data_skill:get(SkillId, 1) of
        #skill{career=Career} -> Career;
        _ -> 0
    end.

%% 统计玩家的所有战斗被动技能
include_battle_passive_skill(Player) ->
    #status_skill{skill_passive = SkillPassive} = Player#player_status.skill,
    SkillVisus = lib_role_visus:get_skills_for_battle(Player),
    SupVipSkills = lib_sup_vip:get_sup_vip_skill(Player),
    NucSkills = lib_role_nucleon:get_skills(Player),
    ChildSkills = lib_child_api:get_child_passive_skills(Player),
    PetSkills = lib_pet_api:get_pet_passive_skills(Player),
    SkillPassive
        ++ SkillVisus
        ++ SupVipSkills
        ++ NucSkills
        ++ ChildSkills
        ++ PetSkills
        ++ warsoul_api:get_warsoul_skill(Player).
