%%-----------------------------------------------------------------------------
%% module  :       cross_guild_war_local
%% @author  :      zmh
%% Created :       2020-07-29
%% description:    跨服国战本地管理
%%-----------------------------------------------------------------------------
-module(cross_guild_war_local).

-include("errcode.hrl").
-include("common.hrl").
-include("def_module.hrl").
-include("predefine.hrl").
-include("cross_guild_war.hrl").
-include("guild.hrl").
-include("czone.hrl").

-export ([init/1, handle_call/3, handle_cast/2, handle_info/2, code_change/3, terminate/2]).
-export ([
    start_link/0,
    sync_cur_state/2,
    send_act_info/1,
    get_local_state/0,
    is_cgwar_open/0,
    sync_switch_state/1
]).


%%跨服中心同步状态回来
sync_cur_state(WarCtrl,IsNotic)->
    case czone_api:env_get_zone() of
        #czone_svr_local{stage = Stage} ->
            skip;
        _->
            Stage = 0
    end,
    NeedDay = data_cross_guild_war:get_config(need_open_days),
    case util:get_open_day()>= NeedDay andalso Stage>=2 of
        true->
            gen_server:cast(?MODULE, {sync_cur_state,WarCtrl,IsNotic});
        _->
            skip
    end.

sync_switch_state(Switch)->
    gen_server:cast(?MODULE, {sync_switch_state,Switch}).


send_act_info(Sid)->
    gen_server:cast(?MODULE,{send_act_info,Sid}).

get_local_state()->
    case catch gen_server:call(?MODULE,get_local_state) of
        {ok,Reply}->
            Reply;
        _->
            0
    end.

is_cgwar_open()->
    case catch gen_server:call(?MODULE,is_cgwar_open) of
        {ok,Reply}->
            Reply;
        _->
            false
    end.

%% API
start_link() ->
    gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).

init([]) ->
    {ok, #cgwar_local_mgr{}}.

handle_call(Msg, From, State) ->
    case catch do_handle_call(Msg, From, State) of
        {'EXIT', Error} ->
            ?ERR("handle_call error: ~p~nMsg=~p~n", [Error, Msg]),
            {reply, error, State};
        Return  ->
            Return
    end.

handle_cast(Msg, State) ->
    case catch do_handle_cast(Msg, State) of
        {'EXIT', Error} ->
            ?ERR("handle_cast error: ~p~nMsg=~p~n", [Error, Msg]),
            {noreply, State};
        Return  ->
            Return
    end.

handle_info(Info, State) ->
    case catch do_handle_info(Info, State) of
        {'EXIT', Error} ->
            ?ERR("handle_info error: ~p~nInfo=~p~n", [Error, Info]),
            {noreply, State};
        Return  ->
            Return
    end.

code_change(_OldVsn, State, _Extra) ->
    {ok, State}.

terminate(_Reason, _State) ->
    ?ERR("~p is terminate:~p", [?MODULE, _Reason]),
    ok.

do_handle_call(get_local_state,_From,State)->
    #cgwar_local_mgr{
        state = CurS
    }=State,
    {reply,{ok,CurS},State};

do_handle_call(is_cgwar_open,_From,State)->
    #cgwar_local_mgr{
        state = CurS
    }=State,
    IsOpen = lists:member(CurS,[?CGW_ST_SIGN,?CGW_ST_SUPPORT,?CGW_ST_PK]),
    {reply,{ok,IsOpen},State};

do_handle_call(_Msg, _From, State) ->
    {reply, ok, State}.

%%连上跨服中心 如果是报名期 已报名数据要同步到跨服
%%顺便把可参与社团上报
do_handle_cast({sync_cur_state, WarCtrl,false},State)->
    #cgwar_time_mgr{
        state=CurState,
        etime=Etime
    } = WarCtrl,
    NState = State#cgwar_local_mgr{
        state = CurState,
        etime = Etime
    },
    {noreply,NState};
%%活动进度管理进度变化
do_handle_cast({sync_cur_state, WarCtrl,true},State)->
    #cgwar_time_mgr{
        state=CurState,
        etime=Etime
    } = WarCtrl,
    NState = State#cgwar_local_mgr{
        state = CurState,
        etime = Etime
    },
    case CurState of
        ?CGW_ST_SUPPORT->
            lib_chat:send_TV({all}, ?MOD_CGWAR, 3, []);
        ?CGW_ST_PK->
            lib_chat:send_TV({all}, ?MOD_CGWAR, 4, []);
        ?CGW_ST_OVER->
            offline_hosting_cgwar:notice_logout();
        ?CGW_ST_SIGN-> %% 报名阶段取消霸主身份
            cross_guild_war_api:cancel_overlord();
        _->
            skip
    end,
    brocast_all_state(NState),
    {noreply,NState};

do_handle_cast({sync_switch_state,Switch},State)->
    NState = State#cgwar_local_mgr{switch=Switch},
    brocast_all_state(NState),
    {noreply,NState};

do_handle_cast({send_act_info, Sid},State)->
    #cgwar_local_mgr{
        switch = Switch,
        state = CurS,
        etime = ETime
    }=State,
    {ok,Bin} = pt_601:write(60100,[Switch,CurS,ETime]),
    lib_server_send:send_to_sid(Sid,Bin),
    {noreply,State};

do_handle_cast(_Msg, State) ->
    {noreply, State}.

do_handle_info(_Msg, State) ->
    {noreply, State}.


brocast_all_state(State)->
    #cgwar_local_mgr{
        switch = Switch,
        state = S,
        etime = ETime
    }=State,
    OpenLv = lib_module:get_open_lv(?MOD_CGWAR),
    {ok,Bin} = pt_601:write(60100,[Switch,S,ETime]),
    lib_server_send:send_to_all(all_lv, {OpenLv, 60100}, Bin),
    ok.