%% ---------------------------------------------------------------------------
%% @doc 多人副本
%% @author zengzy
%% @since  2019-03-26
%% ---------------------------------------------------------------------------
-module(pp_dun_multi).

-export([handle/3]).

-include("common.hrl").
-include("server.hrl").
-include("dungeon.hrl").
-include("errcode.hrl").
-include("dungeon_multi.hrl").
-include("figure.hrl").

handle(Cmd, Player, Args) ->
    NotCenter = [21519,21539,21540],
    IsNotCenter = lists:member(Cmd, NotCenter),
    case clusters_api:is_connected_center() of
        false when IsNotCenter == false ->
            {ok, Bin} = pt_215:write(21513, [?ERRCODE(err215_not_connect_center)]),
            lib_server_send:send_to_sid(Player#player_status.sid, Bin),
            ok;
        _ -> do_handle(Cmd, Player, Args)
    end.

%% 查询多人副本队伍基本信息
do_handle(21500, Player, []) ->
    lib_dun_multi:send_dun_multi_info(Player),
    {ok, Player};

%% 查询多人副本队伍列表
do_handle(21501, Player, [DunId]) ->
    lib_dun_multi_cluster:send_dun_teams(Player, DunId),
    {ok, Player};

%% 创建多人副本队伍
do_handle(21502, Player, [DunId, MinLv, Power, Password, SingleMode, NeedApprove, MergType]) ->
    {ProData, LastPlayer} =
    case lib_dun_multi_cluster:create_team(Player, DunId, MinLv, Power, Password, {[], 0}, 1, NeedApprove, SingleMode, MergType) of
        {false, Reason} ->
            {[Reason, 0, 0, MinLv, Power, NeedApprove], Player};
        {ok, NewPlayer, TeamId, NewDunId} ->
            {[?SUCCESS, NewDunId, TeamId, MinLv, Power, NeedApprove], NewPlayer}
    end,
    {ok, BinData} = pt_215:write(21502, ProData),
    lib_server_send:send_to_sid(Player#player_status.sid, BinData),
    {ok, LastPlayer};

%% 加入多人副本队伍
do_handle(21503, Role, [TeamId, DunId, Password, IsReady]) ->
    {ProData, NewRole2} =
    case lib_dun_multi_cluster:join_team(
            Role, TeamId, DunId, Password, IsReady) of
        {false, Reason} ->
            {[Reason, TeamId, DunId], Role};
        {ok, NewRole} ->
            {[1, TeamId, DunId], NewRole}
    end,
    {ok, BinData} = pt_215:write(21503, ProData),
    lib_server_send:send_to_sid(Role#player_status.sid, BinData),
    {ok, NewRole2};

%% 查询多人副本队伍信息
do_handle(21504, Role, [TeamId]) ->
    Args = [node(), Role#player_status.id, TeamId],
    lib_dun_multi_cluster:cast_dun_multi_mgr_cluster({pack_team_info, Args}),
    {ok, Role};

%% 踢除多人副本队员
do_handle(21505, Role, [RoleId]) ->
    case lib_dun_multi_cluster:kick_out(Role, {RoleId}) of
        {false, Reason} ->
            {ok, BinData} = pt_215:write(21505, [Reason, RoleId]),
            lib_server_send:send_to_sid(Role#player_status.sid, BinData),
            {ok, Role};
        {ok, NewRole} ->
            {ok, NewRole}
    end;

%% 离开多人副本队伍
do_handle(21506, Role, _) ->
    {ProData, NewRole2} =
    case lib_dun_multi_cluster:leave_team(Role) of
        {false, Reason} ->
            {[Reason], Role};
        {ok, NewRole} ->
		    {ok, BinData1} = pt_215:write(21513, [?ERRCODE(err240_self_out_team)]),
		    lib_server_send:send_to_sid(Role#player_status.sid, BinData1),
            {[1], NewRole}
    end,
    {ok, BinData} = pt_215:write(21506, ProData),
    lib_server_send:send_to_sid(Role#player_status.sid, BinData),
    {ok, NewRole2};

%% 切换准备状态
do_handle(21507, Role, [IsReady]) ->
    case lib_dun_multi_cluster:change_ready_status(Role, IsReady) of
        {false, Reason} ->
            {ok, BinData} = pt_215:write(21507, [Reason, IsReady]),
            lib_server_send:send_to_sid(Role#player_status.sid, BinData),
            {ok, Role};
        {ok, NewRole} ->
            {ok, NewRole}
    end;

%% 队长开始副本
do_handle(21508, Role, _) ->
    case lib_dun_multi_cluster:start_dun_multi(Role) of
        {false, Reason} ->
            {ok, BinData} = pt_215:write(21508, [Reason]),
            lib_server_send:send_to_sid(Role#player_status.sid, BinData),
            {ok, Role};
        {ok} ->
            {ok, Role}
    end;

%% 邀请
do_handle(21510, Role, [TeamId, DunId, RoleList, InviteType]) ->
    case lib_dun_multi:invite_role(Role, TeamId, DunId, RoleList, InviteType) of
        {false, Reason} ->
            {ok, BinData} = pt_215:write(21510, [Reason, TeamId, DunId, RoleList]),
            lib_server_send:send_to_sid(Role#player_status.sid, BinData),
            {ok, Role};
        {ok} ->
            {ok, Role}
    end;

%% 邀请列表
do_handle(21511, Role, []) ->
    lib_dun_multi:show_inviter_list(Role);

%% 匹配加入队伍
% do_handle(21512, Role, [Type, DunId]) ->
%     case lib_dun_multi_cluster:match_join_team(Role, Type, DunId) of
%         {false, Reason} ->
%             {ok, BinData} = pt_215:write(21512, [Reason, Type]),
%             lib_server_send:send_to_sid(Role#player_status.sid, BinData),
%             {ok, Role};
%         {ok, NewRole} ->
%             {ok, NewRole}
%     end;

%% 招募
do_handle(21514, Role, [Type]) ->
	case lib_dun_multi_cluster:recruit_member(Role, Type) of
		{false, Reason} ->
            {ok, BinData} = pt_215:write(21514, [Reason]),
            lib_server_send:send_to_sid(Role#player_status.sid, BinData),
            {ok, Role};
        {ok} ->
        	{ok, Role}
	end;

%% 修改设置
do_handle(21515, Role, [MinLv, Power, Password]) ->
	case lib_dun_multi_cluster:update_set_info(Role, MinLv, Power, Password) of
		{false, Reason} ->
            {ok, BinData} = pt_215:write(21515, [Reason, MinLv, Power, Password]),
            lib_server_send:send_to_sid(Role#player_status.sid, BinData),
            {ok, Role};
        {ok} ->
        	{ok, Role}
	end;

%% 委任队长
do_handle(21516, Role, [ToId]) ->
	case lib_dun_multi_cluster:change_leader(Role, ToId) of
		{false, Reason} ->
            {ok, BinData} = pt_215:write(21516, [Reason]),
            lib_server_send:send_to_sid(Role#player_status.sid, BinData),
            {ok, Role};
        {ok} ->
        	{ok, Role}
	end;

%% 催促队长
do_handle(21517, Role, []) ->
	case lib_dun_multi_cluster:prompt_enter(Role) of
		{false, Reason} ->
            {ok, BinData} = pt_215:write(21517, [Reason]),
            lib_server_send:send_to_sid(Role#player_status.sid, BinData),
            {ok, Role};
        {ok} ->
        	{ok, Role}
	end;

%% 获取组队情况
do_handle(21519, Role, []) ->
	lib_dun_multi:send_team_info(Role);

%% 拒绝邀请
do_handle(21520, Role, [Type, RejectList]) ->
	lib_dun_multi:reject_inviter(Role, Type, RejectList);


%% 讨伐副本信息
do_handle(21522, Role, _) ->
    #player_status{id = RoleId, sid = Sid, figure = #figure{lv = RoleLv}} = Role,
    {Errcode, EnterCount, RestCount, NextRewardPoint} = lib_dun_multi:get_crusade_count_info(RoleId, RoleLv),
    {ok, BinData} = pt_215:write(21522, [Errcode, EnterCount, RestCount, NextRewardPoint]),
    lib_server_send:send_to_sid(Sid, BinData),
    ok;

%% 讨伐副本每日次数奖励领取
% do_handle(21523, Role, [RewardPoint]) ->
%     #player_status{sid = Sid} = Role,
%     case lib_dun_multi:get_crusade_count_reward(Role, RewardPoint) of
%         {true, NewPlayer, NextRewardPoint} ->
%             {ok, BinData} = pt_215:write(21523, [?SUCCESS, NextRewardPoint]),
%             lib_server_send:send_to_sid(Sid, BinData),
%             {ok, NewPlayer};
%         {false, Errcode} ->
%             {ok, BinData} = pt_215:write(21523, [Errcode, 0]),
%             lib_server_send:send_to_sid(Sid, BinData)
%     end;

%% 连续讨伐
do_handle(21524, Role, [DunType, SubType, MatchType]) ->
    case lib_dun_multi_cluster:macth_or_create_team(Role, DunType, SubType, MatchType) of
        {false, Reason} ->
            {ok, BinData} = pt_215:write(21524, [Reason, MatchType]),
            lib_server_send:send_to_sid(Role#player_status.sid, BinData),
            {ok, Role};
        {ok, NewRole} ->
            {ok, NewRole}
    end;

%% 查看可邀请的玩家列表
do_handle(21526, Role, [DunId, InviteType]) ->
    lib_dun_multi_cluster:get_access_invite_role(Role, DunId, InviteType),
    ok;

%% 发起镜像协助
do_handle(21527, Role, [DunId, TeamId]) ->
    case lib_dun_multi_cluster:dun_multi_mirror_help(Role, DunId, TeamId) of
        {false, Reason} ->
            {ok, BinData} = pt_215:write(21527, [Reason, DunId, TeamId]),
            lib_server_send:send_to_sid(Role#player_status.sid, BinData),
            ok;
        {ok, NewRole} ->
            {ok, BinData} = pt_215:write(21527, [?SUCCESS, DunId, TeamId]),
            lib_server_send:send_to_sid(Role#player_status.sid, BinData),
            {ok, NewRole}
    end;

%% 邀请辅助信息
do_handle(21528, Role, [DunType]) ->
    #player_status{id = RoleId, sid = Sid} = Role,
    if DunType == ?DUNGEON_TYPE_GUARD ->
            {AssistCount, RecoverTime} = lib_dun_multi:calc_guard_assist_count(RoleId);
        true ->
            {AssistCount, RecoverTime} = {0, 0}
    end,
    {ok, BinData} = pt_215:write(21528, [DunType, RecoverTime, AssistCount]),
    lib_server_send:send_to_sid(Sid, BinData),
    ok;

%% 队长发起投票
do_handle(21530, Role, [TeamId]) ->
    #player_status{sid = Sid} = Role,
    case lib_dun_multi_cluster:dun_multi_vote(Role, TeamId) of
        {false, Reason} ->
            {ok, BinData} = pt_215:write(21530, [Reason, TeamId]),
            lib_server_send:send_to_sid(Sid, BinData),
            ok;
        ok ->
            ok
            % {ok, BinData} = pt_215:write(21530, [?SUCCESS, TeamId]),
            % lib_server_send:send_to_sid(Sid, BinData)
    end;

%% 队员投票
do_handle(21531, Role, [VoteRet]) ->
    #player_status{sid = Sid} = Role,
    if
        VoteRet =/= 0 andalso VoteRet =/= 1 ->
            lib_server_send:send_to_sid(Sid, pt_215, 21531, [?FAIL, VoteRet]);
        true ->
            lib_dun_multi_cluster:team_mate_vote(Role, VoteRet)
    end;

% %% 更改组队目标
do_handle(21534, Role, [DunId, Approve]) ->
    lib_dun_multi_cluster:change_team_target(Role, DunId, Approve);

%% 队长回应入队申请
do_handle(21537, Role, [ReqList]) ->
    #player_status{sid = Sid} = Role,
    if
        ReqList =/= [] ->
            lib_dun_multi_cluster:reply_to_req_roles(Role, ReqList);
        true ->
            lib_server_send:send_to_sid(Sid, pt_215, 21537, [?FAIL, []])
    end;

do_handle(21538, Role, [Approve]) ->
    case lib_dun_multi_cluster:change_approve_state(Role, Approve) of
        ok ->
            % lib_server_send:send_to_sid(Role#player_status.sid, pt_215, 21538, [?SUCCESS, Approve]),
            ok;
        {false, Errcode} ->
            lib_server_send:send_to_sid(Role#player_status.sid, pt_215, 21538, [Errcode, Approve])
    end;

do_handle(21539, Role, []) ->
    #player_status{sid = Sid} = Role,
    {StartTime, EndTime} = lib_dungeon_guard:get_guard_buff_time(utime:unixtime()),
    lib_server_send:send_to_sid(Sid, pt_215, 21539, [StartTime, EndTime]),
    ok;

%% 修改协助状态
do_handle(21540, Role, [DunType, HelpType]) ->
    #player_status{id = RoleId, sid = Sid, dun_multi = RoleDunMulti} = Role,
    if DunType =/= ?DUNGEON_TYPE_GUARD andalso DunType =/= ?DUNGEON_TYPE_CRUSADE ->
            lib_server_send:send_to_sid(Sid, pt_215, 21540, [?FAIL, DunType, HelpType]),
            ok;
        true ->
            case RoleDunMulti of
                #role_dun_multi{team_id = TeamId, is_leader = IsLeader, dun_id = TeamDunId} -> ok;
                _ -> IsLeader = 0, TeamDunId = 0, TeamId = 0
            end,
            DunType1 = data_dungeon:get_dun_type(TeamDunId),
            if IsLeader == 1 ->
                    lib_server_send:send_to_sid(Sid, pt_215, 21540, [?ERRCODE(err215_leader_limit), DunType, HelpType]),
                    ok;
                TeamDunId =/= 0 andalso DunType1 =/= DunType ->
                    lib_server_send:send_to_sid(Sid, pt_215, 21540, [?ERRCODE(err215_help_dun_error), DunType, HelpType]),
                    ok;
                TeamId == 0 orelse TeamDunId == 0 -> %% 当前还未入队
                    lib_server_send:send_to_sid(Sid, pt_215, 21540, [?ERRCODE(err215_not_in_team), DunType, HelpType]),
                    ok;
                true -> %% 已有队伍
                    MaxTime   = lib_dun_multi:get_max_times(TeamDunId),
                    {_, EnterCount, _} = lib_dun_multi:get_role_times(RoleId, TeamDunId, DunType),
                    if EnterCount >= MaxTime andalso HelpType == ?DUN_TEAM_HELP_NO ->
                            lib_server_send:send_to_sid(Sid, pt_215, 21540, [?ERRCODE(err215_count_max), DunType, HelpType]),
                            ok;
                        true ->                    
                            case lib_dun_multi_cluster:change_help_type(Role, HelpType) of
                                ok ->
                                    lib_server_send:send_to_sid(Sid, pt_215, 21540, [?SUCCESS, DunType, HelpType]),
                                    ok;
                                {false, Errcode} ->
                                    lib_server_send:send_to_sid(Sid, pt_215, 21540, [Errcode, DunType, HelpType]),
                                    ok
                            end
                    end
            end
    end;

do_handle(21541, Role, [DunType, MergeType]) ->
    #player_status{id = RoleId, sid = Sid, dun_multi = RoleDunMulti, figure = #figure{lv = RoleLv}} = Role,
    if 
        DunType =/= ?DUNGEON_TYPE_CRUSADE ->
            lib_server_send:send_to_sid(Sid, pt_215, 21541, [?FAIL, DunType, MergeType]),
            ok;
        true ->
            case RoleDunMulti of
                #role_dun_multi{team_id = TeamId, merge_type = OldMergeType} -> ok;
                _ -> TeamId = 0, OldMergeType = 0
            end,
            OnDungeon = lib_dungeon:is_on_dungeon(Role),
            MergeLvLimit = data_dungeon_special:get(DunType, merges_lv_2),
            if 
                OnDungeon ->
                    lib_server_send:send_to_sid(Sid, pt_215, 21541, [?ERRCODE(err215_is_on_dungeon), DunType, MergeType]),
                    ok;
                RoleLv < MergeLvLimit ->
                    lib_server_send:send_to_sid(Sid, pt_215, 21541, [?ERRCODE(err610_mer_lv_limit), DunType, MergeType]),
                    ok;
                OldMergeType == MergeType ->
                    lib_server_send:send_to_sid(Sid, pt_215, 21541, [?ERRCODE(err215_same_merge_type), DunType, MergeType]),
                    ok;
                true ->
                    [RandDunId|_] = data_dungeon:get_ids_by_type(DunType),
                    MaxTime  = lib_dun_multi:get_max_times(RandDunId),
                    {_, RoleTime, _} = lib_dun_multi:get_role_times(RoleId, RandDunId, DunType),
                    RestTime = MaxTime - RoleTime,
                    if RestTime =< 0 ->
                            lib_server_send:send_to_sid(Sid, pt_215, 21541, [?ERRCODE(err215_merge_count_limit), DunType, MergeType]),
                            ok;
                        true ->
                            if TeamId =/= 0 ->
                                    lib_dun_multi_cluster:change_merge_type(Role, DunType, MergeType, RestTime);
                                true ->
                                    RoleDunMulti1 = RoleDunMulti#role_dun_multi{merge_type = MergeType},
                                    Role1 = Role#player_status{dun_multi = RoleDunMulti1},
                                    lib_server_send:send_to_sid(Sid, pt_215, 21541, [?SUCCESS, DunType, MergeType]),
                                    {ok, Role1}
                            end
                    end
            end
    end;

do_handle(_Cmd, _Player, _Data) ->
    ?PRINT(" Data:~p ~n", [ _Data]),
    {error, "pp_dun_multi no match~n"}.
