-module(pt_190).
-include("common.hrl").
-export([
        read/2,
        write/2
    ]).

read(19001, Bin0) ->
    <<Type:8, _Bin1/binary>> = Bin0, 
    {ok, [Type]};
read(19002, Bin0) ->
    <<MailId:64, _Bin1/binary>> = Bin0, 
    {ok, [MailId]};
read(19003, Bin0) ->
    FunArray0 = fun(<<RestBin0/binary>>) -> 
        <<MailId:64, _Args1/binary>> = RestBin0, 
        {MailId,_Args1}
        end,
    {MailIds, Bin1} = pt:read_array(FunArray0, Bin0),

    <<Type:8, _Bin2/binary>> = Bin1, 
    {ok, [MailIds, Type]};
read(19005, Bin0) ->
    FunArray1 = fun(<<RestBin1/binary>>) -> 
        <<MailId:64, _Args1/binary>> = RestBin1, 
        {MailId,_Args1}
        end,
    {MailIds, Bin1} = pt:read_array(FunArray1, Bin0),

    <<Type:8, _Bin2/binary>> = Bin1, 
    {ok, [MailIds, Type]};
read(19006, Bin0) ->
    {Title, Bin1} = pt:read_string(Bin0), 
    {Content, _Bin2} = pt:read_string(Bin1), 
    {ok, [Title, Content]};
read(19007, Bin0) ->
    <<MailId:64, _Bin1/binary>> = Bin0, 
    {ok, [MailId]};
read(19008, _) ->
    {ok, []};
read(19009, _) ->
    {ok, []};
read(19010, Bin0) ->
    {Content, _Bin1} = pt:read_string(Bin0), 
    {ok, [Content]};
read(_Cmd, _R) -> {error, no_match}.

write (19000,[
    ErrorCode
] = Src_Data) ->
    Data = <<
        ErrorCode:32
    >>,
    {ok, pt:encode(19000, Data, Src_Data)};

write (19001,[
    MailList
] = Src_Data) ->
    BinList_MailList = [
        item_to_bin_0(MailList_Item) || MailList_Item <- MailList
    ], 

    MailList_Len = length(MailList), 
    Bin_MailList = list_to_binary(BinList_MailList),

    Data = <<
        MailList_Len:16, Bin_MailList/binary
    >>,
    {ok, pt:encode(19001, Data, Src_Data)};

write (19002,[
    MailId,
    Sender,
    Title,
    Content,
    Attachment,
    Time,
    IsReceive,
    ModuleId,
    SubModuleId
] = Src_Data) ->
    Bin_Sender = pt:write_string(Sender), 

    Bin_Title = pt:write_string(Title), 

    Bin_Content = pt:write_string(Content), 

    BinList_Attachment = [
        item_to_bin_1(Attachment_Item) || Attachment_Item <- Attachment
    ], 

    Attachment_Len = length(Attachment), 
    Bin_Attachment = list_to_binary(BinList_Attachment),

    Data = <<
        MailId:64,
        Bin_Sender/binary,
        Bin_Title/binary,
        Bin_Content/binary,
        Attachment_Len:16, Bin_Attachment/binary,
        Time:32,
        IsReceive:8,
        ModuleId:16,
        SubModuleId:16
    >>,
    {ok, pt:encode(19002, Data, Src_Data)};

write (19003,[
    ErrorCode,
    MailIds
] = Src_Data) ->
    BinList_MailIds = [
        item_to_bin_3(MailIds_Item) || MailIds_Item <- MailIds
    ], 

    MailIds_Len = length(MailIds), 
    Bin_MailIds = list_to_binary(BinList_MailIds),

    Data = <<
        ErrorCode:32,
        MailIds_Len:16, Bin_MailIds/binary
    >>,
    {ok, pt:encode(19003, Data, Src_Data)};

write (19004,[
    MailList
] = Src_Data) ->
    BinList_MailList = [
        item_to_bin_4(MailList_Item) || MailList_Item <- MailList
    ], 

    MailList_Len = length(MailList), 
    Bin_MailList = list_to_binary(BinList_MailList),

    Data = <<
        MailList_Len:16, Bin_MailList/binary
    >>,
    {ok, pt:encode(19004, Data, Src_Data)};

write (19005,[
    ErrorCode,
    MailIds
] = Src_Data) ->
    BinList_MailIds = [
        item_to_bin_5(MailIds_Item) || MailIds_Item <- MailIds
    ], 

    MailIds_Len = length(MailIds), 
    Bin_MailIds = list_to_binary(BinList_MailIds),

    Data = <<
        ErrorCode:32,
        MailIds_Len:16, Bin_MailIds/binary
    >>,
    {ok, pt:encode(19005, Data, Src_Data)};

write (19006,[
    ErrorCode
] = Src_Data) ->
    Data = <<
        ErrorCode:32
    >>,
    {ok, pt:encode(19006, Data, Src_Data)};

write (19007,[
    MailId,
    Type,
    State,
    Title,
    IsAttach,
    Time,
    EffectEt,
    ModuleId,
    SubModuleId,
    Important
] = Src_Data) ->
    Bin_Title = pt:write_string(Title), 

    Data = <<
        MailId:64,
        Type:8,
        State:8,
        Bin_Title/binary,
        IsAttach:8,
        Time:32,
        EffectEt:32,
        ModuleId:16,
        SubModuleId:16,
        Important:8
    >>,
    {ok, pt:encode(19007, Data, Src_Data)};

write (19008,[
    IsUnreadMail
] = Src_Data) ->
    Data = <<
        IsUnreadMail:8
    >>,
    {ok, pt:encode(19008, Data, Src_Data)};

write (19009,[
    LeftNum
] = Src_Data) ->
    Data = <<
        LeftNum:8
    >>,
    {ok, pt:encode(19009, Data, Src_Data)};

write (19010,[
    ErrorCode
] = Src_Data) ->
    Data = <<
        ErrorCode:32
    >>,
    {ok, pt:encode(19010, Data, Src_Data)};

write(_Cmd, _R) -> ?DEBUG("no_match:~p,~p~n", [_Cmd, _R]), {ok, pt:pack(0, <<>>)}.


item_to_bin_0 ({
    MailId,
    Type,
    State,
    Title,
    IsAttach,
    Time,
    EffectEt,
    ModuleId,
    SubModuleId,
    Important
}) ->
    Bin_Title = pt:write_string(Title), 

    Data = <<
        MailId:64,
        Type:8,
        State:8,
        Bin_Title/binary,
        IsAttach:8,
        Time:32,
        EffectEt:32,
        ModuleId:16,
        SubModuleId:16,
        Important:8
    >>,
    Data.
item_to_bin_1 ({
    ObjectType,
    TypeId,
    Lock,
    Num,
    OtherData
}) ->
    BinList_OtherData = [
        item_to_bin_2(OtherData_Item) || OtherData_Item <- OtherData
    ], 

    OtherData_Len = length(OtherData), 
    Bin_OtherData = list_to_binary(BinList_OtherData),

    Data = <<
        ObjectType:8,
        TypeId:32,
        Lock:8,
        Num:32,
        OtherData_Len:16, Bin_OtherData/binary
    >>,
    Data.
item_to_bin_2 ({
    Type,
    Info
}) ->
    Bin_Info = pt:write_string(Info), 

    Data = <<
        Type:8,
        Bin_Info/binary
    >>,
    Data.
item_to_bin_3 (
    MailId
) ->
    Data = <<
        MailId:64
    >>,
    Data.
item_to_bin_4 ({
    MailId,
    Type,
    State,
    Title,
    IsAttach,
    Time,
    EffectEt,
    Important
}) ->
    Bin_Title = pt:write_string(Title), 

    Data = <<
        MailId:64,
        Type:8,
        State:8,
        Bin_Title/binary,
        IsAttach:8,
        Time:32,
        EffectEt:32,
        Important:8
    >>,
    Data.
item_to_bin_5 (
    MailId
) ->
    Data = <<
        MailId:64
    >>,
    Data.
