%%%-----------------------------------
%%% @Author		: gaomeng 595088880@qq.com
%%% @Created	: 2017/10/17
%%% @Doc		: APP启动相关
%%%-----------------------------------
-module(config).
-include("common.hrl").
-define(APP, server).

-export([
   config/1
   , server_ids/0
   , listen_port/0
   , get_ticket/0
   , get_enter_ticket/0
   , get_platform/0
   , get_create_max_num/0
   , is_gm_open/0
   , set_gm_open/1
   , get_white_ip/0
   , add_white_ip/1
   , del_white_ip/1
   , open_date/0
   , merge_date/0
   , set_open_date/1
   , set_merge_date/1
   , print_cmd/0
   , set_print_cmd/1
   , version/0
   , is_https/0
]).

%% @doc 是否是HTTPS; return true|false
is_https() ->
   case catch config(is_https) of
      {'EXIT', _} ->
         false;
      Val ->
         Val
   end.

%% @doc 获取版本
version() ->
   case catch config(version) of
      {'EXIT', _} ->
         "未知版本";
      Val ->
         Val
   end.

%% @doc 获取协议打印状态
print_cmd() ->
   config(print_cmd).

%% @doc 设置协议打印状态
set_print_cmd(Flag) ->
   application:set_env(?APP, print_cmd, Flag).

%% @doc 设置开服时间
set_open_date(Date) ->
   application:set_env(?APP, open_date, Date, [{persistent, true}]).

%% @doc 设置合服时间
set_merge_date(Date) ->
   application:set_env(?APP, merge_date, Date, [{persistent, true}]).

%% @spec open_date() -> {Year, Month, Day}
%%@ doc 获取开服日期
open_date() ->
   config(open_date).

%% @spec merge_date() -> {Year, Month, Day}
%%@ doc 获取合服日期
merge_date() ->
   config(merge_date).

%% @doc 添加白名单
add_white_ip(Ip) ->
   List = get_white_ip(),
   List1 = [Ip | List],
   application:set_env(?APP, white_ip, List1).

%% @doc 删除白名单
del_white_ip(Ip) ->
   List = get_white_ip(),
   List1 = lists:delete(Ip, List),
   application:set_env(?APP, white_ip, List1).

%% @doc 获取白名单
get_white_ip() ->
   config(white_ip).

set_gm_open(Flag) ->
   application:set_env(?APP, gm, Flag).

is_gm_open() ->
   config(gm).

get_enter_ticket() ->
   config(enter_ticket).

get_ticket() ->
   config(ticket).

listen_port() ->
   config(listen_port).

server_ids() ->
   config(server_ids).

get_platform() ->
   config(platform).

get_create_max_num() ->
   config(create_max_num).

%% @doc 根据KEY获取对应的值
config(Key) ->
   case application:get_env(?APP, Key) of
      {ok, Val} ->
         Val;
      _ ->
         throw(undefined)
   end.