%% ---------------------------------------------------------------------------
%% @doc 拍卖行
%% @author hek
%% @since  2016-11-14
%% @deprecated
%% ---------------------------------------------------------------------------

-module (lib_auction).
-include ("common.hrl").
-include ("rec_auction.hrl").
-include ("server.hrl").
-include ("errcode.hrl").
-include ("predefine.hrl").
-include ("def_fun.hrl").
-include ("figure.hrl").
-include ("goods.hrl").
-include ("def_goods.hrl").
-export ([pay_auction/2]).
-export ([
    %% 宝箱拍卖提示
    add_goods_tip/3
    %% 增加个人拍卖
    , add_personal_goods/4
    %% 个人拍卖：扣除物品
    , cost_personal_goods/3
]).

pay_auction(PS, {_PlayerId, _GuildId, _Cmd, _GoodsId, PriceType, PriceList} = Args) ->
	CheckList  = [{correct_price, PriceType},{enough_money, PS, PriceList}],
	case checklist(CheckList) of
		true -> mod_auction:pay_auction(Args), skip;
		{false, Res} -> {false, Res}
	end.

-type object_list() :: [{ObjectType::integer(), GoodsTypeId::integer(), GoodsNum::integer()}].
%% ---------------------------------------------------------------------------
%% @doc 宝箱拍卖提示
-spec add_goods_tip(PS, GiveList, UpdateGoodsList) -> NewPS when
    PS          :: #player_status{},
    GiveList    :: object_list(),
    UpdateGoodsList :: [#goods{}],
    NewPS       :: #player_status{}.
%% ---------------------------------------------------------------------------
add_goods_tip(PS, GiveList, _UpdateGoodsList) ->
    WorldLv = util:get_world_lv(),
    RoleAuction = PS#player_status.role_auction,
    #status_auction{goods_list = OldGoodsList, auto_id = AutoId} = RoleAuction,
    % List = [{Type, GoodsTypeId, GoodsNum}||{Type, GoodsTypeId, GoodsNum}<-GiveList,
    %     (Type==?TYPE_GOODS orelse Type == ?TYPE_HERO_EQUIP ) andalso data_auction:get_goods(GoodsTypeId, WorldLv)=/=[] ],
    List = [{Type, GoodsTypeId, GoodsNum}||{Type, GoodsTypeId, GoodsNum}<-GiveList,
                (Type==?TYPE_GOODS) andalso data_auction:get_goods(GoodsTypeId, WorldLv)=/=[] ],
    NewAutoId = AutoId + 1,
    case List of
        %% 上架拍卖物品物品
        [{Type = ?TYPE_GOODS, GoodsTypeId, GoodsNum}|_] ->
            #base_auction_goods{personal_base_price = PersonalBasePrice,
                personal_one_price = PersonalOnePrice} = data_auction:get_goods(GoodsTypeId, WorldLv),
            Charge = round(PersonalBasePrice*?ADD_PERSONAL_CHARGE),
            Args = [NewAutoId, GoodsTypeId, PersonalBasePrice, PersonalOnePrice, Charge],
            lib_server_send:send_to_sid(PS#player_status.sid, pt_154, 15409, Args),
            NewGoodsList = [{NewAutoId, {Type, GoodsTypeId, GoodsNum}}]++OldGoodsList,
            NewRoleAuction = RoleAuction#status_auction{goods_list = NewGoodsList, auto_id = NewAutoId},
            PS#player_status{role_auction = NewRoleAuction};

        %% 上架拍卖英灵装备
        % [{Type = ?TYPE_HERO_EQUIP, GoodsTypeId, _GoodsNum}|_] ->
        %     HeroEquipList = lists:filter(fun(#goods{goods_id = TypeId, location = Location}) ->
        %             TypeId == GoodsTypeId andalso Location == ?GOODS_LOC_HERO_BAG end, UpdateGoodsList),
        %     case HeroEquipList of
        %         [#goods{id = GoodsId}|_] ->
        %             ClientAutoId = GoodsId,
        %             #base_auction_goods{personal_base_price = PersonalBasePrice,
        %                 personal_one_price = PersonalOnePrice} = data_auction:get_goods(GoodsTypeId, WorldLv),
        %             Charge = round(PersonalBasePrice*?ADD_PERSONAL_CHARGE),
        %             Args = [ClientAutoId, GoodsTypeId, PersonalBasePrice, PersonalOnePrice, Charge],
        %             lib_server_send:send_to_sid(PS#player_status.sid, pt_154, 15409, Args),
        %             NewGoodsList = [{ClientAutoId, {Type, GoodsTypeId, 1}}]++OldGoodsList,
        %             NewRoleAuction = RoleAuction#status_auction{goods_list = NewGoodsList},
        %             PS#player_status{role_auction = NewRoleAuction};
        %         _ -> PS
        %     end;
        _ ->
            ?INFO("missing cfg:~p~n", [{GiveList}]),
            PS
    end.

%% ---------------------------------------------------------------------------
%% @doc 增加个人拍卖：确认拍卖
-spec add_personal_goods(PS, AutoId, GoodsTypeId, Type) -> {ok, NewPS} when
    PS          :: #player_status{},
    AutoId      :: integer(),
    GoodsTypeId :: integer(),
    Type        :: integer(),
    NewPS       :: #player_status{}.
%% ---------------------------------------------------------------------------
add_personal_goods(PS, AutoId, GoodsTypeId, Type = 1) ->
    GoodsList = get_personal_goods_list(PS, AutoId, GoodsTypeId),
    CheckList  = [is_correct_goods, is_config, is_goods_enough],
    case check_add_personal_goods(PS, GoodsList, CheckList) of
        {true, NewPS} ->
            PlayerId = PS#player_status.id,
            PlayerName = PS#player_status.figure#figure.name,
            mod_auction:add_personal_goods({PlayerId, PlayerName, AutoId, GoodsTypeId, GoodsList}),
            {ok, NewPS};
        {false, Res, NewPS} ->
            lib_server_send:send_to_sid(PS#player_status.sid, pt_154, 15410, [Res, AutoId, GoodsTypeId, Type]),
            {ok, NewPS}
    end;
%% 增加个人拍卖：取消拍卖
add_personal_goods(PS, AutoId, GoodsTypeId, 2) ->
    NewPS = delete_personal_goods(PS, AutoId),
    lib_server_send:send_to_sid(PS#player_status.sid, pt_154, 15410, [?SUCCESS, AutoId, GoodsTypeId, 2]),
    {ok, NewPS};
add_personal_goods(PS, AutoId, GoodsTypeId, Type) ->
    % ?ERR("add_personal_goods:~p~n", [{AutoId, GoodsTypeId, Type}]),
    lib_server_send:send_to_sid(PS#player_status.sid, pt_154, 15410, [?ERRCODE(err154_error_args), AutoId, GoodsTypeId, Type]),
    {ok, PS}.

delete_personal_goods(PS, AutoId) ->
    RoleAuction = PS#player_status.role_auction,
    AuctionGoodsList = RoleAuction#status_auction.goods_list,
    NewAuctionGoodsList = lists:keydelete(AutoId, 1, AuctionGoodsList),
    NewPS = PS#player_status{role_auction = RoleAuction#status_auction{goods_list = NewAuctionGoodsList}},
    NewPS.

get_personal_goods_list(PS, AutoId, GoodsTypeId) ->
    AuctionGoodsList = PS#player_status.role_auction#status_auction.goods_list,
    case lists:keyfind(AutoId, 1, AuctionGoodsList) of
        {AutoId, {?TYPE_GOODS,  GoodsTypeId, GoodsNum}} -> [{?TYPE_GOODS,  GoodsTypeId, GoodsNum}];
        _ -> []
    end.

%% ---------------------------------------------------------------------------
%% @doc 个人拍卖：扣除物品
-spec cost_personal_goods(PS, AutoId, OldGoodsList) -> {true, NewPS} | {false, Res, NewPS} when
    PS          :: #player_status{},
    AutoId      :: integer(),
    OldGoodsList:: object_list(),
    Res         :: integer(),
    NewPS       :: #player_status{}.
%% ---------------------------------------------------------------------------
cost_personal_goods(PS, AutoId, [{ObjectType, GoodsTypeId, GoodsNum}] = _OldGoodsList) ->
    %% 再次确认下AutoId是否存在
    CheckList  = [is_correct_goods],
    case get_personal_goods_list(PS, AutoId, GoodsTypeId) of
        [{ObjectType, GoodsTypeId, GoodsNum}] = GoodsList -> skip;
        _ -> GoodsList = []
    end,
    case check_add_personal_goods(PS, GoodsList, CheckList) of
        {true, NewPS} when ObjectType == ?TYPE_GOODS ->
            case lib_goods_api:cost_object_list_with_check(PS, GoodsList, add_personal_goods, "") of
                {true, NewPS,_} ->
                    LastPS = delete_personal_goods(NewPS, AutoId),
                    {true, LastPS};
                {false, Res, NewPS} -> {false, Res, NewPS}
            end;
        % {true, NewPS} when ObjectType == ?TYPE_HERO_EQUIP ->
        %     case catch lib_goods_api:delete_hero_one_by_id(PS, AutoId, 1, add_personal_goods) of
        %         1 ->
        %             LastPS = delete_personal_goods(NewPS, AutoId),
        %             {true, LastPS};
        %         Res -> {false, Res, NewPS}
        %     end;
        {true, NewPS} ->
            ?ERR("unkown cost_personal_goods ObjectType:~p~n", [{PS#player_status.id, ObjectType}]),
            {false, ?FAIL, NewPS};
        {false, Res, NewPS} ->  {false, Res, NewPS}
    end.

check_add_personal_goods(PS, _GoodsList, []) -> {true, PS};
check_add_personal_goods(PS, GoodsList, [H|T]) ->
    case do_check_add_personal_goods(PS, GoodsList, H) of
        {true, NewPS} -> check_add_personal_goods(NewPS, GoodsList, T);
        {false, Res, NewPS} -> {false, Res, NewPS}
    end.

do_check_add_personal_goods(PS, GoodsList, is_correct_goods) ->
    case GoodsList of
        [{?TYPE_GOODS,  _GoodsTypeId, _GoodsNum}|_] -> {true, PS};
        _ -> {false, ?ERRCODE(err154_error_goods), PS}
    end;
do_check_add_personal_goods(PS, [{?TYPE_GOODS,  GoodsTypeId, _GoodsNum}|_], is_config) ->
    WorldLv = util:get_world_lv(),
    case data_auction:get_goods(GoodsTypeId, WorldLv) of
        #base_auction_goods{} -> {true, PS};
        _ -> {false, ?ERRCODE(err154_missing_config), PS}
    end;
% do_check_add_personal_goods(PS, _AutoId, ?TYPE_HERO_EQUIP, _GoodsTypeId, is_goods_enough) ->
%     %% 英灵装备跟进唯一id扣除材料，此处不做检查，扣除的地方检查
%     {true, PS};
do_check_add_personal_goods(PS, [{?TYPE_GOODS,  _GoodsTypeId, _GoodsNum}|_] = GoodsList, is_goods_enough) ->
    case lib_goods_api:check_object_list(PS, GoodsList) of
        true -> {true, PS};
        {false, ErrCode} -> {false, ErrCode, PS}
    end;
do_check_add_personal_goods(PS, _GoodsList, _T) ->
    ?ERR("do_check_add_personal_goods:~p~n", [_T]),
    {false, ?FAIL, PS}.

%% ---------------------------- check function %% ----------------------------
check({correct_price, PriceType}) ->
	CorrectType = lists:member(PriceType, [?PRICE_TYPE_AUCTION, ?PRICE_TYPE_ONE_PRICE, ?PRICE_TYPE_ADD_PRICE]),
	if
		CorrectType == false ->
			%% 非法竞价类型
			{false, ?ERRCODE(err154_error_pricetype)};
		true -> true
	end;

check({enough_money, PS, ObjectList}) ->
    case lib_goods_api:check_object_list(PS, ObjectList, pay_auction) of
    	true ->
            true;
    	{false, Res} ->
            {false, Res}
    end;

check(_Type) ->
    {false, ?FAIL}.

checklist([]) -> true;
checklist([H|T]) ->
    case check(H) of
        true -> checklist(T);
        {false, Res} -> {false, Res}
    end.



