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

read(15901, _) ->
    {ok, []};
read(15902, Bin0) ->
    <<ProductId:32, _Bin1/binary>> = Bin0, 
    {ok, [ProductId]};
read(15903, Bin0) ->
    <<ProductType:8, _Bin1/binary>> = Bin0, 
    {ok, [ProductType]};
read(15904, Bin0) ->
    <<ProductId:32, _Bin1/binary>> = Bin0, 
    {ok, [ProductId]};
read(15905, _) ->
    {ok, []};
read(15906, Bin0) ->
    <<Index:8, Bin1/binary>> = Bin0, 
    <<ProductId:32, _Bin2/binary>> = Bin1, 
    {ok, [Index, ProductId]};
read(15951, _) ->
    {ok, []};
read(15952, Bin0) ->
    <<ProductId:32, _Bin1/binary>> = Bin0, 
    {ok, [ProductId]};
read(15953, Bin0) ->
    <<ProductId:32, _Bin1/binary>> = Bin0, 
    {ok, [ProductId]};
read(15955, Bin0) ->
    <<Subtype:16, _Bin1/binary>> = Bin0, 
    {ok, [Subtype]};
read(15956, Bin0) ->
    <<Subtype:16, _Bin1/binary>> = Bin0, 
    {ok, [Subtype]};
read(15957, Bin0) ->
    <<Type:16, Bin1/binary>> = Bin0, 
    <<Subtype:16, Bin2/binary>> = Bin1, 
    <<StartTime:32, _Bin3/binary>> = Bin2, 
    {ok, [Type, Subtype, StartTime]};
read(_Cmd, _R) -> {error, no_match}.

write (15901,[
    ProductList
] = Src_Data) ->
    BinList_ProductList = [
        item_to_bin_0(ProductList_Item) || ProductList_Item <- ProductList
    ], 

    ProductList_Len = length(ProductList), 
    Bin_ProductList = list_to_binary(BinList_ProductList),

    Data = <<
        ProductList_Len:16, Bin_ProductList/binary
    >>,
    {ok, pt:encode(15901, Data, Src_Data)};

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

write (15903,[
    ProductType,
    ProductList,
    TimeList
] = Src_Data) ->
    BinList_ProductList = [
        item_to_bin_1(ProductList_Item) || ProductList_Item <- ProductList
    ], 

    ProductList_Len = length(ProductList), 
    Bin_ProductList = list_to_binary(BinList_ProductList),

    BinList_TimeList = [
        item_to_bin_2(TimeList_Item) || TimeList_Item <- TimeList
    ], 

    TimeList_Len = length(TimeList), 
    Bin_TimeList = list_to_binary(BinList_TimeList),

    Data = <<
        ProductType:8,
        ProductList_Len:16, Bin_ProductList/binary,
        TimeList_Len:16, Bin_TimeList/binary
    >>,
    {ok, pt:encode(15903, Data, Src_Data)};

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

write (15905,[
    ProductList
] = Src_Data) ->
    BinList_ProductList = [
        item_to_bin_3(ProductList_Item) || ProductList_Item <- ProductList
    ], 

    ProductList_Len = length(ProductList), 
    Bin_ProductList = list_to_binary(BinList_ProductList),

    Data = <<
        ProductList_Len:16, Bin_ProductList/binary
    >>,
    {ok, pt:encode(15905, Data, Src_Data)};

write (15906,[
    Index,
    ProductId,
    Errcode
] = Src_Data) ->
    Data = <<
        Index:8,
        ProductId:32,
        Errcode:32
    >>,
    {ok, pt:encode(15906, Data, Src_Data)};

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

write (15951,[
    Product
] = Src_Data) ->
    BinList_Product = [
        item_to_bin_5(Product_Item) || Product_Item <- Product
    ], 

    Product_Len = length(Product), 
    Bin_Product = list_to_binary(BinList_Product),

    Data = <<
        Product_Len:16, Bin_Product/binary
    >>,
    {ok, pt:encode(15951, Data, Src_Data)};

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

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

write (15955,[
    Subtype,
    Num,
    RewardInfos
] = Src_Data) ->
    BinList_RewardInfos = [
        item_to_bin_6(RewardInfos_Item) || RewardInfos_Item <- RewardInfos
    ], 

    RewardInfos_Len = length(RewardInfos), 
    Bin_RewardInfos = list_to_binary(BinList_RewardInfos),

    Data = <<
        Subtype:16,
        Num:32,
        RewardInfos_Len:16, Bin_RewardInfos/binary
    >>,
    {ok, pt:encode(15955, Data, Src_Data)};

write (15956,[
    Subtype,
    RewardList
] = Src_Data) ->
    BinList_RewardList = [
        item_to_bin_7(RewardList_Item) || RewardList_Item <- RewardList
    ], 

    RewardList_Len = length(RewardList), 
    Bin_RewardList = list_to_binary(BinList_RewardList),

    Data = <<
        Subtype:16,
        RewardList_Len:16, Bin_RewardList/binary
    >>,
    {ok, pt:encode(15956, Data, Src_Data)};

write (15957,[
    Type,
    Subtype,
    TotalGold
] = Src_Data) ->
    Data = <<
        Type:16,
        Subtype:16,
        TotalGold:32
    >>,
    {ok, pt:encode(15957, Data, Src_Data)};

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


item_to_bin_0 ({
    ProductType,
    ProductSubtype,
    ProductId,
    State,
    LeftCount,
    EndTime,
    LeftTimes
}) ->
    Data = <<
        ProductType:32,
        ProductSubtype:32,
        ProductId:32,
        State:8,
        LeftCount:16,
        EndTime:32,
        LeftTimes:8
    >>,
    Data.
item_to_bin_1 ({
    ProductId,
    BuyCounts
}) ->
    Data = <<
        ProductId:32,
        BuyCounts:32
    >>,
    Data.
item_to_bin_2 ({
    SubType,
    EndTime
}) ->
    Data = <<
        SubType:32,
        EndTime:32
    >>,
    Data.
item_to_bin_3 ({
    ProductId,
    ChargeValue,
    Open,
    Days,
    AwardList
}) ->
    BinList_AwardList = [
        item_to_bin_4(AwardList_Item) || AwardList_Item <- AwardList
    ], 

    AwardList_Len = length(AwardList), 
    Bin_AwardList = list_to_binary(BinList_AwardList),

    Data = <<
        ProductId:32,
        ChargeValue:32,
        Open:8,
        Days:8,
        AwardList_Len:16, Bin_AwardList/binary
    >>,
    Data.
item_to_bin_4 (
    Index
) ->
    Data = <<
        Index:8
    >>,
    Data.
item_to_bin_5 ({
    ProductId,
    State
}) ->
    Data = <<
        ProductId:32,
        State:8
    >>,
    Data.
item_to_bin_6 ({
    Id,
    State,
    Val,
    Max,
    RewardList,
    Desc
}) ->
    Bin_RewardList = pt:write_object_list(RewardList), 

    Bin_Desc = pt:write_string(Desc), 

    Data = <<
        Id:16,
        State:8,
        Val:32,
        Max:32,
        Bin_RewardList/binary,
        Bin_Desc/binary
    >>,
    Data.
item_to_bin_7 ({
    Id,
    State,
    Val,
    Max,
    RewardList,
    Desc
}) ->
    Bin_RewardList = pt:write_object_list(RewardList), 

    Bin_Desc = pt:write_string(Desc), 

    Data = <<
        Id:16,
        State:8,
        Val:32,
        Max:32,
        Bin_RewardList/binary,
        Bin_Desc/binary
    >>,
    Data.
