%%%--------------------------------------
%%% @Module  : pp_vip
%%% @Author  : yxf
%%% @Created : 2017-11-8
%%% @Description:  vip
%%%--------------------------------------
-module(pp_vip).
-compile(export_all).
-include("server.hrl").
-include("figure.hrl").
-include("errcode.hrl").
-include("relationship.hrl").
-include("vip.hrl").
-include("predefine.hrl").
-include("common.hrl").
-include("shop.hrl").
-include("def_goods.hrl").
-include("goods.hrl").
-include("customer_service.hrl").
-include("def_module.hrl").
-include("def_daily.hrl").
-include("def_vip.hrl").

%% 获取用户vip信息
handle(45001, PS, []) ->
    lib_vip:send_vip_info(PS);

%% 通过元宝卡激活vip
handle(45002, PS, [_CardType]) ->
    {ok, PS};

%% 客服信息
handle(Cmd = 45004, Player, []) ->
    #player_status{sid = Sid, source = BSource} = Player,
    Source = util:make_sure_list(BSource),
    Cfg = case lib_vip:get_service_cfg(Source) of
        TmpCfg when is_record(TmpCfg, base_customer_service) -> TmpCfg;
        _ ->
            case lib_vip:get_key_service_cfg(Source) of
                TmpCfg  when is_record(TmpCfg, base_customer_service) -> TmpCfg;
                _ ->
                    case lib_vip:get_service_cfg("default") of   %% 找不到对应渠道的 为默认配置的信息
                        TmpCfg when is_record(TmpCfg, base_customer_service) -> TmpCfg;
                        _ -> #base_customer_service{}
                    end
            end
    end,
    #base_customer_service{display = Display, display_code = DisplayCode, medium  = Medium, link_info_1 = LinkInfo1,
        link_info_2 = LinkInfo2,  work_time = WorkTime, code_path = CodePath, remark = Remark
    } = Cfg,
    {ok, BinData} = pt_450:write(Cmd, [Display, DisplayCode, Medium, LinkInfo1, LinkInfo2, CodePath, Remark, WorkTime]),
    lib_server_send:send_to_sid(Sid, BinData);

%% 特权礼包信息
handle(45005, Player, _) ->
    #player_status{id = RoleId, sid = Sid, vip_status = VipStatus} = Player,
    #vip_status{lv = Lv} = VipStatus,
    if Lv == 0 ->
            lib_server_send:send_to_sid(Sid, pt_450, 45005, [[]]);
            ok;
        true ->
            MinLv = data_vip:get_min_vip(),
            Vips = lists:seq(MinLv, Lv),
            VipList = [{?MOD_VIP, ?MOD_VIP_PRESENT, VipLv} || VipLv <- Vips],
            VipCountList = lib_counter:get_count(RoleId, VipList),
            VipCountList1 = [begin
                BuySign = if CountX == 0 -> ?VIP_CAN_BUY; true -> ?VIP_BUYED end,
             {VipLvX, BuySign} end || {{_, _, VipLvX}, CountX} <- VipCountList],
            lib_server_send:send_to_sid(Sid, pt_450, 45005, [VipCountList1]),
            ok
    end;

handle(45006, Player, [VipLv]) ->
    #player_status{id = RoleId, sid = Sid, vip_status = VipStatus} = Player,
    case lib_vip_util:check_buy_privilege(RoleId, VipLv, VipStatus) of
        {true, Cost, Rewards} ->
            case lib_goods_api:cost_object_list_with_check(Player, Cost, buy_vip_gift, "") of
                {true, NewPlayer, _} ->
                    NewPlayer1 = lib_goods_api:send_reward(NewPlayer, Rewards, vip_gift_reward),
                    lib_counter:increment(RoleId, ?MOD_VIP, ?MOD_VIP_PRESENT, VipLv),
                    lib_server_send:send_to_sid(Sid, pt_450, 45006, [?SUCCESS, Rewards]),
                    {ok, NewPlayer1};
                {false, Errcode, _} ->
                    lib_server_send:send_to_sid(Sid, pt_450, 45006, [Errcode, []])
            end;
        {false, Errcode} ->
            lib_server_send:send_to_sid(Sid, pt_450, 45006, [Errcode, []])
    end;

handle(45007, Player, []) ->
    #player_status{sid = Sid, id = RoleId, vip_status = VipStatus} = Player,
    #vip_status{lv = VipLv} = VipStatus,
    case lists:seq(1, VipLv) of
        VipL = [_|_] ->
            CountL = [{?MOD_VIP, 2, Vip} || Vip <- VipL],
            CountL1 = lib_counter:get_count(RoleId, CountL),
            VipCountL = [VipX|| {{?MOD_VIP, 2, VipX}, Count} <- CountL1, Count =/= 0],
            lib_server_send:send_to_sid(Sid, pt_450, 45007, [VipCountL]);
        _ ->
            lib_server_send:send_to_sid(Sid, pt_450, 45007, [[]])
    end;

handle(45008, Player, [VipLv]) ->
    #player_status{sid = Sid, id = RoleId, vip_status = VipStatus} = Player,
    #vip_status{lv = RoleVip} = VipStatus,
    if
        VipLv =< RoleVip ->
            lib_counter:increment(RoleId, ?MOD_VIP, 2, VipLv),
            lib_server_send:send_to_sid(Sid, pt_450, 45008, [?SUCCESS]);
        true ->
            lib_server_send:send_to_sid(Sid, pt_450, 45008, [?FAIL])
    end;

handle(45009, Player, [VipLv]) ->
    #player_status{id = RoleId, sid = Sid, figure = #figure{vip = RealLv}} = Player,
    Count = lib_counter:get_count(RoleId, ?MOD_VIP, 3, VipLv),
    if
        Count > 0 ->
            lib_server_send:send_to_sid(Sid, pt_450, 45009, [?ERRCODE(reward_is_got), VipLv, []]);
        VipLv == 0 orelse RealLv < VipLv ->
            lib_server_send:send_to_sid(Sid, pt_450, 45009, [?ERRCODE(err152_vip_lv_error), VipLv, []]);
        true ->
            case data_vip:get_vip(VipLv) of
                #base_vip_cfg{reward = Rewards} ->
                    NewPlayer = lib_goods_api:send_reward(Player, Rewards, vip_lv_rewards),
                    lib_counter:set_count(RoleId, ?MOD_VIP, 3, VipLv, 1),
                    lib_server_send:send_to_sid(Sid, pt_450, 45009, [?SUCCESS, VipLv, Rewards]),
                    {ok, NewPlayer};
                _ ->
                    lib_server_send:send_to_sid(Sid, pt_450, 45009, [?FAIL, VipLv, []])
            end
    end;

%% V3每日引导礼包状态
handle(45010, Player, []) ->
    #player_status{id= RoleId, sid = Sid, figure = #figure{vip = RealLv}} = Player,
    Count = lib_daily:get_count(RoleId, ?MOD_VIP, 1),
    if
        Count > 0 -> %% 已领
            lib_server_send:send_to_sid(Sid, pt_450, 45010, [2]);
        RealLv >= 3 orelse RealLv < 0 -> %% 不可领
            lib_server_send:send_to_sid(Sid, pt_450, 45010, [0]);
        true -> %% 可领
            lib_server_send:send_to_sid(Sid, pt_450, 45010, [1])
    end;

%% V3每日引导礼包领取
handle(45011, Player, []) ->
    #player_status{id = RoleId, sid = Sid, figure = #figure{vip = RealLv}} = Player,
    Count = lib_daily:get_count(RoleId, ?MOD_VIP, 1),
    if
        Count > 0 ->
            lib_server_send:send_to_sid(Sid, pt_450, 45011, [?FAIL]);
        RealLv >= 3 orelse RealLv < 0 ->
            lib_server_send:send_to_sid(Sid, pt_450, 45011, [?FAIL]);
        true ->
            Rewards = data_vip:get_cfg(6),
            NewPlayer = lib_goods_api:send_reward(Player, Rewards, v3_guide_reward),
            lib_daily:increment(RoleId, ?MOD_VIP, 1),
            lib_server_send:send_to_sid(Sid, pt_450, 45011, [?SUCCESS]),
            {ok, NewPlayer}
    end;

handle(_Cmd, _Player, _Data) ->
    {error, atom_to_list(?MODULE) ++ " no match"}.
