%%%===================================================================
%%% @author zmh
%%% @doc 竞榜 许愿池
%%% @end
%%%===================================================================
-module(lib_role_wish).
-include("common.hrl").
-include("server.hrl").
-include("predefine.hrl").
-include("scene.hrl").
-include("goods.hrl").
-include("chat.hrl").
-include("figure.hrl").
-include("race_rank.hrl").
-include("def_goods.hrl").
-include("def_event.hrl").
-include("rec_event.hrl").
-compile(export_all).

-export([
		handle_event/2,
		change_gold/2
	]).


%充值事件处理
handle_event(Status,#event_callback{type_id = ?EVENT_RECHARGE}) ->
    #player_status{id=RoleID} = Status,
    race_rank_act_local:cast({event_recharge, RoleID}),
    {ok, Status};
%% 0点处理
handle_event(#player_status{online = ?ONLINE_ON} = Status, #event_callback{type_id = ?EVENT_ZERO_CLOCK}) ->
    NStatus = zero_time(Status),
    {ok, NStatus};
handle_event(Status, _R) ->
    {ok, Status}.

%%玩家充值
change_gold(Status,WishList)->
	#player_status{id=RoleID}=Status,
	Total = lib_recharge_api:get_today_pay_gold(RoleID),
	change_gold(Status,Total,WishList).

change_gold(Status,_,[])->
	Status;
change_gold(Status,Total,[H|T])->
	#player_status{
		id = RoleID,
		figure=#figure{name=Name},
		race_rank = RaceRankData} = Status,
    #role_race_rank{act_data = DataList} = RaceRankData,
    NActData = case lists:keyfind(H, #rr_act_data.act_id, DataList) of
        ActData = #rr_act_data{} ->
            race_rank:refresh_act_data(ActData);
        _ ->
            #rr_act_data{act_id = H}
    end,
    #rr_act_data{coin=OGold,stage=OStage,time=Time}=NActData,
	{Gold0,Stage0} = case utime:is_today(Time) of
		true->
			{OGold,OStage};
		_->
			{0,0}
	end,
	Stage1 = data_race_rank:get_stage(Total),
	Stage = max(Stage1,Stage0),
	Gold  = Gold0+max(Stage1-Stage0,0),
	NewActData = NActData#rr_act_data{coin=Gold,stage=Stage,time=utime:unixtime()},
	NStatus = race_rank:save_race_rank_act_data(Status, NewActData),
	race_rank:wish_rank_act_info(NStatus,H),
	lib_log_api:log_wish_gold(RoleID,Name,Total,Gold0,Gold),
	change_gold(NStatus,T).

zero_time(Status)->
	#player_status{race_rank = RaceRankData} = Status,
    #role_race_rank{act_data = DataList} = RaceRankData,
    Now = utime:unixtime(),
    NStatus = reset_wish_race(DataList,Now,Status),
	NStatus.

reset_wish_race([],_,Status)->
	Status;
reset_wish_race([H|T],Now,Status)->
	case data_race_rank:get_race_rank_act(H#rr_act_data.act_id) of
		#race_rank_act{type=?TYPE_WISH}->
			NewActData = H#rr_act_data{coin=0,stage=0,time=Now,rune_draw=0,rune_time=Now},
			NStatus = race_rank:save_race_rank_act_data(Status, NewActData),
			race_rank:wish_rank_act_info(NStatus,H#rr_act_data.act_id),
			reset_wish_race(T,Now,NStatus);
		_->
			reset_wish_race(T,Now,Status)
	end.

%%操作日志
% log_role_wish(RoleID,Name,OpTy,Before,After)->
% 	Sql = io_lib:format(<<"insert into `log_role_wish` set `rid`=~p, `name`='~ts', `opty`=~p, `before`=~p, `after`=~p,`time`=UNIX_TIMESTAMP()">>,
%         [RoleID, Name, OpTy, Before, After]),
%     db:execute(Sql),
%     ok.