%%%-------------------------------------------------------------------
%%% @author liushl
%%% @doc
%%%     物品合成
%%% @end
%%%-------------------------------------------------------------------
-module(pp_compose).
-author("liushl").
-include("common.hrl").
-include("server.hrl").
%% API
-export([handle/3]).

%% 物品合成 pt_15101_[1,536870918104,1,[{536870918115,240082,1}],1]
handle(15101, Player, [RuleId, MainGoodsId, MainGoodsLocation, CostList, Times, ShopBuy]) ->
    CostList1 = lib_goods_util:merge_same_goods(CostList),
    CostList2 = [{GoodsId, TypeId, Num} || {GoodsId, TypeId, Num} <- CostList1, GoodsId =/= MainGoodsId],
    case lib_goods_do:goods_compose_or_evolution(RuleId, MainGoodsId, MainGoodsLocation, CostList2, Times, Player, ShopBuy) of
        {false, Err} ->
            {ok, BinData} = pt_151:write(15101, [Err, 0, Times]),
            lib_server_send:send_to_sid(Player#player_status.sid, BinData),
            ok;
        {fail, Player1} ->
            {ok, BinData} = pt_151:write(15101, [2, 0, Times]),
            lib_server_send:send_to_sid(Player#player_status.sid, BinData),
            {ok, Player1};
        {_, Player1, _, TargetGoodsId} ->
            {ok, BinData} = pt_151:write(15101, [1, TargetGoodsId, Times]),
            lib_server_send:send_to_sid(Player#player_status.sid, BinData),
            {ok, Player1}
    end;

handle(_Cmd, _Status, _Data) ->
    ?PRINT("pp_compose no match :~p~n", [{_Cmd, _Data}]),
    {error, "pp_compose no match"}.