%% ---------------------------------------------------------
%% Author:  zmh
%% Created: 20210125
%% Description: 跨服战团
%% --------------------------------------------------------
-module(pp_legion).
-export([handle/3]).
-include("server.hrl").
-include("goods.hrl").
-include("predefine.hrl").
-include("kf_legion.hrl").
-include("errcode.hrl").
-include("figure.hrl").
-include("def_module.hrl").
-include("def_goods.hrl").
-include("czone.hrl").
-include("role.hrl").

%%获取信息 指挥室信息
handle(60300, Status, []) ->
    #player_status{
        id=RoleID
    } = Status,
    Zone = czone_api:env_get_zone_id(),
    Fact = czone_api:env_get_faction(),
    clusters_node:apply_cast(mod_legion_mgr,get_director_roles,[Zone,Fact,node(),RoleID]),
    {ok,Status};

%%任务单
handle(60301,Status,[])->
    #player_status{
        id = RoleID,
        role_legion = RoleLegion
    }=Status,
    NRoleLegion = lib_role_legion:check_legion(RoleID,RoleLegion),
    #role_legion{
        process = Pro,
        arms = Arms,
        dot_list = DotList,
        post = Post,
        rank_score = RankScore
    }=NRoleLegion,
    Tasks = lib_role_legion:get_role_task(),
    TaskL = lib_role_legion:pack_task(Tasks,[]),
    {ok,Bin} = pt_603:write(60301,[Pro,DotList,Arms,Post,RankScore,TaskL]),
    lib_server_send:send_to_uid(RoleID,Bin),
    {ok,Status#player_status{role_legion=NRoleLegion}};


%%领取
handle(60304,Status,[OpTy,Args])->
    #player_status{
        id = RoleID,
        role_legion = RoleLegion
    }=Status,
    NRoleLegion = lib_role_legion:check_legion(RoleID,RoleLegion),
    case lib_role_legion:fetch_awards(Status#player_status{role_legion=NRoleLegion},OpTy,Args) of
        {ok,NStatus}->
            Res = ?SUCCESS;
        {false,Res}->
            NStatus = Status
    end,
    {ok,Bin} = pt_603:write(60304,[Res,OpTy,Args]),
    lib_server_send:send_to_uid(RoleID,Bin),
    {ok,NStatus};

%%军衔进阶
handle(60305,Status,[])->
    #player_status{id=RoleID}=Status,
    case lib_role_legion:upgrade_post(Status) of
        {ok,Post,NStatus}->
            Res = ?SUCCESS;
        {false,Res}->
            Post = 0,
            NStatus = Status
    end,
    {ok,Bin} = pt_603:write(60305,[Res,Post]),
    lib_server_send:send_to_uid(RoleID,Bin),
    {ok,NStatus};

%%主动查询个人形象
handle(60306,Status,[Aim]) ->
    #player_status{id=RoleID}=Status,
    ASerId = mod_player_create:get_serid_by_id(Aim),
    MergeSerIds = config:get_merge_server_ids(),
    case lists:member(ASerId, MergeSerIds) of
        false ->
            Zone = czone_api:env_get_zone_id(),
            Fact = czone_api:env_get_faction(),
            case lib_role:get_role_show(Aim) of
                #ets_role_show{figure=Figure}->
                    clusters_node:apply_cast(mod_legion_mgr, upload_figure, [Zone,Fact,Figure]),
                    Now = utime:unixtime(),
                    {ok,Bin} = pt_603:write(60306,[Figure,Now]),
                    lib_server_send:send_to_uid(RoleID,Bin);
                _->
                    skip
            end;
        true -> %%通知到跨服中心分发
            clusters_node:apply_cast(lib_role_legion, center_upload_figure, [RoleID,ASerId,Aim])
    end,
    {ok, Status};

%%国家军功榜单
handle(60307,Status,[Fact])->
    #player_status{
        id=RoleID,
        role_legion=RoleLegion
    }=Status,
    #role_legion{rank_score=RankScore}=RoleLegion,
    Zone = czone_api:env_get_zone_id(),
    clusters_node:apply_cast(mod_legion_rank_mgr, get_fact_rank, [Zone,Fact,node(),RoleID,RankScore]),
    {ok,Status};

%%膜拜和每日领取状态
handle(60308,Status,[])->
    #player_status{
        id = RoleID,
        role_legion = RoleLegion
    }=Status,
    NRoleLegion = lib_role_legion:check_legion(RoleID,RoleLegion),
    #role_legion{
        click_role = CRid,
        day_award  = FStatus
    }=NRoleLegion,
    Zone = czone_api:env_get_zone_id(),
    Fact = czone_api:env_get_faction(),
    clusters_node:apply_cast(mod_legion_rank_mgr, get_award_data, [Zone,Fact,node(),RoleID,CRid,FStatus]),
    {ok,Status#player_status{role_legion=NRoleLegion}};

%%膜拜
handle(60309,Status,[Aim])->
    #player_status{id=RoleID}=Status,
    case lib_role_legion:click_rank_role(Aim,Status) of
        {ok,NStatus}->
            Res = ?SUCCESS;
        {false,Res}->
            NStatus = Status
    end,
    {ok,Bin} = pt_603:write(60309,[Res,Aim]),
    lib_server_send:send_to_uid(RoleID,Bin),
    {ok,NStatus};

%%每日领取
handle(60310,Status,[])->
    #player_status{id=RoleID}=Status,
    case lib_role_legion:fetch_day_awards(Status) of
        {ok,NStatus}->
            Res = ?SUCCESS;
        {false,Res}->
            NStatus = Status
    end,
    {ok,Bin} = pt_603:write(60310,[Res]),
    lib_server_send:send_to_uid(RoleID,Bin),
    {ok,NStatus};

handle(60311,Status,[])->
    #player_status{id=RoleID}=Status,
    Zone = czone_api:env_get_zone_id(),
    Fact = czone_api:env_get_faction(),
    clusters_node:apply_cast(mod_legion_rank_mgr, get_last_rank, [Zone,Fact,node(),RoleID]),
    {ok,Status};

handle(60312,Status,[OpTy])->
    #player_status{id=RoleID}=Status,
    local_legion_mgr:get_history_director(OpTy,RoleID),
    {ok,Status};

%% 军衔排行
handle(60313, Status, [])->
    #player_status{id = RoleId} = Status,
    Node = clusters_node:get_node(),
    ZoneId = czone_api:env_get_zone_id(),
    clusters_node:apply_cast(mod_military_rank_mgr, get_military_rank_info, [ZoneId, Node, RoleId]),
    {ok,Status};

handle(60314,Status,[])->
    #player_status{sid=Sid}=Status,
    Zone = czone_api:env_get_zone_id(),
    Fact = czone_api:env_get_faction(),
    camp_war_mgr:get_camp_war_ret(Zone,Fact,node(),Sid),
    {ok,Status};

handle(60315,Status,[])->
    #player_status{
        id = RoleID,
        role_legion = RoleLegion
    }=Status,
    #role_legion{
        advance_awards  = DaysList
    }=RoleLegion,
    {ok,Bin} = pt_603:write(60315,[DaysList]),
    lib_server_send:send_to_uid(RoleID,Bin),
    {ok,Status};

handle(60316,Status,[FetchDay])->
    #player_status{id=RoleID}=Status,
    case lib_role_legion:fetch_advance_awards(FetchDay,Status) of
        {ok,NStatus}->
            Res = ?SUCCESS;
        {false,Res}->
            NStatus = Status
    end,
    {ok,Bin} = pt_603:write(60316,[Res,FetchDay]),
    lib_server_send:send_to_uid(RoleID,Bin),
    {ok,NStatus};


handle(_Cmd, _Status, _Data) ->
    {error, "pp_visus no match"}.
