%% ---------------------------------------------------------------------------
%% @doc pp_mail.erl

%% @author  hjh
%% email  hejiahua@163.com
%% @since  2017-01-13
%% @deprecated 邮件协议处理
%% ---------------------------------------------------------------------------
-module(pp_mail).
-export([handle/3]).

-include("server.hrl").
-include("common.hrl").
-include("errcode.hrl").
-include("mail.hrl").
-include("def_daily.hrl").
-include("def_module.hrl").
-include("figure.hrl").
-include("def_fun.hrl").
-include("def_goods.hrl").

%% 获得邮件列表  
%% 修改邮件类型，分系统邮件和交易邮件 20191106 update
handle(19001, #player_status{id = RoleId, sid = Sid}, [MailType]) ->
    MailList = lib_mail:get_sub_effect_mail_list(RoleId, MailType),
    F = fun(Mail) ->
        #mail{id = MailId, type = Type, state = State, title = Title, time = Time,
              effect_et = EffectEt, module_id = ModuleId, sub_module_id = SubModuleId, important = Important} = Mail,
        IsAttach = lib_mail:is_attach(Mail),
        {MailId, Type, State, Title, IsAttach, Time, EffectEt, ModuleId, SubModuleId, Important}
    end,
    List = [F(T)||T<-MailList],
    {ok, BinData} = pt_190:write(19001, [List]),
    lib_server_send:send_to_sid(Sid, BinData);

%% 获得邮件详细信息
handle(19002, #player_status{id = RoleId, sid = Sid}, [MailId]) ->
    case lib_mail:get_mail(RoleId, MailId) of
        {false, ErrorCode} ->
            lib_mail:send_error_code(Sid, ErrorCode);
        {ok, Mail} ->
            #mail{id = MailId, state = State, sname = SName, title = Title, content = Content,
                  cm_attachment = CmAttachment, time = Time, module_id = ModuleId, sub_module_id = SubModuleId} = Mail,
            F = fun(T, Acc) ->
                case T of
                    {ObjectType, Id, Num} ->
                        Bind = lib_goods_api:get_goods_bind(Id),
                        [{ObjectType, Id, Bind, Num, []}|Acc];
                    {?TYPE_ATTR_GOODS, Id, Num, ExtraAttr} ->
                        FixExtraAttr =
                        case is_list(ExtraAttr) of
                            true  -> ExtraAttr;
                            false -> []
                        end,
                        case lists:keyfind(extra_data, 1, FixExtraAttr) of
                            {extra_data, AttrList} ->
                                ExtraData = [{K, util:term_to_bitstring(V)} || {K, V} <- AttrList],
                                Bind = case lists:keyfind(bind, 1, FixExtraAttr) of
                                    {bind, BindTemp} -> BindTemp;
                                    _ -> ?UNBIND
                                end,
                                [{?TYPE_GOODS, Id, Bind, Num, ExtraData}|Acc];
                            _ ->
                                Bind = case lists:keyfind(bind, 1, FixExtraAttr) of
                                           {bind, BindTemp} -> BindTemp;
                                           _ -> ?UNBIND
                                       end,
                                [{?TYPE_GOODS, Id, Bind, Num, []}|Acc]
                        end;
                    _ -> Acc
                end
            end,
            NewCmAttachment = lists:foldl(F, [], CmAttachment),
            IsReceive = ?IF(State == ?MAIL_STATE_HAS_RECEIVE, 1, 0),
            {ok, BinData} = pt_190:write(19002, [MailId, SName, Title, Content, NewCmAttachment, Time, IsReceive, ModuleId, SubModuleId]),
            lib_server_send:send_to_sid(Sid, BinData)
    end;

%% 删除邮件 
%% 增加一个类型判断区分交易邮件和非交易邮件 20191106 update
handle(19003, Player = #player_status{id = RoleId}, [DelMailIds, Type]) ->
    Mails = lib_mail:get_mails_by_ids_on_dict(RoleId, DelMailIds),
    F = fun(M) -> M#mail.type == Type end,
    DelMailIs = lists:filter(F, Mails),
    DelMailIds1 = [Mail#mail.id || Mail <- DelMailIs],
    NewDelMailIds = lists:usort(DelMailIds1),
    {Code, RealDelIds} = lib_mail:delete_mail_by_mail_ids(Player#player_status.id, NewDelMailIds),
    {ok, BinData} = pt_190:write(19003, [Code, RealDelIds]),
    lib_server_send:send_to_sid(Player#player_status.sid, BinData);

%% 提取附件 
%% 增加一个类型判断区分交易邮件和非交易邮件 20191106 update
handle(19005, Player = #player_status{id = RoleId}, [MailIds, Type]) ->
    Mails = lib_mail:get_mails_by_ids_on_dict(RoleId, MailIds),
    F = fun(M) -> M#mail.type == Type end,
    Mails1 = lists:filter(F, Mails),
    MailIds1 = [Mail#mail.id || Mail <- Mails1],
    NewMailIds = lists:usort(MailIds1),
    lib_mail:extract_attachment_by_mail_ids(Player, NewMailIds);

%% 获得邮件基本信息
handle(19007, #player_status{id = RoleId, sid = Sid}, [MailId]) ->
    case lib_mail:get_mail(RoleId, MailId) of
        {false, ErrorCode} ->
            lib_mail:send_error_code(Sid, ErrorCode);
        {ok, Mail} ->
            #mail{id = MailId, type = Type, state = State, title = Title, time = Time,
                  effect_et = EffectEt, module_id = ModuleId, sub_module_id = SubModuleId, important = Important} = Mail,
            IsAttach = lib_mail:is_attach(Mail),
            {ok, BinData} = pt_190:write(19007, [MailId, Type, State, Title, IsAttach, Time, EffectEt, ModuleId, SubModuleId, Important]),
            lib_server_send:send_to_sid(Sid, BinData)
    end;

%% 邮件通知
handle(19008, Player, []) ->
    lib_mail:send_mail_notice(Player#player_status.id);

%% 处理玩家反馈信息
handle(19010, Player, [Content]) ->
    Unixtime = utime:unixtime(),
    % 200字
    CheckContentLen = util:check_length(Content, 400),
    case CheckContentLen == false of
        true ->
            {ok, BinData} = pt_190:write(19010, [?ERRCODE(err190_feedback_not_valid)]),
            lib_server_send:send_to_sid(Player#player_status.sid, BinData);
        false ->
            TimeS = get(send_to_feedback_time),
            Res = case TimeS =:= undefined orelse (TimeS + 30 < Unixtime) of
                true ->
                    NewContent = lib_mail:replace_illegal_word(Content),
                    #player_status{id = RoleId, figure = #figure{name = Name}, ip = Ip} = Player,
                    Server = atom_to_list(node()),
                    Sql = io_lib:format(?sql_feedback_insert, [RoleId, util:make_sure_binary(Name), util:make_sure_binary(NewContent), Unixtime, Ip, Server]),
                    db:execute(Sql),
                    put(send_to_feedback_time, Unixtime),
                    ?SUCCESS;
                false ->
                    ?ERRCODE(err190_on_feedback_cd)
            end,
            {ok, BinData} = pt_190:write(19010, [Res]),
            lib_server_send:send_to_sid(Player#player_status.sid, BinData)
    end;

handle(_Cmd, _Player, _Data) ->
    {error, atom_to_list(?MODULE) ++ " no match"}.
