%%%-------------------------------------------------------------------
%%% @doc 境界学位
%%% Module: pp_degree.erl
%%% Created : 2019-11-25
%%% @Author: tyl
%%%-------------------------------------------------------------------
-module(pp_degree).

-include("common.hrl").
-include("server.hrl").
-include("degree.hrl").
-include("figure.hrl").
-include("errcode.hrl").

-export([handle/3]).


handle(Cmd, Player, Data) ->
	#player_status{figure = Figure} = Player,
	case Figure#figure.lv >= ?DegreeOpenLv of
		true ->
			case lists:member(Cmd, [16600,16601,16602,16603]) of
			 	true -> skip;
				_ -> do_handle(Cmd, Player, Data)
			end;
			% do_handle(Cmd, Player, Data);
		false -> skip
	end.

%% 查询学位状态信息
do_handle(16600, Player, []) ->
	#player_status{sid = Sid} = Player,
	NewPlayer = lib_degree:get_degree_state(Player),
	#player_status{degree = Degree} = NewPlayer,
	#degree{degree_id = DegreeId, degree_exp = Exp, dun_state = DunState} = Degree,
	{ok, Bin} = pt_166:write(16600, [DegreeId, Exp, DunState]),
	lib_server_send:send_to_sid(Sid, Bin),
	{ok, NewPlayer};

%% 查询日常功课状态信息
do_handle(16601, Player, []) ->
	#player_status{sid = Sid} = Player,
	DailyClass = lib_degree:get_daily_classwork_state(Player),
	#daily_class{
		class_status_map = ClassStatusMap,
		class_progress_map = ClassProgressMap
	} = DailyClass,
	ClassWorksList = pack_classwork_list(ClassStatusMap, ClassProgressMap),
	{ok, Bin} = pt_166:write(16601, [ClassWorksList]),
	lib_server_send:send_to_sid(Sid, Bin),
	NewPlayer = lib_degree:save_dict_degree_classwork(Player, DailyClass),
	{ok, NewPlayer};

%% 学位晋升
do_handle(16602, Player, []) ->
	#player_status{sid = Sid} = Player,
	case lib_degree:upgrade_degree(Player) of
		{true, NewPlayer} ->
			#player_status{degree = NewDegree} = NewPlayer,
			#degree{degree_id = DegreeId, degree_exp = DegreeExp} = NewDegree,
			{ok, Bin} = pt_166:write(16602, [?SUCCESS, DegreeId, DegreeExp]),
			lib_server_send:send_to_sid(Sid, Bin),
			{ok, NewPlayer};
		{false, Res, NewPlayer} ->
			#player_status{degree = NewDegree} = NewPlayer,
			#degree{degree_id = DegreeId, degree_exp = DegreeExp} = NewDegree,
			{ok, Bin} = pt_166:write(16602, [Res, DegreeId, DegreeExp]),
			lib_server_send:send_to_sid(Sid, Bin)
	end;

%% 领取功课奖励
do_handle(16603, Player, [ClassId]) ->
	#player_status{sid = Sid} = Player,
	case lib_degree:receive_classwork_reward(Player, ClassId) of
		{true, NewDegree} ->
			#degree{degree_exp = DegreeExp} = NewDegree,
			{ok, Bin} = pt_166:write(16603, [?SUCCESS, ClassId, DegreeExp]),
			lib_server_send:send_to_sid(Sid, Bin),
			NewPlayer = Player#player_status{degree = NewDegree},
			{ok, NewPlayer};
		{false, Res, _Degree1} ->
			#player_status{degree = Degree} = Player,
			#degree{degree_exp = DegreeExp} = Degree,
			{ok, Bin} = pt_166:write(16603, [Res, ClassId, DegreeExp]),
			lib_server_send:send_to_sid(Sid, Bin)
	end;

do_handle(_Cmd, _PS, _Args) ->
	?ERR("Nomatch Cmd:~p Args:~p", [_Cmd, _Args]),
	{ok, _PS}.

pack_classwork_list(ClassStatusMap, ClassProgressMap) ->
	F = fun(ClassId, ClassState, ClassWorkList) ->
		ClassWorkContent = maps:get(ClassId, ClassProgressMap, []),
		case ClassWorkContent of
			[] ->
				case data_degree:get_degree_timetable_cfg(ClassId) of
					#degree_timetable_cfg{target = Target} ->
						[TargetType, TargetId, _TargetNum] = Target,
						[{ClassId, TargetType, TargetId, 0, 0}|ClassWorkList];
					_ -> ClassWorkList  %% 配置中没有该功课Id的话不发送
				end;
			[_|_] = Content ->
				[Type, Id, Progress] = Content,
				[{ClassId, Type, Id, Progress, ClassState}|ClassWorkList]
		end
	    end,
	ClassWorksList = maps:fold(F, [], ClassStatusMap),
	ClassWorksList.