%%%-------------------------------------------------------------------
%%% @author liushl
%%% @doc
%%%     模拟聊天
%%% @end
%%%-------------------------------------------------------------------
-module(chat_simulation).
-author("liushl").

-include("server.hrl").
-include("def_event.hrl").
-include("rec_event.hrl").
-include("figure.hrl").
-include("def_module.hrl").
-include("common.hrl").
-include("chat.hrl").

-export([login/1, handle_event/2, is_lv_beyond/1]).

login(Player) ->
    #player_status{accid = AccId, accname = AccName} = Player,
    Data = db:get_row(io_lib:format(<<"select trigger_id,trigger_content from chat_simulation where accid=~p and accname='~s'">>, [AccId, AccName])),
    case Data of
        [TriggerId, TriggerContent] ->
            ChatSimu = #chat_simulation{
                trigger_id = TriggerId,
                trigger_content = util:bitstring_to_term(TriggerContent)
                },
            Player#player_status{chat_simulation = ChatSimu};
        _ ->
            Player#player_status{chat_simulation = #chat_simulation{}}
    end.

is_lv_beyond(Lv) ->
    StopLv = lib_module:get_open_lv(?MOD_CHAT, ?MOD_CHAT_STOP_SIMULATION),
    Lv >= StopLv.

%% 升级触发
handle_event(Player, #event_callback{type_id = ?EVENT_LV_UP}) ->
    case is_lv_beyond(Player#player_status.figure#figure.lv) of
        true  ->
            {ok, Player};
        false ->
            Player1 = trigger(Player),
            {ok, Player1}
    end;

handle_event(Player, #event_callback{}) ->
    {ok, Player}.

trigger(Player) ->
    #player_status{chat_simulation = ChatSimu, figure = #figure{lv = Lv}} = Player,
    #chat_simulation{
        trigger_id = LastTriggerId, trigger_content = TriggerContent, trigger_lv = TriggerLvs
    } = ChatSimu,
    case data_chat_simulation:get_trigger_content(Lv) of
        #base_chat_simulation_trigger{id = Id, lv_max = LvMax, content = AllClassify} ->
            %% 只有 Id 大于 LastTriggerId 认为没有触发过
            case Id > LastTriggerId of
                true ->
                    %% 确定该区间触发等级
                    TriggerLv = case lists:keyfind(Id, 1, TriggerLvs) of
                                    {_, NeedLv} -> NeedLv;
                                    _ -> urand:rand(Lv, LvMax)
                                end,
                    case TriggerLv =:= Lv of
                        true  ->
                            %% 已经触发过的模块不再重复触发
                            case pick_classify(Player, urand:list_shuffle(AllClassify) -- TriggerContent) of
                                Classify when is_integer(Classify) ->
                                    NewTriggerContent = [Classify|TriggerContent],
                                    NewChatSimu = ChatSimu#chat_simulation{
                                        trigger_id = Id,
                                        trigger_content = NewTriggerContent,
                                        trigger_lv = lists:keydelete(Id, 1, TriggerLvs)
                                    },
                                    Sql = io_lib:format(<<"replace into chat_simulation(accId,accname,trigger_id,trigger_content) values(~p,'~s',~p,'~ts')">>,
                                        [Player#player_status.accid, Player#player_status.accname, Id, util:term_to_bitstring(NewTriggerContent)]),
                                    db:execute(Sql),
                                    mod_chat_cache:simu_chat(Player#player_status.id, Classify),
                                    Player#player_status{chat_simulation = NewChatSimu};
                                _ ->
                                    %% 分类不足
                                    Player
                            end;
                        false ->
                            NewChatSimu = ChatSimu#chat_simulation{
                                trigger_lv = lists:keystore(Id, 1, TriggerLvs, {Id, TriggerLv})
                            },
                            Player#player_status{chat_simulation = NewChatSimu}
                    end;
                false ->
                    %% 该等级区间已经触发过了
                    Player
            end;
        _ ->
            Player
    end.

pick_classify(_, []) -> false;
pick_classify(Player, [Classify|T]) ->
    case check(Player, Classify) of
        true  -> Classify;
        false -> pick_classify(Player, T)
    end.

check(Player, Classify) ->
    Condition = data_chat_simulation:get_condition_by_classify(Classify),
    F = fun({K, V}) ->
        case K of
            power ->
                Player#player_status.combat_power < V;
            account_lv ->
                [MaxLv] = db:get_row(<<"select max(player_low.lv) from player_low left join player_login on player_login.accid=~p and player_login.accname='~s' where player_low.id = player_login.id;">>
                    , [Player#player_status.accid, Player#player_status.accname]),
                MaxLv < V
        end
        end,
    lists:all(F, Condition).


