%%---------------------------------------------------------------------------
%% @title 魅力海滩
%% @create yxf
%% @date 20171216
%%---------------------------------------------------------------------------
-module(pp_charm_beach).
-include("server.hrl").
-include("charm_beach.hrl").
-include("figure.hrl").
-include("errcode.hrl").
-include("common.hrl").
-include("def_module.hrl").
-include("def_act_label.hrl").
-include("kv.hrl").
-export([handle/3]).

%% 活动状态
handle(41801, PS, []) ->
    Node = clusters_node:get_node(),
    case lib_charm_beach:get_act_cls_type() of
        ?ACT_TYPE_KF -> clusters_node:apply_cast(mod_charm_beach, get_act_info, [Node, PS#player_status.id]);
        _ -> mod_charm_beach:get_act_info(Node, PS#player_status.id)
    end;

%% 进入场景
handle(41802, PS, []) ->
    #player_status{id = RoleId, pid = RolePid, server_id = ServerId, server_num = ServerNum, scene = Scene, figure = Figure} = PS,
    #figure{lv = RoleLv} = Figure,
    ConfigLv = data_module:get_module_open_lv(?MOD_BEACH),
    if
        RoleLv < ConfigLv ->
            send_error_code(PS#player_status.sid, ?ERRCODE(err418_lv_invalid));
        true ->
            case lib_player_check:check_list(PS, [alive, action_free]) of
                true ->
                    IsHosting = case offline_hosting_api:is_offline_hosting(PS) of
                                    true -> 1;
                                    _ -> 0
                                end,
                    RoleMsg = {RoleId, RolePid, ServerId, ServerNum, Scene, IsHosting},
                    Node = clusters_node:get_node(),
                    case lib_charm_beach:get_act_cls_type() of
                        ?ACT_TYPE_KF -> clusters_node:apply_cast(mod_charm_beach, enter_scene, [Node, RoleMsg, Figure]);
                        _ -> mod_charm_beach:enter_scene(Node, RoleMsg, Figure)
                    end;
                {false, ErrCode} ->
                    send_error_code(PS#player_status.sid, ErrCode)
            end
    end;

%% 退出场景
handle(41803, PS, []) ->
    #player_status{id = RoleId, scene = Scene, scene_pool_id = PoolId, copy_id = CopyId, x = X, y = Y} = PS,
    Node = clusters_node:get_node(),
    case lib_charm_beach:get_act_cls_type() of
        ?ACT_TYPE_KF ->
            clusters_node:apply_cast(mod_charm_beach, quit_scene, [Node, RoleId, Scene, PoolId, CopyId, X, Y]);
        _ ->
            mod_charm_beach:quit_scene(Node, RoleId, Scene, PoolId, CopyId, X, Y)
    end;

%% 个人信息
handle(41804, PS, []) ->
    Node = clusters_node:get_node(),
    AddFreeTimes = lib_pet_api:pet_life_skill_about_beach_vote_add(PS),
    case lib_charm_beach:get_act_cls_type() of
        ?ACT_TYPE_KF ->
            clusters_node:apply_cast(mod_charm_beach, get_self_info, [Node, PS#player_status.id, AddFreeTimes]);
        _ ->
            mod_charm_beach:get_self_info(Node, PS#player_status.id, AddFreeTimes)
    end;

%% 魅力排行榜
handle(41805, PS, [Length]) ->
    Node = clusters_node:get_node(),
    case lib_charm_beach:get_act_cls_type() of
        ?ACT_TYPE_KF ->
            clusters_node:apply_cast(mod_charm_beach, get_rank_info, [Node, PS#player_status.id, Length]);
        _ ->
            mod_charm_beach:get_rank_info(Node, PS#player_status.id, Length)
    end;

%% 增加经验
handle(41806, PS, []) ->
    #player_status{id = RoleId, figure = #figure{vip_type = VipType, vip = VipLv}, scene = SceneId} = PS,
    WorldLv = util:get_world_lv(),
    Node = clusters_node:get_node(),
    case lib_charm_beach:get_act_cls_type() of
        ?ACT_TYPE_KF ->
            clusters_node:apply_cast(mod_charm_beach, trigger_addexp, [Node, RoleId, VipType, VipLv, SceneId, WorldLv]);
        _ ->
            mod_charm_beach:trigger_addexp(Node, RoleId, VipType, VipLv, SceneId, WorldLv)
    end;

%% ----------- 投票相关 -----------------
%% 投票
handle(41808, PS, [BeVoteId]) ->
    #player_status{id = RoleId} = PS,
    %[{_, RelaType, _}] = lib_relationship_api:get_rela_and_intimacy_online(RoleId, [BeVoteId]),
    %case RelaType > 0 of
    %    true ->
    %        IsFriend = 1;
    %    _ ->
    %        IsFriend = 0
    %end,
    IsFriend = 0,
    Node = clusters_node:get_node(),
    %% 宠物生活技能增加5次免费次数
    AddFreeTimes = lib_pet_api:pet_life_skill_about_beach_vote_add(PS),
    case lib_charm_beach:get_act_cls_type() of
        ?ACT_TYPE_KF ->
            %% 加上好友关系
            clusters_node:apply_cast(mod_charm_beach, vote, [Node, RoleId, BeVoteId, IsFriend, AddFreeTimes]);
        _ ->
            mod_charm_beach:vote(Node, RoleId, BeVoteId, IsFriend, AddFreeTimes)
    end;

%% 获取我的被投票记录
handle(41809, PS, []) ->
    #player_status{id = RoleId} = PS,
    Node = clusters_node:get_node(),
    case lib_charm_beach:get_act_cls_type() of
        ?ACT_TYPE_KF ->
            clusters_node:apply_cast(mod_charm_beach, get_vote_record, [Node, RoleId]);
        _ ->
            mod_charm_beach:get_vote_record(Node, RoleId)
    end;

%% 获取场景中的好友列表/公会成员列表(废弃)
handle(41810, PS, [Type]) ->
    #player_status{id = RoleId} = PS,
    Node = clusters_node:get_node(),
    case lib_charm_beach:get_act_cls_type() of
        ?ACT_TYPE_KF ->
            clusters_node:apply_cast(mod_charm_beach, get_member_list, [Node, RoleId, Type]);
        _ ->
            mod_charm_beach:get_member_list(Node, RoleId, Type)
    end;

%% 购买投票次数 %% todo
handle(41811, PS, [BuyCount]) ->
    #player_status{id = RoleId, sid = Sid} = PS,
    Node = clusters_node:get_node(),
    BuyList = data_charm_beach:get_cfg(buy_times_money),
    %% 红钻不够用彩钻石
    SumPriceL = calc_sum_price(PS, BuyCount, BuyList, []),
    %% 扣费
    case SumPriceL of
        [_|_] ->
            %% 宠物生活技能增加5次免费次数
            AddFreeTimes = lib_pet_api:pet_life_skill_about_beach_vote_add(PS),
            NewPS = cost_money(PS, SumPriceL, buy_vote_count, ""),
            %% 增加次数
            case lib_charm_beach:get_act_cls_type() of
                ?ACT_TYPE_KF ->
                    clusters_node:apply_cast(mod_charm_beach, buy_vote_count, [Node, RoleId, BuyCount, AddFreeTimes]);
                _ ->
                    mod_charm_beach:buy_vote_count(Node, RoleId, BuyCount, AddFreeTimes)
            end,
            {ok, NewPS};
        _ -> send_error_code(Sid, ?ERRCODE(bgold_not_enough)), {ok, PS}
    end;

handle(_Code, PS, _Args) ->
    ?ERR("Nomatch Cmd:~p Args:~p", [_Code, _Args]),
    {ok, PS}.

send_error_code(Sid, ErrCode) ->
    {ok, BinData} = pt_418:write(41800, [ErrCode]),
    lib_server_send:send_to_sid(Sid, BinData).

calc_sum_price(_PS, 0, _PriceList, SumPrice) -> SumPrice;
calc_sum_price(PS, BuyCount, PriceList, _TPrice) ->
    #player_status{gold = Gold, bgold = BGold} = PS,
    [{_, TMoney}] = PriceList,
    Money = BuyCount * TMoney,
    case BGold >= Money of
        true -> [{2, Money}];
        _ -> %% 不够扣除彩钻
            NeedGold = Money - BGold,
            case Gold >= NeedGold of
                true ->
                    [{1, NeedGold}, {2, BGold}];
                _ ->
                    0
            end
    end.

cost_money(Ps, PriceL, ConsumeType, About) ->
    lists:foldl(
        fun({MoneyType, Money}, TPs) ->
                    goods_del:cost_money(TPs, Money, MoneyType, ConsumeType, About)
        end, Ps, PriceL).
