%%%-------------------------------------------------------------------
%%% @doc    衣橱逻辑模块
%%% Module  : lib_wardrobe_active.erl
%%% Created : 2020-11-19
%%% @Author : tyl
%%% @end
%%%-------------------------------------------------------------------
-module(lib_wardrobe_active).

-include("wardrobe.hrl").
-include("def_module.hrl").
-include("def_goods.hrl").
-include("def_fun.hrl").
-include("common.hrl").
-include("server.hrl").
-include("figure.hrl").
-include("goods.hrl").
-include("profile_photo.hrl").
-include("predefine.hrl").
-include("rec_dress_up.hrl").
-include("fashion.hrl").
-include("errcode.hrl").

%% API
-compile([export_all]).

%% 异步触发
async_active(PlayerId, M, F, A) when is_integer(PlayerId) ->
	lib_player:apply_cast(PlayerId, ?APPLY_CAST_SAVE, M, F, A);
async_active(_PlayerId, _M, _F, _A) -> skip.

%% -------------------------------------------------------------
%% @doc 激活时装
%%  格式[{{sub_type,color},num}...]
%% @end
%% -------------------------------------------------------------
active_fashion(Player, KeyValues, NotOpenNum, GoodsId) ->
	Type  = ?WARDROBE_TYPE_FASHION,
	Color = get_goods_color(GoodsId),
	active_appear(Player, Type, KeyValues, [GoodsId, Color], NotOpenNum).

%% -------------------------------------------------------------
%% @doc 激活称号
%%  格式[{{sub_type,color},num}...],注意:称号只有一个sub_type-1
%% @end
%% -------------------------------------------------------------
active_desg(Player, KeyValues, GoodsId) ->
	Type  = ?WARDROBE_TYPE_DESG,
	Color = get_goods_color(GoodsId),
	active_appear(Player, Type, KeyValues, [GoodsId, Color]).

%% -------------------------------------------------------------
%% @doc 激活头像
%%  格式[{{sub_type,color},num}...],注意:头像只有一个sub_type-1
%% @end
%% -------------------------------------------------------------
active_picture(Player, KeyValues, NotOpenNum, GoodsId) ->
	Type  = ?WARDROBE_TYPE_PICTURE,
	Color = get_goods_color(GoodsId),
	active_appear(Player, Type, KeyValues, [GoodsId, Color], NotOpenNum).

%% -------------------------------------------------------------
%% @doc 激活相框/气泡
%%  格式[{{sub_type,color},num}...],
%%  注意:装扮有两个type-1/2,但每个大类下只有一个sub_type-1
%% @end
%% -------------------------------------------------------------
active_dress_up(Player, DressType, KeyValues, NotOpenNum, GoodsId) ->
	case DressType of
		?DRESS_UP_FRAME -> Type = ?WARDROBE_TYPE_FRAME;
		_ -> Type = ?WARDROBE_TYPE_BUBBLE
	end,
	Color = get_goods_color(GoodsId),
	active_appear(Player, Type, KeyValues, [GoodsId, Color], NotOpenNum).

%% -------------------------------------------------------------
%% @doc 激活珍宝
%%  格式[{{sub_type,color},num}...]
%% @end
%% -------------------------------------------------------------
active_foster_skin(Player, KeyValues, GoodsId) ->
	Type  = ?WARDROBE_TYPE_FOSTER_SKIN,
	Color = get_goods_color(GoodsId),
	active_appear(Player, Type, KeyValues, [GoodsId, Color]).

%% -------------------------------------------------------------
%% @doc 激活宝宝时装
%%  格式[{{sub_type,color},num}...],注意:宝宝时装只有一个sub_type-1
%% @end
%% -------------------------------------------------------------
active_child_fashion(Player, KeyValues, GoodsId) ->
	Type  = ?WARDROBE_TYPE_CHILD_FASHION,
	Color = get_goods_color(GoodsId),
	active_appear(Player, Type, KeyValues, [GoodsId, Color]).

%% -------------------------------------------------------------
%% @doc 激活星辰皮肤
%%  格式[{{sub_type,color},num}...],注意:星辰皮肤只有一个sub_type-1
%% @end
%% -------------------------------------------------------------
active_galaxy(Player, KeyValues, GoodsId) ->
	Type  = ?WARDROBE_TYPE_GALAXY,
	Color = get_goods_color(GoodsId),
	active_appear(Player, Type, KeyValues, [GoodsId, Color]).

%% -------------------------------------------------------------
%% @doc 激活进阶/伙伴
%%  格式[{{sub_type,color},num}...]
%%  type-9 进阶 type-10 伙伴
%% @end
%% -------------------------------------------------------------
active_foster(Player, Type, KeyValues, IdColor) ->
	active_appear(Player, Type, KeyValues, IdColor).


%% 物品颜色(品质)
get_goods_color(GoodsId) ->
	case data_goods_type:get(GoodsId) of
		#ets_goods_type{color = Color} -> ok;
		_ -> Color = 0
	end,
	Color.

%% -------------------------------------------------------------
%% @doc 激活外观
%% @param:
%%  Type-大类
%%  KeyValues-激活外观[{{sub_type,color},num}...]
%%  IdColor-进阶、伙伴类型/物品Id [Id, Stage]/[GoodsTypeId, Color]
%%  NotOpenNum-还没有到开放时间但已经激活的外观,默认数量为0
%% @end
%% -------------------------------------------------------------
active_appear(Player, Type, KeyValues, IdColor) ->
	active_appear(Player, Type, KeyValues, IdColor, 0).
active_appear(Player, Type, KeyValues, IdColor, NotOpenNum) ->
	#player_status{sid = Sid, id = RoleId, wardrobe = Wardrobe, figure = Figure} = Player,
	#status_wardrobe{wardrobe_map = WardrobeMap} = Wardrobe,
	case maps:get(Type, WardrobeMap, #wardrobe_appearance{}) of
		#wardrobe_appearance{} = WardrobeAppear ->
			SumAppearanceValue = calc_appearance_value(Type, KeyValues, 0),
			F = fun({{_SubType, TColor}, Num}, Sum) ->
				case Type == ?WARDROBE_TYPE_FOSTER of
					true -> TColor + Sum;
					_ -> 	Sum + Num
				end end,
			Collect = lists:foldl(F, 0, KeyValues),
			CfgCollectSum = cfg_collect_sum(Figure#figure.career, Type),
			NewWardrobeAppear = WardrobeAppear#wardrobe_appearance{
				type = Type
				,wardrobe_value = SumAppearanceValue
				,collect        = Collect
				,collect_sum    = CfgCollectSum + NotOpenNum
			},
			NewWardrobeMap = maps:put(Type, NewWardrobeAppear, WardrobeMap),
			SumValue = calc_sum_wardrobe_value(NewWardrobeMap),
			Stage = calc_wardrobe_stage(SumValue),
			lib_wardrobe:db_save_wardrobe_value(RoleId, Type, Collect, SumAppearanceValue),
			case IdColor of
				[Id, Color] -> ok;
				_ -> Id = 0, Color = 0
			end,
			lib_achievement_api:wardrobe_value_event(Player, SumValue),
			%% 推送给客户端,用于解锁货柜
			lib_wardrobe:send_msg(Sid, 42201, [Stage, SumValue, Type, SumAppearanceValue, Collect, CfgCollectSum+NotOpenNum, Id, Color]),
			NewWardrobe = #status_wardrobe{
				stage = Stage
				,wardrobe_value = SumValue
				,wardrobe_map   = NewWardrobeMap
			};
		_ ->
			NewWardrobe = Wardrobe
	end,
	{ok, Player#player_status{wardrobe = NewWardrobe}}.

%% 计算风采值(单个大类)
calc_appearance_value(_Type, [], SumValue) -> SumValue;
calc_appearance_value(Type, [{{SubType, Color}, Num}|L], CalcValue) ->
	Key = case Type of
		      ?WARDROBE_TYPE_FOSTER -> 2;
		      ?WARDROBE_TYPE_PARTNER -> 2;
		      _ -> 1
	      end,
	%% 进阶类型的需要使用累计
	CalcValue1 = case Type == ?WARDROBE_TYPE_FOSTER  of
					 true ->
						 F = fun(TColor, TCalcValue) ->
							 case data_wardrobe:get_type_value(Type, SubType, {Key, TColor}) of
								 Value when is_integer(Value), Value > 0 ->
									 TCalcValue + Num * Value;
								 _ ->
									 TCalcValue
							 end end,
						 CalcValue + lists:foldl(F, 0, lists:seq(1, Color));
					 _ ->
						 case data_wardrobe:get_type_value(Type, SubType, {Key, Color}) of
							 Value when is_integer(Value), Value > 0 ->
								 CalcValue + Num * Value;
							 _ ->
								 CalcValue
						 end
	             end,
	calc_appearance_value(Type, L, CalcValue1).

%% 计算总风采值(整个衣橱系统)
calc_sum_wardrobe_value(WardrobeMap) ->
	F = fun(_Type, #wardrobe_appearance{wardrobe_value = Value}, CalcValue) ->
		Value + CalcValue
		end,
	SumValue = maps:fold(F, 0, WardrobeMap),
	SumValue.

%% 计算衣橱阶数
calc_wardrobe_stage(SumValue) ->
	case data_wardrobe:get_wardrobe_stage(SumValue) of
		Stage when is_integer(Stage), Stage > 0 -> ok;
		_ -> Stage = 0
	end,
	Stage.

%% 总收集进度
cfg_collect_sum(Career, Type) ->
	CfgCollectSum =
	case Type of
		?WARDROBE_TYPE_FASHION ->
			AllNum = fashion_collect_sum(Career),
			AllNum;
		?WARDROBE_TYPE_DESG ->
			AllNum = dsg_collect_sum(),
			AllNum;
		?WARDROBE_TYPE_PICTURE ->
			AllNum = photo_collect_sum(Career),
			AllNum;
		?WARDROBE_TYPE_FRAME ->
			AllNum = frame_collect_sum(),
			AllNum;
		?WARDROBE_TYPE_BUBBLE ->
			AllNum = bubble_collect_sum(),
			AllNum;
		?WARDROBE_TYPE_FOSTER_SKIN ->
			AllNum = foster_skin_collect_sum(),
			AllNum;
		?WARDROBE_TYPE_CHILD_FASHION ->
			AllNum = child_fashion_collect_sum(),
			AllNum;
		?WARDROBE_TYPE_GALAXY ->
			AllNum = galaxy_skin_collect_sum(),
			AllNum;
		?WARDROBE_TYPE_FOSTER ->
			AllNum = foster_collect_sum(),
			AllNum;
		?WARDROBE_TYPE_PARTNER ->
			AllNum = partner_collect_sum(),
			AllNum
	end,
	CfgCollectSum.

%% 时装需要计算是否到达开放时间
fashion_collect_sum(_Career) ->
	AllFashion = data_fashion:get_all_fashion_with_color(),
	OpenDay = util:get_open_day(),
	NowTime = utime:unixtime(),
	Filter = fun({FashionId, _Color}, Num) ->
		case data_fashion:get_fashion_model(FashionId) of
			#fashion_model_con{open_day = CfgOpenDay, open_time = CfgOpenTime}
				when is_integer(CfgOpenDay), is_integer(CfgOpenTime),
				OpenDay >= CfgOpenDay, NowTime >= CfgOpenTime ->
				Num+1;
			_ -> Num
		end end,
	SumNum = lists:foldl(Filter, 0, AllFashion),
	SumNum.

dsg_collect_sum() ->
	AllNum = erlang:length(data_designation:get_all_dsgt_by_type(0)--
	[433014,433015,433016,433017,433018,433019,433020,433021,433022]),
	AllNum.

photo_collect_sum(Career) ->
	AllPhoto = data_profile_photo:get_profile_photo_ids(),
	OpenDay = util:get_open_day(),
	NowTime = utime:unixtime(),
	F = fun(PhotoId, Num) ->
		case data_profile_photo:get_profile_photo_cfg(PhotoId) of
			#profile_photo{career = [0], open_day = CfgOpenDay, open_time = CfgOpenTime}
				when is_integer(CfgOpenDay), is_integer(CfgOpenTime) ->
				case OpenDay >= CfgOpenDay andalso NowTime >= CfgOpenTime of
					true -> Num + 1;
					_ -> Num
				end;
			#profile_photo{career = [_|_] = CareerList, open_day = CfgOpenDay, open_time = CfgOpenTime}
				when is_integer(CfgOpenDay), is_integer(CfgOpenTime) ->
				case lists:member(Career, CareerList) of
					true ->
						case OpenDay >= CfgOpenDay andalso NowTime >= CfgOpenTime of
							true -> Num + 1;
							_ -> Num
						end;
					_ -> Num
				end;
			_ -> Num
		end end,
	AllNum = lists:foldl(F, 0, AllPhoto),
	AllNum.

frame_collect_sum() ->
	AllFrame = data_dress_up:get_dress_list(2),
	OpenDay = util:get_open_day(),
	NowTime = utime:unixtime(),
	F = fun(DressId, Num) ->
		case data_dress_up:get_dress_cfg(DressId) of
			#base_dress_up{open_day = CfgOpenDay, open_time = CfgOpenTime}
				when is_integer(CfgOpenDay), is_integer(CfgOpenTime) ->
				case OpenDay >= CfgOpenDay andalso NowTime >= CfgOpenTime of
					true -> Num + 1;
					_ -> Num
				end;
			_ -> Num
		end end,
	AllNum = lists:foldl(F, 0, AllFrame),
	AllNum.

bubble_collect_sum() ->
	AllBubble = data_dress_up:get_dress_list(1),
	OpenDay = util:get_open_day(),
	NowTime = utime:unixtime(),
	F = fun(DressId, Num) ->
		case data_dress_up:get_dress_cfg(DressId) of
			#base_dress_up{open_day = CfgOpenDay, open_time = CfgOpenTime}
				when is_integer(CfgOpenDay), is_integer(CfgOpenTime) ->
				case OpenDay >= CfgOpenDay andalso NowTime >= CfgOpenTime of
					true -> Num + 1;
					_ -> Num
				end;
			_ -> Num
		end end,
	AllNum = lists:foldl(F, 0, AllBubble),
	AllNum.

foster_skin_collect_sum() ->
	AllTypes = data_foster:get_skin_types(),
	F = fun(Type, CalcNum) ->
		case data_foster:get_skin_max(Type) of
			Num when is_integer(Num), Num > 0 ->
				Num + CalcNum;
			_ -> CalcNum
		end end,
	AllNum = lists:foldl(F, 0, AllTypes),
	AllNum.

child_fashion_collect_sum() ->
	AllNum = erlang:length(data_child:get_all_fashion()),
	AllNum.

galaxy_skin_collect_sum() ->
	AllNum = erlang:length(data_galaxy:get_all_skins()),
	AllNum.

foster_collect_sum() ->
	F = fun(Type, CalcNum) ->
		Num = case data_foster:get_all_grade(Type) of
				  [_|_] = GradeList -> erlang:length(GradeList);
				  _ -> 0
		      end,
		Num + CalcNum
	end,
	AllNum = lists:foldl(F, 0, lists:seq(1, 4)),
	AllNum.

partner_collect_sum() ->
	%% 只统计皮肤
	AllNum = lists:foldl(fun(Type, TNum) ->
		erlang:length(data_foster:get_all_skin(Type)) + TNum
	                     end, 0, lists:seq(5, 8)),
	AllNum.

%%-define(WARDROBE_TYPE_FASHION, 1).       %% 时装
%%-define(WARDROBE_TYPE_DESG,    2).       %% 称号
%%-define(WARDROBE_TYPE_PICTURE, 3).       %% 头像
%%-define(WARDROBE_TYPE_FRAME,   4).       %% 相框
%%-define(WARDROBE_TYPE_BUBBLE,  5).       %% 气泡
%%-define(WARDROBE_TYPE_FOSTER_SKIN,   6). %% 珍宝
%%-define(WARDROBE_TYPE_CHILD_FASHION, 7). %% 宝宝时装
%%-define(WARDROBE_TYPE_GALAXY,   8).      %% 星辰
%%-define(WARDROBE_TYPE_FOSTER,   9).      %% 进阶
%%-define(WARDROBE_TYPE_PARTNER, 10).      %% 伙伴