%%--------------------------------------------------------------------
%% @create ywf
%% @date 2018-8-28
%% @title 魅力海滩
%%--------------------------------------------------------------------
-module(lib_charm_beach_mod).
-include("charm_beach.hrl").
-include("common.hrl").
-include("server.hrl").
-include("figure.hrl").
-include("goods.hrl").
-include("errcode.hrl").
-include("def_fun.hrl").
-include("def_act_label.hrl").
-include("def_goods.hrl").
-include("def_module.hrl").
-include("role.hrl").
-compile(export_all).


%% 获取我的投票列表(已废弃)
get_vote_info(Node, RoleId, State) ->
    #beach_room_stage{members = Members} = State,
    case maps:get(RoleId, Members, false) of
    	#beach_member{vote_role = VoteRoles, scene = Scene} when Scene =/=0 ->
		    Fun = fun({VoteId, Count}, TList) ->
			    VoteMember = maps:get(VoteId, Members),
			    #beach_member{server_id = ServerId, figure = Figure} = VoteMember,
			    #figure{name = RoleName, career = Career, sex = Sex, turn = Turn,
				    picture = Picture, picture_ver = PictureVer, profile_photo_id = ProfilePhotoId} = Figure,
		    	[{VoteId, Count, ServerId, RoleName, Career, Sex, Turn, Picture, PictureVer, ProfilePhotoId}|TList]
			end,
			VoteList = lists:foldl(Fun, [], VoteRoles),
            mod_charm_beach:res_client(Node, 41809, RoleId, [VoteList]);
    	_ ->
            mod_charm_beach:res_client(Node, 41800, RoleId, [?ERRCODE(err418_not_join_act)])
    end.

%% 获取我的被投票记录
get_vote_record(Node, RoleId, State) ->
	#beach_room_stage{members = Members} = State,
    case maps:get(RoleId, Members, false) of
    	#beach_member{vote_role = VoteRoles, scene = Scene, voted_ids = VotedIds} when Scene =/=0 ->
    		Fun = fun({VoteRoleId, VoteCount, ServerNum, _}, TList) ->
    			case maps:get(VoteRoleId, Members, false) of
    				#beach_member{figure = Figure} ->
					    HasVote = case lists:member(VoteRoleId, VotedIds) of
									  true -> 1;
									  _    -> 0
					              end,
    					[{Figure, VoteCount, ServerNum, HasVote}|TList];
    				_ ->
					    TList
    			end
    		end,
    		VoteRec = lists:foldl(Fun, [], VoteRoles),
    		mod_charm_beach:res_client(Node, 41809, RoleId, [VoteRec]);
    	_ ->
    		mod_charm_beach:res_client(Node, 41800, RoleId, [?ERRCODE(err418_not_join_act)])
    end.

%% 发送经验奖励
add_exp(#player_status{id = RoleId, figure = Figure} = Player, AddExp, Type) ->
	#figure{lv = Lv, vip_type = VipType, vip = VipLv} = Figure,
	%% 投票的不加buff
	case Type of
		?ADD_EXP_TYPE_TRIGGER ->
			{BaseExp, VipRatio} = lib_charm_beach:get_add_exp(?MOD_BEACH, 1, Lv, VipType, VipLv),
			case custom_unite_buff:check_buff(Player,extra_exp) of
				{ok, Extra}->
					skip;
				_ ->
					Extra = 0
			end,
			AddExp1 = round(BaseExp*(1+(VipRatio+Extra)/100)),
			Node = clusters_node:get_node(),
			case lib_charm_beach:get_act_cls_type() of
				?ACT_TYPE_KF ->
					clusters_node:apply_cast(mod_charm_beach, click_update_exp, [Node, RoleId, AddExp1]);
				_ ->
					mod_charm_beach:click_update_exp(Node, RoleId, AddExp1)
			end,
			mod_charm_beach:res_client(Node, 41806, RoleId, [?SUCCESS, AddExp1]);
		_ ->
			%% 投票直接用传入的值
			AddExp1 = AddExp
	end,
	NewPS = lib_player:add_exp(Player, AddExp1, charm_beach),
    % NewPS = lib_goods_api:send_reward(PS, #produce{type = charm_beach, reward = [{?TYPE_EXP, 0, AddExp}]}),
    {ok, NewPS}.

%% 购买投票次数
buy_vote_count(Node, RoleId, BuyCount, AddFreeTimes, State) ->
	#beach_room_stage{members = Members} = State,
	case maps:get(RoleId, Members, false) of
		#beach_member{vote_times = VoteTimes, buy_times = BuyTimes, scene = Scene} = BeachMember when Scene =/= 0 ->
			FreeTimes   = data_charm_beach:get_cfg(free_vote_times),
			LeftTimes = FreeTimes + AddFreeTimes + BuyTimes + BuyCount - VoteTimes,
			NewBuyTimes = BuyTimes + BuyCount,
			NewBeachMember = BeachMember#beach_member{buy_times = NewBuyTimes},
			NewMembers = maps:put(RoleId, NewBeachMember, Members),
			mod_charm_beach:res_client(Node, 41811, RoleId, [NewBuyTimes, LeftTimes]),
			{ok, State#beach_room_stage{members = NewMembers}};
		_ ->
			mod_charm_beach:res_client(Node, 41800, RoleId, [?ERRCODE(err418_not_join_act)]),
			{ok, State}
	end.

%% 获取好友列表(已废弃)
get_friend(#player_status{id = RoleId, sid = Sid} = Player, MemberList) ->
	F = fun({Id, CharmV}, TList) ->
		[{_, RelaType, _}] =  lib_relationship_api:get_rela_and_intimacy_online(RoleId, [Id]),
		case RelaType > 0 of
			true ->
				case lib_role:get_role_show(RoleId) of
					#ets_role_show{figure = #figure{name = Name}} ->
						[{Id, Name, CharmV}|TList];
					_ ->
						[{Id, "", CharmV}|TList]
				end;
			_ ->
				TList
		end
	end,
	PackL = lists:foldl(F, [], MemberList),
	{ok, BinData} = pt_418:write(41810, [1, PackL]),
	lib_server_send:send_to_sid(Sid, BinData),
	{ok, Player}.

