%%%--------------------------------------
%%% @Module  : pp_skill
%%% @Author  : zhenghehe
%%% @Created : 2010.07.27
%%% @Description:  技能管理
%%%--------------------------------------
-module(pp_skill).
-export([handle/3]).
-include("common.hrl").
-include("server.hrl").
-include("skill.hrl").
-include("figure.hrl").
-include("errcode.hrl").
-include("predefine.hrl").

%% 学习技能
handle(21001, Status, [SkillId]) ->
    {NewStatus, ErrCode, SkillData} = case lib_skill:upgrade_skill(Status, SkillId) of
        {false, Reason} ->
            {Status, Reason, false};
        {true, Status2, SData} ->
            {Status2, ?SUCCESS, SData}
    end,
    #player_status{sid = Sid, figure = #figure{turn = Turn}} = NewStatus,
    {ok, Bin} = pt_210:write(21001, [ErrCode, SkillId]),
    lib_server_send:send_to_sid(Sid, Bin),
    lib_skill:send_my_skill_list(NewStatus),
    NewStatus2 = case ErrCode =:= ?SUCCESS of
        true ->
            #skill{is_anger = IsAnger, type = SkillType} = SkillData,
            AddSkills = [{SkillId, SkillType, IsAnger}],
            StatusUpdateQuickbar = quickbar_api:after_auto_learn_skill(
                NewStatus, Turn, AddSkills, login
            ),
            StatusUpdateQuickbar;
        _ ->
            NewStatus
    end,
    {ok, NewStatus2};

%% 获取技能列表
handle(21002, Status, _) ->
    lib_skill:send_my_skill_list(Status);

%% 技能升级
handle(21003, Status, [SkillId]) ->
    {NewStatus, ErrCode} = case lib_skill:upgrade_skill(Status, SkillId) of
        {false, Reason} ->
            {Status, Reason};
        {true, Status2, _SkillData} ->
            {Status2, ?SUCCESS}
    end,
    #player_status{sid = Sid} = Status,
    %% 前端要求先刷新技能列表
    case ErrCode =:= ?SUCCESS of
        true -> lib_skill:send_my_skill_list(NewStatus);
        _ -> ok
    end,
    {ok, Bin} = pt_210:write(21003, [ErrCode, SkillId]),
    lib_server_send:send_to_sid(Sid, Bin),
    {ok, NewStatus};


%% 天赋技能面板
handle(21010, Status, _) ->
    #player_status{sid = Sid, figure = #figure{turn = Turn}} = Status,
    if
        Turn < 5 -> LeftPoint = 0, TypeSkillList = [];
        true ->
            #status_skill{skill_talent_list = SkillTalentList,
                use_point = UsePoint, point = AllPoint} = Status#player_status.skill,
            {_UsePoint, _Power, TypeSkillList} = lib_skill:get_talent_skill_type_info(SkillTalentList),
            LeftPoint = max(0, AllPoint-UsePoint)
    end,
    {ok, Bin} = pt_210:write(21010, [LeftPoint, TypeSkillList]),
    lib_server_send:send_to_sid(Sid, Bin),
    ok;

%% 学习天赋技能
handle(21011, Status, [SkillId]) ->
    case lib_skill:learn_talent_skill(Status, SkillId) of
        {ok, LastStatus} -> {ok, LastStatus};
        _ -> skip
    end;

%% 天赋技能重置
handle(21012, Status, _) ->
    case lib_skill:reset_talent_skill(Status) of
        {ok, NewStatus} ->
            handle(21010, NewStatus, []),
            {ok, NewStatus};
        _ -> skip
    end;

%% 没有匹配
handle(_Cmd, Status, _Data) ->
    ?ERR("no match _cmd:~p, _data:~p~n", [_Cmd, _Data]),
    {ok, Status}.
