%%%-------------------------------------------------------------------
%%% @author : tyl
%%% @doc    时装工坊检查模块
%%% Module  : lib_workroom_check.erl
%%% Created : 2021-05-21
%%% @end
%%%-------------------------------------------------------------------
-module(lib_workroom_check).

-include("fashion_workroom.hrl").
-include("common.hrl").
-include("server.hrl").
-include("predefine.hrl").
-include("errcode.hrl").

%% API
-export([
	check_change_forge_target/1
	,check_forge_good/3
	,check_active_suit/1
	,check_buy_good/1
]).

check_change_forge_target([]) -> true;
check_change_forge_target([Check|T]) ->
	case do_check_change_forge_target(Check) of
		true -> check_change_forge_target(T);
		{false, Err} -> {false, Err}
	end.

check_forge_good([], _Player, _GoodsId) -> true;
check_forge_good([Check|T], Player, GoodsId) ->
	case do_check_forge_good(Check, Player, GoodsId) of
		true -> check_forge_good(T, Player, GoodsId);
		{false, Err} -> {false, Err}
	end.

check_active_suit([]) -> true;
check_active_suit([Check|T]) ->
	case do_check_active_suit(Check) of
		true -> check_active_suit(T);
		{false, Err} -> {false, Err}
	end.

check_buy_good([]) -> true;
check_buy_good([Check|T]) ->
	case do_check_buy_good(Check) of
		true -> check_buy_good(T);
		{false, Err} -> {false, Err}
	end.

do_check_change_forge_target({check_cfg, GoodsId}) ->
	case data_fashion_workroom:get_forge_goods(GoodsId) of
		#base_workroom_forge{} -> true;
		_ ->  {false, ?ERRCODE(err421_forge_cfg_err)}
	end;
do_check_change_forge_target({has_on_pos, TargetList, GoodsId}) ->
	case lists:keyfind(GoodsId, 2, TargetList) of
		{_, _} -> {false, ?ERRCODE(err421_has_on_pos)};
		_ -> true
	end;
do_check_change_forge_target(_Check) ->
	?ERR("<<Error check_change_forge_target>>"),
	{false, ?FAIL}.

do_check_forge_good(forge_good, _Player, GoodId) ->
	case data_fashion_workroom:get_forge_goods(GoodId) of
		#base_workroom_forge{} -> true;
		_ -> {false, ?ERRCODE(err421_forge_cfg_err)}
	end;
do_check_forge_good(forge_material, _Player, GoodsId) ->
	case data_fashion_workroom:get_forge_goods(GoodsId) of
		#base_workroom_forge{cost = CostList} ->
			case CostList of
				[{_, _, _}|_] -> true;
				_ -> {false, ?ERRCODE(err421_forge_cfg_err)}
			end;
		_ -> {false, ?ERRCODE(err421_forge_cfg_err)}
	end;
do_check_forge_good(material_enough, Player, GoodsId) ->
	#base_workroom_forge{cost = CostList} =
		data_fashion_workroom:get_forge_goods(GoodsId),
	F = fun(CostGood) ->
		{_, CostId, NeedNum} = CostGood,
		[{_, HasNum}] = lib_goods_api:get_goods_num(Player, [CostId]),
		HasNum >= NeedNum
	    end,
	case lists:all(F, CostList) of
		true -> true;
		_ -> {false, ?ERRCODE(err421_material_not_enough)}
	end;
do_check_forge_good(_Check, _, _) ->
	?ERR("<<Error check_forge_good>>"),
	{false, ?FAIL}.

do_check_active_suit({check_cfg, SuitId, ActiveNum}) ->
	case data_fashion_workroom:get_suit(SuitId) of
		#base_workroom_suit{attr_list = AttrList} ->
			case lists:keyfind(ActiveNum, 1, AttrList) of
				{_, _} -> true;
				_ -> {false, ?ERRCODE(err421_max_suit_num)}
			end;
		_ ->
			{false, ?ERRCODE(err421_suit_cfg_err)}
	end;
do_check_active_suit({can_active, OwnNum, ActiveNum}) ->
	case OwnNum >= ActiveNum of
		true -> true;
		_ -> {false, ?ERRCODE(err421_cannot_active)}
	end;
do_check_active_suit(_Check) ->
	?ERR("<<Error check_active_suit>>"),
	{false, ?FAIL}.

do_check_buy_good({has_buy, ShopGoods, Pos}) ->
	case lists:keyfind(Pos, #shop_good.pos, ShopGoods) of
		#shop_good{has_buy = HasBuy} ->
			case HasBuy > 0 of
				true -> {false, ?ERRCODE(err421_has_buy)};
				_ -> true
			end;
		_ -> {false, ?ERRCODE(err421_has_not_good)}
	end;
do_check_buy_good({price_cfg, ShopGoods, Pos}) ->
	case lists:keyfind(Pos, #shop_good.pos, ShopGoods) of
		#shop_good{goods_id = GoodsId} ->
			case data_fashion_workroom:get_good_cfg(GoodsId) of
				#base_workroom_store{price_list = [_|_] = _PriceList} ->
					true;
				_ ->
					{false, ?ERRCODE(err421_good_cfg_err)}
			end;
		_ -> {false, ?ERRCODE(err421_has_not_good)}
	end;
do_check_buy_good(_Check) ->
	?ERR("<<Error check_buy_good>>"),
	{false, ?FAIL}.
