%% ----------------------------
%% desc     :藏宝图
%% module   :pp_treasure_map.erl
%% author   :hh
%% time     :21/03/10
%% ----------------------------

-module (pp_treasure_map).

-include ("server.hrl").
-include ("treasure_map.hrl").
-include ("errcode.hrl").
-include ("def_module.hrl").
-include ("common.hrl").
-include ("figure.hrl").

-export ([handle/3]).

%% 查询藏宝图
handle(Cmd, Player, Arg) ->
    #player_status{figure = #figure{lv = RoleLv}} = Player,
    OpenLv = lib_module:get_open_lv(?MOD_TREASURE_MAP),
    if RoleLv >= OpenLv ->
            do_handle(Cmd, Player, Arg);
        true ->
            skip
    end.

do_handle(42401, Player, [0]) ->
    #player_status{sid = Sid, role_treasure_map = RoleTreasureMap} = Player,
    Now = utime:unixtime(),
    #role_treasure_map{time = Time, type = OldType} = RoleTreasureMap,
    ExpireTime = Time + ?USE_EXPIRE_TIME,
    if Time =/= 0 andalso Now >= ExpireTime andalso OldType =/= 0 ->
            NewRoleTreasureMap = lib_treasure_map:reset_treasure_map(OldType, RoleTreasureMap),
            NewPlayer = Player#player_status{role_treasure_map = NewRoleTreasureMap};
        true ->
            NewPlayer = Player
    end,
    #player_status{role_treasure_map = #role_treasure_map{target = Target, type = Type, map = DataMap}} = NewPlayer,
    case Target of
        [{_, Scene, X, Y}|_] -> ok;
        _   -> Scene = 0, X = 0, Y = 0
    end,
    case Type of
        ?ADVANCE_TREASURE -> EventId = ?TREASURE_TURNTABLE;
        ?NORMAL_TREASURE -> 
            TypeDataMap = maps:get(?NORMAL_TREASURE, DataMap, #normal_map{}),
            #normal_map{event_id = EventId} = TypeDataMap;
        _ -> EventId = 0
    end,
    lib_server_send:send_to_sid(Sid, pt_424, 42401, [?SUCCESS, Type, 0, Scene, 0, X, Y, EventId]),
    {ok, NewPlayer};
    
%% 使用藏宝图
do_handle(42401, Player, [Type]) ->
    #player_status{id = RoleId, sid = Sid, role_treasure_map = RoleTreasureMap} = Player,
    CheckList = [
        {type_check, Type}                          %% Type参数检查
        ,{is_not_in_treasure_time, RoleTreasureMap, Type} %% 是否处于藏宝阶段
        ,{is_enough_cost, Player, Type}     %% 是否有足够藏宝消耗
        ,{times_limit, Type, RoleId}        %% 是否达到藏宝类型使用上限
    ],
    case lib_treasure_map:check(CheckList) of
        true ->
            case lib_treasure_map:use_treasure_map(Player, Type) of
                {ok, NewPlayer, {Scene, ScenePool, X, Y, EventId}} ->
                    lib_server_send:send_to_sid(Player#player_status.sid, pt_424, 42401, [?SUCCESS, Type, 1, Scene, ScenePool, X, Y, EventId]),
                    {ok, NewPlayer};
                {false, Errcode} ->
                    lib_server_send:send_to_sid(Player#player_status.sid, pt_424, 42401, [Errcode, Type, 1, 0, 0, 0, 0, 0]),
                    ok
            end;
        {false, Errcode} ->
            lib_server_send:send_to_sid(Sid, pt_424, 42401, [Errcode, Type, 1, 0, 0, 0, 0, 0])
    end;

%% 获取藏宝图奖励 开宝箱|轮盘
do_handle(42402, Player, [Type, EventId]) ->
    #player_status{
        id = RoleId, sid = Sid, scene = Scene,
        x = X, y = Y, role_treasure_map = RoleTreasureMap} = Player,
    CheckList = [
        {type_event_check, Type, EventId}                            %% Type EventId 参数检查
        ,{is_in_treasure, RoleTreasureMap, Type}                     %% 是否正在挖宝
        ,{coordinate_check, Scene, X, Y, RoleTreasureMap}            %% 坐标距离判断
        ,{is_enough_cost, Player, Type}                              %% 消耗是否足够
    ],
    case lib_treasure_map:check(CheckList) of
        true ->
            case lib_treasure_map:get_treasure(Player, Type, EventId) of
                %% 某些情况获取到展示的物品列表为空,功能内部已返回错误码,这里忽略
                {ok, NewPlayer, _, []} ->
                    {ok, NewPlayer};
                {ok, Player1, RewardId, ShowRewardIds} ->
                    About    = integer_to_list(Type),
                    CostList = lib_treasure_map:get_treasure_cost(Type),
                    case lib_goods_api:cost_object_list_with_check(Player1, CostList, treasure_map_cost, About) of
                        {true, NewPlayer, _} ->
                            case lib_treasure_map:send_treasure_reward(NewPlayer, Type) of
                                {ok, NewPlayer1} ->
                                    #player_status{role_treasure_map = RoleTreasureMap1} = NewPlayer1,
                                    lib_daily:increment(RoleId, ?MOD_TREASURE_MAP, ?TREASURE_MAP_TYPE_MAX, Type),
                                    if Type == ?NORMAL_TREASURE ->
                                            NewCount = lib_daily:get_count(RoleId, ?MOD_TREASURE_MAP, ?TREASURE_MAP_TYPE_MAX, Type),
                                            {ok, BinData} = pt_424:write(42402, [?SUCCESS, Type, EventId, RewardId, ShowRewardIds]),
                                            {ok, BinData1} = pt_424:write(42403, [NewCount]),
                                            lib_server_send:send_to_sid(Sid, <<BinData/binary, BinData1/binary>>);
                                        true ->
                                            lib_server_send:send_to_sid(Sid, pt_424, 42402, [?SUCCESS, Type, EventId, RewardId, ShowRewardIds])
                                    end,
                                    NewRoleTreasureMap = lib_treasure_map:reset_treasure_map(Type, RoleTreasureMap1),
                                    NewPlayer2 = NewPlayer1#player_status{role_treasure_map = NewRoleTreasureMap},
                                    % lib_server_send:send_to_sid(Sid, pt_424, 42402, [?SUCCESS, Type, EventId, RewardId, ShowRewardIds]),
                                    {ok, NewPlayer2};
                                {false, Errcode} ->
                                    lib_server_send:send_to_sid(Sid, pt_424, 42402, [Errcode, Type, 0, 0, []]),
                                    ok
                            end;
                        {false, Errcode, _} ->
                            lib_server_send:send_to_sid(Sid, pt_424, 42402, [Errcode, Type, 0, 0, []]),
                            ok
                    end;
                {false, Errcode} ->
                    lib_server_send:send_to_sid(Sid, pt_424, 42402, [Errcode, Type, 0, 0, []]),
                    ok
            end;
        {false, Errcode} ->
            lib_server_send:send_to_sid(Sid, pt_424, 42402, [Errcode, Type, 0, 0, []]),
            ok
    end;

%% 藏宝图普通次数
do_handle(42403, Player, []) ->
    #player_status{id = RoleId, sid = Sid} = Player,
    Count = lib_daily:get_count(RoleId, ?MOD_TREASURE_MAP, ?TREASURE_MAP_TYPE_MAX, ?NORMAL_TREASURE),
    lib_server_send:send_to_sid(Sid, pt_424, 42403, [Count]),
    ok;

%% 月度高级挖宝次数奖励
do_handle(42405, Player, []) ->
    mod_treasure_map_advance:monthly_reward_info(Player#player_status.id),
    {ok, Player};

%% 领取月度高级挖宝次数奖励
do_handle(42406, Player, [ReceiveTimes]) ->
    lib_treasure_map_advance:receive_monthly_reward(Player, ReceiveTimes),
    {ok, Player};


%% 获取随机题目
do_handle(42407, Player, []) ->
    #player_status{
        id = RoleId, sid = Sid, scene = Scene,
        x = X, y = Y, role_treasure_map = RoleTreasureMap} = Player,
    Type = ?NORMAL_TREASURE,
    CheckList = [
        {is_in_treasure, RoleTreasureMap, Type}
        ,{coordinate_check, Scene, X, Y, RoleTreasureMap}
        ,{times_limit, Type, RoleId}
        ,{is_enough_cost, Player, Type}
    ],
    case lib_treasure_map:check(CheckList) of
        true ->
            lib_treasure_map:get_quiz(Player);
        {false, Errcode} ->
            lib_server_send:send_to_sid(Sid, pt_424, 42407, [Errcode, 0]),
            ok
    end;

%% 答题
do_handle(42408, Player, [QuizId, AnswerId]) ->
    #player_status{id = RoleId, sid = Sid, role_treasure_map = RoleTreasureMap} = Player,
    Type = ?NORMAL_TREASURE,
    CheckList = [
        {is_in_treasure, RoleTreasureMap, Type}
        ,{times_limit, Type, RoleId}
        ,{is_enough_cost, Player, Type}
        ,{normal_event_check, ?TREASURE_QUIZ, RoleTreasureMap}
        ,{is_quiz_event, QuizId, RoleTreasureMap}
    ],
    case lib_treasure_map:check(CheckList) of
        true ->
            lib_treasure_map:answer_quiz(Player, QuizId, AnswerId);
        {false, Errcode} ->
            lib_server_send:send_to_sid(Sid, pt_424, 42408, [Errcode])
    end;

%% 获取限时商城信息
do_handle(42409, Player, []) ->
    #player_status{id = RoleId, sid = Sid, scene = Scene, x = X, y = Y, role_treasure_map = RoleTreasureMap} = Player,
    Type = ?NORMAL_TREASURE,
    CheckList = [
        {is_in_treasure, RoleTreasureMap, Type}
        ,{times_limit, Type, RoleId}
        ,{is_enough_cost, Player, Type}
        ,{is_shop_event, [false], RoleTreasureMap}
        ,{coordinate_check, Scene, X, Y, RoleTreasureMap}
    ],
    case lib_treasure_map:check(CheckList) of
        true ->
            lib_treasure_map:get_treasure_shop_info(Player);
        {false, Errcode} ->
            lib_server_send:send_to_sid(Sid, pt_424, 42409, [Errcode, 0, 0, 0])
    end;

%% 购买限时商城道具
do_handle(42410, Player, [RewardId, Index, BuyNum]) ->
    #player_status{id = RoleId, sid = Sid, role_treasure_map = RoleTreasureMap} = Player,
    Type = ?NORMAL_TREASURE,
    CheckList = [
        {is_in_treasure, RoleTreasureMap, Type}
        ,{times_limit, Type, RoleId}
        ,{normal_event_check, ?TREASURE_SHOP, RoleTreasureMap}
        ,{is_shop_event, [0, shop_status], RoleTreasureMap}
        ,{is_enough_shop_cost, Player, Type, RewardId, Index, BuyNum}
    ],
    case lib_treasure_map:check(CheckList) of
        true ->
            lib_treasure_map:treasure_shopping(Player, RewardId, Index, BuyNum);
        {false, Errcode} ->
            lib_server_send:send_to_sid(Sid, pt_424, 42410, [Errcode, 0, 0])
    end;

%% 触发怪物事件
do_handle(42411, Player, []) ->
    #player_status{id = RoleId, sid = Sid, scene = Scene,
        x = X, y = Y, role_treasure_map = RoleTreasureMap} = Player,
    Type = ?NORMAL_TREASURE,
    CheckList = [
        {is_in_treasure, RoleTreasureMap, Type}
        ,{coordinate_check, Scene, X, Y, RoleTreasureMap}
        ,{times_limit, Type, RoleId}
        ,{is_mon_event, RoleTreasureMap}
    ],
    case lib_treasure_map:check(CheckList) of
        true ->
            lib_treasure_map:treasure_mon(Player);
        {false, Errcode} ->
            lib_server_send:send_to_sid(Sid, pt_424, 42411, [Errcode])
    end;

%% 限时之门领取免费商品
do_handle(42412, Player, []) ->
    #player_status{id = RoleId, sid = Sid, role_treasure_map = RoleTreasureMap} = Player,
    Type = ?NORMAL_TREASURE,
    CheckList = [
        {is_in_treasure, RoleTreasureMap, Type}
        % ,{coordinate_check, Scene, X, Y, RoleTreasureMap}
        ,{times_limit, Type, RoleId}
        ,{normal_event_check, ?TREASURE_SHOP, RoleTreasureMap}
    ],
    case lib_treasure_map:check(CheckList) of
        true -> 
            case lib_treasure_map:treasure_free_shopping(Player) of
                {ok, NewPlayer} ->
                    lib_server_send:send_to_sid(Sid, pt_424, 42412, [?SUCCESS]),
                    {ok, NewPlayer};
                {false, Errcode} ->
                    lib_server_send:send_to_sid(Sid, pt_424, 42412, [Errcode])
            end;
        {false, Errcode} ->
            lib_server_send:send_to_sid(Sid, pt_424, 42412, [Errcode])
    end;

%% 批量使用藏宝图
do_handle(42413, Player, [UseNum]) ->
    lib_treasure_map:batch_use_normal_treasure_map(UseNum, Player);

%% 收纳后限时未过期道具信息
do_handle(42414, Player, []) ->
    lib_treasure_map:limit_shop_info_after_event(Player);

do_handle(42415, Player, [ExpireTime, RewardId, Index, BuyNum]) ->
    lib_treasure_map:buy_limit_shop(Player, ExpireTime, RewardId, Index, BuyNum);

do_handle(_Cmd, Player, _Recv) ->
    {ok, Player}.