%% ---------------------------------------------------------
%% Author:  zmh
%% Created: 20200311
%% Description: 玩家幻光
%% --------------------------------------------------------
-module(pp_visus).
-export([handle/3]).
-include("server.hrl").
-include("goods.hrl").
-include("predefine.hrl").
-include("role_visus.hrl").
-include("errcode.hrl").
-include("figure.hrl").
-include("def_module.hrl").
-include("def_goods.hrl").

%%获取信息
handle(14700, Status, []) ->
    #player_status{
        id=RoleID,
        role_visus = RoleVisus
    } = Status,
    #role_visus{
        login_days = Days,
        visus = Visus
    }=RoleVisus,
    Bin = lib_role_visus:pack_visus(Days,Visus),
    lib_server_send:send_to_uid(RoleID,Bin),
    {ok,Status};

%%激活
handle(14703,Status,[Vid])->
    #player_status{id=RoleID}=Status,
    case lib_role_visus:active_visus(Vid,Status) of
        {ok,NStatus0}->
            NStatus1 = lib_role_visus:count_visus_attr(NStatus0),
            NStatus  = lib_player:count_player_attribute(NStatus1),
            lib_player:send_attribute_change_notify(NStatus, ?NOTIFY_ATTR),
            lib_achievement_api:visus_power_event(NStatus,
                lib_role_visus:calc_attr_power(
                    NStatus1#player_status.role_visus#role_visus.attr
                )
            ),
            {ok,Bin} = pt_147:write(14703,[?SUCCESS,Vid]);
        {false,Err}->
            {ok,Bin} = pt_147:write(14703,[Err,0]),
            NStatus = Status
    end,
    lib_server_send:send_to_uid(RoleID,Bin),
    {ok,NStatus};

%%升级
handle(14701,Status,[Vid])->
    #player_status{id=RoleID}=Status,
    case lib_role_visus:upgrade_visus(Vid,Status) of
        {ok,CellVisus,NStatus0}->
            #cell_visus{
                grade = Grade,
                lv = Lv
            } = CellVisus,

            NStatus1  = lib_role_visus:count_visus_attr(NStatus0),
            NStatus  = lib_player:count_player_attribute(NStatus1),
            lib_player:send_attribute_change_notify(NStatus, ?NOTIFY_ATTR),
            lib_achievement_api:visus_power_event(NStatus,
                lib_role_visus:calc_attr_power(
                    NStatus1#player_status.role_visus#role_visus.attr
                )
            ),
            {ok,Bin} = pt_147:write(14701,[?SUCCESS,Vid,Grade,Lv]);
        {false,Err}->
            {ok,Bin} = pt_147:write(14701,[Err,Vid,0,0]),
            NStatus = Status
    end,
    lib_server_send:send_to_uid(RoleID,Bin),
    {ok,NStatus};

%%幻化
handle(14702,Status,[Vid])->
    #player_status{
        id=RoleID,
        scene = Scene,
        scene_pool_id = PoolID,
        copy_id = CopyId,
        x = X, y = Y
    }=Status,
    case lib_role_visus:change_display(Vid,Status) of
        {ok,OpTy,NStatus}->
            #player_status{figure=#figure{visus=CurShow}}=NStatus,
             %%场景广播
            {ok, Bin1} = pt_120:write(12016, [?MOD_VISUS,1,RoleID,CurShow]),
            lib_server_send:send_to_area_scene(Scene,PoolID,CopyId,X,Y,Bin1),

            mod_scene_agent:update(NStatus,[{visus,CurShow}]),

            {ok,Bin} = pt_147:write(14702,[?SUCCESS,OpTy,Vid]);
        {false,Err}->
            {ok,Bin} = pt_147:write(14702,[Err,0,0]),
            NStatus = Status
    end,
    lib_server_send:send_to_uid(RoleID,Bin),
    {ok,NStatus};

%% 查询幻光技能
handle(14705,Status, _)->
    lib_role_visus:send_visus_skill(Status),
    {ok, Status};

%% 切换幻光技能
handle(14706, Player, [SkillId]) ->
    {ProData, NewPlayer2} =
    case lib_role_visus:use_skill(Player, SkillId) of
        {false, Reason} ->
            {[SkillId, Reason], Player};
        {ok, NewPlayer} ->
            {[SkillId, ?SUCCESS], NewPlayer}
    end,
    {ok, BinData} = pt_147:write(14706, ProData),
    lib_server_send:send_to_sid(Player#player_status.sid, BinData),
    {ok, NewPlayer2};

handle(_Cmd, _Status, _Data) ->
    {error, "pp_visus no match"}.
