%%%-------------------------------------------------------------------
%%% @author liushl
%%% @doc
%%%     装备 TODO 战场类应当禁止操作
%%% @end
%%%-------------------------------------------------------------------
-module(pp_equipment).
-author("liushl").

-include("server.hrl").
-include("errcode.hrl").
-include("equipment.hrl").

%% API
-export([handle/3]).

%% 装备信息
handle(Cmd = 15200, Player, _) ->
    BasicInfo = equipment_logic:equipment_basic_info(Player),
    lib_server_send:send_to_sid(Player#player_status.sid, pt_152, Cmd, BasicInfo),
    ok;
%% 穿装备
handle(Cmd = 15201, Player, [Series, Pos, GoodsId]) ->
    case equipment_logic:equipment_wear(Player, Series, Pos, GoodsId) of
        {false, Err} ->
            lib_server_send:send_to_sid(Player#player_status.sid, pt_152, Cmd, [Err, GoodsId, Series]),
            ok;
        {ok, NewPlayer} ->
            lib_server_send:send_to_sid(Player#player_status.sid, pt_152, Cmd, [1, GoodsId, Series]),
            {ok, NewPlayer}
    end;
%% 卸装备
handle(Cmd = 15202, Player, [GoodsId]) ->
    case equipment_logic:equipment_off(Player, GoodsId) of
        {false, Err} ->
            lib_server_send:send_to_sid(Player#player_status.sid, pt_152, Cmd, [Err]),
            ok;
        {ok, NewPlayer} ->
            lib_server_send:send_to_sid(Player#player_status.sid, pt_152, Cmd, [1]),
            {ok, NewPlayer}
    end;
%% 强化装备
handle(Cmd = 15203, Player, [GoodsId, Location, AutoBuy]) ->
    case equipment_logic:equipment_strength(Player, GoodsId, Location, AutoBuy) of
        {false, Err} ->
            lib_server_send:send_to_sid(Player#player_status.sid, pt_152, Cmd, [Err, 0]),
            ok;
        {fail, NewPlayer} ->
            lib_server_send:send_to_sid(Player#player_status.sid, pt_152, Cmd, [2, 0]),
            {ok, NewPlayer};
        {ok, NewPlayer, NewLv} ->
            lib_server_send:send_to_sid(Player#player_status.sid, pt_152, Cmd, [1, NewLv]),
            {ok, NewPlayer}
    end;
%% 装备进化升星 pt_15204_[536870920213,1,[],[536870912002],0]
handle(Cmd = 15204, Player, [GoodsId, Location, ExtraGoodsList, MarketBuy, IsAuto]) ->
    case equipment_logic:equipment_rise_star(Player, GoodsId, Location, lists:usort(ExtraGoodsList) -- [GoodsId], MarketBuy, IsAuto) of
        {false, Err} ->
            lib_server_send:send_to_sid(Player#player_status.sid, pt_152, Cmd, [Err, 0, GoodsId]);
        {fail, NewPlayer} ->
            lib_server_send:send_to_sid(Player#player_status.sid, pt_152, Cmd, [2, 0, GoodsId]),
            {ok, NewPlayer};
        {true, NewPlayer, AttrId} ->
            lib_server_send:send_to_sid(Player#player_status.sid, pt_152, Cmd, [1, AttrId, GoodsId]),
            {ok, NewPlayer}
    end;
%% 激活星级套装
handle(Cmd = 15206, Player, [Choice]) ->
    case equipment_logic:equipment_active_star(Player, Choice) of
        {false, Err} ->
            lib_server_send:send_to_sid(Player#player_status.sid, pt_152, Cmd, [Err, 0]),
            ok;
        {ok, NewPlayer, NewStars} ->
            lib_server_send:send_to_sid(Player#player_status.sid, pt_152, Cmd, [1, NewStars]),
            handle(15200, NewPlayer, []),
            {ok, NewPlayer}
    end;
%% 激活装备代数
handle(Cmd = 15207, Player, []) ->
    case equipment_logic:equipment_unlock_series(Player) of
        {false, Err} ->
            lib_server_send:send_to_sid(Player#player_status.sid, pt_152, Cmd, [Err, 0]),
            ok;
        {ok, NewPlayer, NewSeries} ->
            lib_server_send:send_to_sid(Player#player_status.sid, pt_152, Cmd, [1, NewSeries]),
            handle(15200, NewPlayer, []),
            {ok, NewPlayer}
    end;
%% 器灵信息
handle(Cmd = 15208, Player, []) ->
    #player_status{sid = Sid, equipment = #equipment{equipment_series = SeriesInfo}} = Player,
    EmpowerInfo = pack_empower(maps:to_list(SeriesInfo), []),
    lib_server_send:send_to_sid(Sid, pt_152, Cmd, [EmpowerInfo]),
    ok;
%% 装备 器灵
handle(Cmd = 15210, Player, [Series, Pos]) ->
    case equipment_logic:equipment_empower(Player, Series, Pos) of
        {false, Err} ->
            lib_server_send:send_to_sid(Player#player_status.sid, pt_152, Cmd, [Err, 0]),
            ok;
        {ok, NewPlayer, NextLv} ->
            lib_server_send:send_to_sid(Player#player_status.sid, pt_152, Cmd, [1, NextLv]),
            handle(15208, NewPlayer, []),
            {ok, NewPlayer}
    end;
%% 装备 进化升星预览
handle(Cmd = 15212, Player, [GoodsId, Lv, NowRatio]) ->
    case equipment_logic:rise_star_preview(Player, GoodsId, Lv, NowRatio) of
        {false, Err} ->
            lib_server_send:send_to_sid(Player#player_status.sid, pt_152, Cmd, [Err, []]);
        {true, List} ->
            lib_server_send:send_to_sid(Player#player_status.sid, pt_152, Cmd, [1, List])
    end,
    ok;
%% 宝石信息
handle(Cmd = 15213, Player, []) ->
    #player_status{sid = Sid, equipment = #equipment{equipment_series = SeriesInfo}} = Player,
    GemsInfo = pack_gems(maps:to_list(SeriesInfo), []),
    lib_server_send:send_to_sid(Sid, pt_152, Cmd, [GemsInfo]),
    ok;
%% 宝石信息
handle(Cmd = 15214, Player, [Series, Pos]) ->
    #player_status{sid = Sid, equipment = #equipment{equipment_series = SeriesInfo}} = Player,
    #equipment_series{gems = Gems} = equipment_api:get_equipment_series_pos_info(SeriesInfo, Series, Pos),
    GemsInfo = [GoodsId || {_, GoodsId} <- Gems],
    lib_server_send:send_to_sid(Sid, pt_152, Cmd, [Series, Pos, GemsInfo]),
    ok;
%% 宝石镶嵌
handle(Cmd = 15215, Player, [Series, Pos, GoodsId]) ->
    case equipment_logic:equipment_gems_inlay(Player, Series, Pos, GoodsId) of
        {false, Err} ->
            lib_server_send:send_to_sid(Player#player_status.sid, pt_152, Cmd, [Err]),
            ok;
        {true, NewPlayer} ->
            lib_server_send:send_to_sid(Player#player_status.sid, pt_152, Cmd, [1]),
            handle(15214, NewPlayer, [Series, Pos]),
            {true, NewPlayer}
    end;
%% 宝石卸下
handle(Cmd = 15216, Player, [Series, Pos, GoodsId]) ->
    case equipment_logic:equipment_gems_unload(Player, Series, Pos, GoodsId) of
        {false, Err} ->
            lib_server_send:send_to_sid(Player#player_status.sid, pt_152, Cmd, [Err]),
            ok;
        {true, NewPlayer} ->
            lib_server_send:send_to_sid(Player#player_status.sid, pt_152, Cmd, [1]),
            handle(15214, NewPlayer, [Series, Pos]),
            {true, NewPlayer}
    end;
%% 宝石升级-对于已镶嵌的
handle(Cmd = 15217, Player, [Series, Pos, GoodsId]) ->
    case equipment_logic:equipment_gems_upgrade(Player, Series, Pos, GoodsId) of
        {false, Err} ->
            lib_server_send:send_to_sid(Player#player_status.sid, pt_152, Cmd, [Err]),
            ok;
        {true, NewPlayer} ->
            lib_server_send:send_to_sid(Player#player_status.sid, pt_152, Cmd, [1]),
            handle(15214, NewPlayer, [Series, Pos]),
            {true, NewPlayer}
    end;

%% 装备收集信息
handle(Cmd = 15218, Player, []) ->
    Info = equipment_collect:send_collect_info(Player),
    {ok, Bin} = pt_152:write(Cmd, [Info]),
    lib_server_send:send_to_sid(Player#player_status.sid, Bin),
    ok;

%% 装备收集-激活进度
handle(Cmd = 15220, Player, [Series, Color]) ->
    case equipment_collect:active(Player, Series, Color) of
        {false, Err} ->
            lib_server_send:send_to_sid(Player#player_status.sid, pt_152, Cmd, [Err, Series, Color, 0]),
            ok;
        {true, NewPlayer, NewActive} ->
            lib_server_send:send_to_sid(Player#player_status.sid, pt_152, Cmd, [1, Series, Color, NewActive]),
            {true, NewPlayer}
    end;

%% 装备收集-跟随守护
handle(_Cmd = 15221, Player, [GoodsId, Status]) when Status =:= 0 orelse Status =:= 1 ->
    case equipment_logic:equipment_follow_guard(Player, GoodsId, Status) of
        {false, _Err} ->
            ok;
        {true, NewPlayer} ->
            {true, NewPlayer}
    end;

%%  宝石-一键镶嵌
handle(Cmd = 15222, Player, [Series]) ->
    case equipment_logic:auto_set_gems(Player, Series) of
        {false, Err} ->
            lib_server_send:send_to_sid(Player#player_status.sid, pt_152, Cmd, [Err, []]),
            ok;
        {true, NewPlayer, ChangePos} ->
            lib_server_send:send_to_sid(Player#player_status.sid, pt_152, Cmd, [1, ChangePos]),
            handle(15213, NewPlayer, []),
            {true, NewPlayer}
    end;

%% 宝石套装:宝石套装信息
handle(Cmd = 15223, Player, []) ->
    #player_status{sid = Sid, equipment = #equipment{gems_suit = GemsSuit}} = Player,
    lib_server_send:send_to_sid(Sid, pt_152, Cmd, [GemsSuit]),
    ok;

%% 宝石套集-激活进度
handle(Cmd = 15224, Player, [Series]) ->
    case equipment_logic:equipment_gems_active_suit(Player, Series) of
        {false, Err} ->
            lib_server_send:send_to_sid(Player#player_status.sid, pt_152, Cmd, [Err, Series, 0]),
            ok;
        {true, NewPlayer, NewActive} ->
            lib_server_send:send_to_sid(Player#player_status.sid, pt_152, Cmd, [1, Series, NewActive]),
            handle(15223, NewPlayer, []),
            {true, NewPlayer}
    end;

%% 特殊装备成长属性信息
handle(Cmd = 15225, Player, [Series, Pos]) ->
    #player_status{sid = Sid} = Player,
    [{Lv, Exp}] = equipment_api:get_equipment_sp_attr(Player, Series, Pos),
    lib_server_send:send_to_sid(Sid, pt_152, Cmd, [Series, Pos, Lv, Exp]),
    ok;

%%  装备战力
handle(Cmd = 15226, Player, []) ->
    #player_status{sid = Sid} = Player,
    Power = equipment_api:get_equipment_power(Player),
    lib_server_send:send_to_sid(Sid, pt_152, Cmd, [Power]),
    ok;

handle(Cmd, _Player, Arg) ->
    erlang:display({?MODULE, ?LINE, Cmd, Arg}),
    ok.

%% 宝石打包
pack_gems([], List) -> List;
pack_gems([{_, PosInfo}|T], List) ->
    NewList = pack_gem_pos(maps:to_list(PosInfo), List),
    pack_gems(T, NewList).

pack_gem_pos([], List) -> List;
pack_gem_pos([{_, #equipment_series{series = Series, pos = Pos, gems = Gems}}|T], List) ->
    One = {Series, Pos, [GoodsId || {_, GoodsId} <- Gems]},
    pack_gem_pos(T, [One|List]).

%% 器灵打包
pack_empower([], List) -> List;
pack_empower([{_, PosInfo}|T], List) ->
    NewList = pack_empower_pos(maps:to_list(PosInfo), List),
    pack_empower(T, NewList).

pack_empower_pos([], List) -> List;
pack_empower_pos([{_, #equipment_series{series = Series, pos = Pos, empower = Lv}}|T], List) ->
    One = {Series, Pos, Lv},
    pack_empower_pos(T, [One|List]).
