%% ---------------------------------------------------------------------------
%% @doc    星辰系统
%% @author  zmh
%% @since  2020-08-17
%% ---------------------------------------------------------------------------
-module(pp_galaxy).

-include("server.hrl").
-include("errcode.hrl").
-include("common.hrl").
-include("predefine.hrl").
-include("def_module.hrl").
-include("figure.hrl").

-export([handle/3]).


handle(Cmd, Status, ProData) ->
    #player_status{figure = #figure{lv = Lev}} = Status,
    ModuleLev = lib_module:get_open_lv(?MOD_GALAXY),
    case Lev >= ModuleLev of
        true ->
            handle_do(Cmd, Status, ProData);
        false ->
            ok
    end.

%% 星辰升级
handle_do(14800, Status, [Loc,Uid]) ->
    #player_status{sid=Sid}=Status,
    case lib_role_galaxy:upgrade_galaxy(Status,Loc,Uid) of
        {false, Result} ->
            NStatus = Status,
            Lev = 0;
        {ok, NStatus, Lev} ->
            lib_task_api:galaxy_lv(NStatus),
            Result = ?SUCCESS
    end,
    {ok, Bin} = pt_148:write(14800, [Result,Loc,Uid,Lev]),
    lib_server_send:send_to_sid(Sid, Bin),
    {ok, NStatus};

%% 查询星辰上阵信息
handle_do(14801, Status, [OpTy]) ->
    lib_role_galaxy:send_galaxy_equip_info(OpTy,Status),
    {ok,Status};

%% 星辰槽位开启
handle_do(14802, Status, [Pos,Auto]) ->
    #player_status{sid=Sid}=Status,
    OpTy  = data_galaxy:get_pos_type(Pos),
    case lib_role_galaxy:active_galaxy_pos(Status, Pos, Auto) of
        {false, Result} ->
            NStatus = Status;
        {ok, NStatus} ->
            Result = ?SUCCESS
    end,
    {ok, Bin} = pt_148:write(14802, [Result,OpTy,Pos]),
    lib_server_send:send_to_sid(Sid, Bin ),
    {ok, NStatus};


%% 星辰上阵
handle_do(14803, Status, [Pos, Uid]) ->
    #player_status{sid=Sid}=Status,
    case lib_role_galaxy:equip_galaxy(Status, Pos, Uid) of
        {false, Result} ->
            NStatus = Status;
        {ok, NStatus} ->
            OpTy = data_galaxy:get_pos_type(Pos),
            lib_role_galaxy:send_galaxy_equip_info(OpTy,NStatus),
            mod_scene_agent:update(NStatus, [{battle_attr, NStatus#player_status.battle_attr}]),
            Result = ?SUCCESS
    end,
    {ok, Bin} = pt_148:write(14803, [Result,Pos, Uid]),
    lib_server_send:send_to_sid(Sid, Bin),
    {ok, NStatus};

%%技能培养
handle_do(14804, Status, [Uid, Pos, CUid]) ->
    #player_status{sid=Sid}=Status,
    {Reason, NStatus, SkillId, SkillLev} =
    case lib_role_galaxy:upgrade_galaxy_skill(Status, Uid, Pos, CUid) of
        {false, Rea} ->
            {Rea, Status, 0, 0};
        {ok, NStatus0, Id, Lev} ->
            {?SUCCESS, NStatus0, Id, Lev}
    end,
    Data = [Reason,Uid, Pos, SkillId, SkillLev,  CUid],
    {ok, Bin} = pt_148:write(14804, Data),
    lib_server_send:send_to_sid(Sid, Bin),
    {ok, NStatus};

%% 星辰分解
handle_do(14805, Status, [Uids]) ->
    #player_status{sid=Sid}=Status,
    {Reason, GoodsList} =
    case lib_role_galaxy:resolve_galaxy(Uids,Status) of
        {false, Rea} ->
            {Rea, []};
        {ok, List} ->
            {?SUCCESS, List}
    end,
    {ok, Bin} = pt_148:write(14805, [Reason, GoodsList]),
    lib_server_send:send_to_sid(Sid, Bin),
    {ok,Status};



%% 星辰回退
handle_do(14806, Status, [Uid]) ->
    #player_status{sid=Sid}=Status,
    {Reason, GoodsList} =
    case lib_role_galaxy:reset_galaxy(Uid,Status) of
        {false, Rea} ->
            {Rea, []};
        {ok, List} ->
            {?SUCCESS, List}
    end,
    {ok, Bin} = pt_148:write(14806, [Reason, GoodsList]),
    lib_server_send:send_to_sid(Sid, Bin),
    {ok,Status};

handle_do(14807,Status,[])->
    lib_role_galaxy:send_galaxy_skin_info(Status),
    {ok,Status};

handle_do(14808,Status,[OpTy,SkinId])->
    #player_status{sid=Sid}=Status,
    case lib_role_galaxy:use_galaxy_skin(Status,OpTy,SkinId) of
        {ok,NStatus}->
            Result = ?SUCCESS;
        {false,Result}->
            NStatus = Status
    end,
    {ok, Bin} = pt_148:write(14808, [Result, OpTy, SkinId]),
    lib_server_send:send_to_sid(Sid, Bin),
    {ok,NStatus};

handle_do(14809,Status,[SkinId])->
    #player_status{sid=Sid}=Status,
    case lib_role_galaxy:upgrade_galaxy_skin(SkinId,Status) of
        {ok,NStar,NStatus}->
            Result = ?SUCCESS;
        {false,Result}->
            NStatus = Status,
            NStar = 0
    end,
    {ok,Bin} = pt_148:write(14809,[Result,SkinId,NStar]),
    lib_server_send:send_to_sid(Sid,Bin),
    {ok,NStatus};

%%物品列表
handle_do(14810, Status, _) ->
    NewStatus = lib_role_galaxy:send_shop_list(Status),
    {ok, NewStatus};

%% 刷新物品
handle_do(14811, Status, _) ->
    #player_status{sid=Sid}=Status,
    {Reason, NewStatus} = case lib_role_galaxy:refresh_item_list(Status) of
        {false, Rea} ->
            {Rea, Status};
        {ok, Status2} ->
            lib_achievement_api:galaxy_shop_refresh_event(Status2, 1),
            {?SUCCESS, Status2}
    end,
    {ok, Bin} = pt_148:write(14811, [Reason]),
    lib_server_send:send_to_sid(Sid, Bin),
    {ok, NewStatus};

%% 购买物品
handle_do(14812, Status, [Pos]) ->
    #player_status{sid=Sid}=Status,
    {Reason, NewStatus} = case lib_role_galaxy:buy_shop_item(Status, Pos) of
        {false, Rea} ->
            {Rea, Status};
        {ok, Status2} ->
            lib_achievement_api:galaxy_shop_buy_event(Status2, 1),
            {?SUCCESS, Status2}
    end,
    {ok, Bin} = pt_148:write(14812, [Pos, Reason]),
    lib_server_send:send_to_sid(Sid, Bin),
    {ok, NewStatus};

handle_do(14813,Status,[])->
    lib_role_galaxy:send_galaxy_collect(Status),
    {ok,Status};

handle_do(14815,Status,[Type,SubT])->
    #player_status{sid=Sid}=Status,
    case lib_role_galaxy:fetch_galaxy_colllect(Type,SubT,Status) of
        {ok,NStatus}->
            Result = ?SUCCESS;
        {false,Result}->
            NStatus = Status
    end,
    {ok,Bin} = pt_148:write(14815,[Result,Type,SubT]),
    lib_server_send:send_to_sid(Sid,Bin),
    {ok,NStatus};

handle_do(14816,Status,[Dis])->
    NStatus = lib_role_galaxy:set_show_galaxy(Status,Dis),
    {ok,NStatus};

%% 宠物生活技能的每日免费刷新次数
handle_do(14817, Status, [])->
    FreeTimes = lib_pet_api:pet_life_skill_about_galaxy_store_refresh_free(Status),
    UseTimes  = lib_daily:get_count(Status#player_status.id, ?MOD_GALAXY, 1),
    lib_server_send:send_to_sid(Status#player_status.sid, pt_148, 14817,
        [max(0, FreeTimes-UseTimes)]
    ),
    {ok, Status};

handle_do(_Cmd, _Status, _Data) ->
    {error, "pp_galaxy no match"}.


