%%%--------------------------------------
%%% @Module  : lib_perfect_lover
%%% @Author  : huyihao
%%% @Created : 2018.03.15
%%% @Description:  完美情人
%%%--------------------------------------
-module(lib_perfect_lover).

-include("server.hrl").
-include("figure.hrl").
-include("errcode.hrl").
-include("common.hrl").
-include("def_module.hrl").
-include("custom_act.hrl").
-include("perfect_lover.hrl").
-include("language.hrl").
-include("predefine.hrl").
-include("goods.hrl").
-include("def_fun.hrl").

-export([
    update_perfect_times_info/4,
    check_wedding_times/2,
    sql_perfect_lover/4,
    open_perfect_lover/2,
    pl_send_mail/3,
    send_reward/4,
    do_send_reward/3,
    update_reward_state/4
    ]).

update_perfect_times_info(RoleId, LoverRoleId, WeddingType, WeddingTimesList) ->
    case lists:keyfind(RoleId, #wedding_times_info.role_id, WeddingTimesList) of
        false ->
            NewLoverInfo = #wedding_times_lover_info{
                role_id = RoleId,
                lover_role_id = LoverRoleId,
                times_list = [{WeddingType, 1}]
            },
            NewWeddingTimesInfo = #wedding_times_info{
                role_id = RoleId,
                lover_list = [NewLoverInfo]
            },
            IfReward = false;
        WeddingTimesInfo ->
            #wedding_times_info{
                lover_list = LoverList
            } = WeddingTimesInfo,
            case lists:keyfind(LoverRoleId, #wedding_times_lover_info.lover_role_id, LoverList) of
                false ->
                    NewLoverInfo = #wedding_times_lover_info{
                        role_id = RoleId,
                        lover_role_id = LoverRoleId,
                        times_list = [{WeddingType, 1}]
                    },
                    IfReward = false;
                LoverInfo ->
                    #wedding_times_lover_info{
                        times_list = TimesList
                    } = LoverInfo,
                    case lists:keyfind(WeddingType, 1, TimesList) of
                        false ->
                            NewTimesList = [{WeddingType, 1}|TimesList],
                            WeddingDIdList = data_wedding:get_wedding_designation_id_list(),
                            WeddingTypeNum = length(WeddingDIdList),
                            %% 和本伴侣举行了本次婚礼刚好与同一人举行完所有类型的婚礼
                            case length(NewTimesList) =:= WeddingTypeNum of
                                true ->
                                    CheckLoverList = lists:delete(LoverInfo, LoverList),
                                    %% 排除本次婚礼的对象与其他人举行的婚礼是否已举行完所有婚礼
                                    case check_wedding_times(CheckLoverList, WeddingTypeNum) of
                                        true ->
                                            IfReward = false;
                                        false ->
                                            IfReward = true
                                    end;
                                false ->
                                    IfReward = false
                            end;
                        {_, WeddingTimes} ->
                            NewTimesList = lists:keyreplace(WeddingType, 1, TimesList, {WeddingType, WeddingTimes+1}),
                            IfReward = false
                    end,
                    NewLoverInfo = LoverInfo#wedding_times_lover_info{
                        times_list = NewTimesList
                    }
            end,
            NewLoverList = lists:keystore(LoverRoleId, #wedding_times_lover_info.lover_role_id, LoverList, NewLoverInfo),
            NewWeddingTimesInfo = WeddingTimesInfo#wedding_times_info{
                lover_list = NewLoverList
            }
    end,
    NewWeddingTimesList = lists:keystore(RoleId, #wedding_times_info.role_id, WeddingTimesList, NewWeddingTimesInfo),
    {IfReward, NewWeddingTimesList}.

%% 判断是否领取了奖励
check_wedding_times([T|G], WeddingTypeNum) ->
    #wedding_times_lover_info{
        times_list = TimesList
    } = T,
    case length(TimesList) >= WeddingTypeNum of
        false ->
            check_wedding_times(G, WeddingTypeNum);
        true ->
            true
    end;
check_wedding_times([], _WeddingTypeNum) ->
    false.

%% 发送礼物
send_reward(RoleId, WeddingType, SubType, State) ->
    RewardStateMap = State#perfect_lover_state.reward_state,
    RewardState = maps:get(RoleId, RewardStateMap),
    NewRewardState = lists:keyreplace(WeddingType, 1, RewardState, {WeddingType, ?ALREADY_RECEIVE}),
    lib_server_send:send_to_uid(RoleId, pt_331, 33117, [SubType, NewRewardState]),
    Sql = io_lib:format(?ReplacePerfectLoverRewardSql, [RoleId, util:term_to_bitstring(NewRewardState)]),
    db:execute(Sql),
    lib_player:apply_cast(RoleId, ?APPLY_CAST_SAVE, lib_perfect_lover, do_send_reward, [WeddingType, SubType]),
    NewRewardStateMap = RewardStateMap#{RoleId => NewRewardState},
    State#perfect_lover_state{reward_state = NewRewardStateMap}.

do_send_reward(PS, WeddingType, SubType) ->
    case lib_custom_act_util:get_act_reward_cfg_info(?CUSTOM_ACT_TYPE_PERFECT_LOVER, SubType, WeddingType) of
        #custom_act_reward_cfg{reward = RewardL} ->
            ActName = lib_custom_act_util:get_act_name(?CUSTOM_ACT_TYPE_PERFECT_LOVER, SubType),
            {ok, NewPS} = lib_goods_api:send_reward_with_mail(PS, #produce{reward = RewardL, type = perfect_lover, title = utext:get(3310010, [ActName]), content = utext:get(3310011, [ActName])}, ?MOD_AC_CUSTOM),
            lib_server_send:send_to_sid(PS#player_status.sid, pt_331, 33116, [?SUCCESS, SubType, WeddingType]);
        _ ->
            NewPS = PS,
            lib_server_send:send_to_sid(PS#player_status.sid, pt_331, 33116, [?MISSING_CONFIG, SubType, WeddingType])
    end,
    {ok, NewPS}.

update_reward_state(RoleId, RewardStateMap, WeddingType, ActSubType) ->
    case maps:get(RoleId, RewardStateMap, false) of
        RewardStateList when is_list(RewardStateList) ->
            case lists:keyfind(WeddingType, 1, RewardStateList) of
                false ->
                    NewRewardStateList = lists:keystore(WeddingType, 1, RewardStateList, {WeddingType, ?CAN_RECEIVE});
                _ ->
                    NewRewardStateList = RewardStateList
            end;
        _ ->
            NewRewardStateList = [{WeddingType, ?CAN_RECEIVE}]
    end,
    lib_server_send:send_to_uid(RoleId, pt_331, 33117, [ActSubType, NewRewardStateList]),
    Sql = io_lib:format(?ReplacePerfectLoverRewardSql, [RoleId, util:term_to_bitstring(NewRewardStateList)]),
    db:execute(Sql),
    RewardStateMap#{RoleId => NewRewardStateList}.

sql_perfect_lover(RoleIdM, RoleIdW, WeddingType, Time) ->
    ReSql = io_lib:format(?InsertPerfectLoverSql, [RoleIdM, RoleIdW, WeddingType, Time]),
    db:execute(ReSql).

open_perfect_lover(Argrs, RewardStateList) ->
    [SubType, RoleId, LoverRoleId, LoverName, IfGetReward, _WeddingTimesSendList, WeddingLogList, SelfLogList, LoverList] = Argrs,
    F = fun({RoleIdM, RoleIdW, NowTime}, SendList1) ->
        PsM = lib_offline_api:get_player_info(RoleIdM, all),
        #player_status{
            figure = FigureM
        } = PsM,
        #figure{
            name = NameM
        } = FigureM,
        PsW = lib_offline_api:get_player_info(RoleIdW, all),
        #player_status{
            figure = FigureW
        } = PsW,
        #figure{
            name = NameW
        } = FigureW,
        [{RoleIdM, util:make_sure_binary(NameM), RoleIdW, util:make_sure_binary(NameW), NowTime}|SendList1]
    end,
    %% 汇总婚礼次数
    WeddingTimes = count_wedding_times(LoverList),
    WeddingLogSendList = lists:foldl(F, [], WeddingLogList),
    SelfLogSendList = lists:foldl(F, [], SelfLogList),
    F2 = fun(WeddingType, Times) ->
        case lists:keyfind(WeddingType, 1, RewardStateList) of
            {WeddingType, RewardState} ->
                {WeddingType, Times, RewardState};
            _ ->
                {WeddingType, Times, ?CANNOT_RECEIVE}
        end
    end,
    WeddingTypeList = [F2(WeddingType, Times) || {WeddingType, Times} <- WeddingTimes],
    WeddingTypes =[],
    lib_server_send:send_to_uid(RoleId, pt_331, 33115, [?SUCCESS, SubType, LoverRoleId, util:make_sure_binary(LoverName), IfGetReward, WeddingTypeList, WeddingLogSendList, SelfLogSendList, WeddingTypes]).

pl_send_mail(RoleId, LoverId, RewardList) ->
    PsL = lib_offline_api:get_player_info(LoverId, all),
    #player_status{
        figure = FigureL
    } = PsL,
    #figure{
        name = LoverName
    } = FigureL,
    Title = ?LAN_MSG(?LAN_TITLE_PERFECT_LOVER),
    Content = utext:get(?LAN_CONTENT_PERFECT_LOVER, [LoverName]),
    lib_mail_api:send_sys_mail([RoleId], ?MOD_AC_CUSTOM, ?CUSTOM_ACT_TYPE_PERFECT_LOVER, Title, Content, RewardList).

count_wedding_times(LoverList) ->
    F = fun(#wedding_times_lover_info{times_list = WeddingTimesSendList}, WeddingTimes) ->
        WeddingTimesSendList ++ WeddingTimes
    end,
    WeddingTimes = lists:foldl(F, [], LoverList),
    util:combine_list(WeddingTimes).
