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

read(17000, Bin0) ->
    <<PoolType:8, _Bin1/binary>> = Bin0, 
    {ok, [PoolType]};
read(17001, Bin0) ->
    <<PoolType:8, _Bin1/binary>> = Bin0, 
    {ok, [PoolType]};
read(17002, _) ->
    {ok, []};
read(17003, Bin0) ->
    <<PoolType:8, Bin1/binary>> = Bin0, 
    <<DrawTimes:8, Bin2/binary>> = Bin1, 
    <<IsFree:8, _Bin3/binary>> = Bin2, 
    {ok, [PoolType, DrawTimes, IsFree]};
read(17004, _) ->
    {ok, []};
read(17005, _) ->
    {ok, []};
read(17006, Bin0) ->
    <<Pos:16, _Bin1/binary>> = Bin0, 
    {ok, [Pos]};
read(17007, _) ->
    {ok, []};
read(17008, Bin0) ->
    <<GoodsId:64, _Bin1/binary>> = Bin0, 
    {ok, [GoodsId]};
read(17009, _) ->
    {ok, []};
read(17010, _) ->
    {ok, []};
read(17011, Bin0) ->
    <<Option:8, Bin1/binary>> = Bin0, 
    <<PoolType:8, Bin2/binary>> = Bin1, 
    <<Times:32, _Bin3/binary>> = Bin2, 
    {ok, [Option, PoolType, Times]};
read(_Cmd, _R) -> {error, no_match}.

write (17000,[
    PoolType,
    FreeTimes,
    NextFreeDraw,
    SumDrawTimes
] = Src_Data) ->
    Data = <<
        PoolType:8,
        FreeTimes:8,
        NextFreeDraw:32,
        SumDrawTimes:32
    >>,
    {ok, pt:encode(17000, Data, Src_Data)};

write (17001,[
    PoolType,
    ServerLucky,
    DrawLog
] = Src_Data) ->
    BinList_DrawLog = [
        item_to_bin_0(DrawLog_Item) || DrawLog_Item <- DrawLog
    ], 

    DrawLog_Len = length(DrawLog), 
    Bin_DrawLog = list_to_binary(BinList_DrawLog),

    Data = <<
        PoolType:8,
        ServerLucky:16,
        DrawLog_Len:16, Bin_DrawLog/binary
    >>,
    {ok, pt:encode(17001, Data, Src_Data)};

write (17002,[
    PoolType,
    ServerLucky,
    RoleId,
    RoleName,
    RewardIdList,
    RecordTime
] = Src_Data) ->
    Bin_RoleName = pt:write_string(RoleName), 

    BinList_RewardIdList = [
        item_to_bin_1(RewardIdList_Item) || RewardIdList_Item <- RewardIdList
    ], 

    RewardIdList_Len = length(RewardIdList), 
    Bin_RewardIdList = list_to_binary(BinList_RewardIdList),

    Data = <<
        PoolType:8,
        ServerLucky:16,
        RoleId:64,
        Bin_RoleName/binary,
        RewardIdList_Len:16, Bin_RewardIdList/binary,
        RecordTime:32
    >>,
    {ok, pt:encode(17002, Data, Src_Data)};

write (17003,[
    Res,
    PoolType,
    FreeTimes,
    NextFreeDraw,
    RewardList,
    SumDrawTimes
] = Src_Data) ->
    BinList_RewardList = [
        item_to_bin_2(RewardList_Item) || RewardList_Item <- RewardList
    ], 

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

    Data = <<
        Res:32,
        PoolType:8,
        FreeTimes:8,
        NextFreeDraw:32,
        RewardList_Len:16, Bin_RewardList/binary,
        SumDrawTimes:32
    >>,
    {ok, pt:encode(17003, Data, Src_Data)};

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

write (17005,[
    GoodsList
] = Src_Data) ->
    BinList_GoodsList = [
        item_to_bin_3(GoodsList_Item) || GoodsList_Item <- GoodsList
    ], 

    GoodsList_Len = length(GoodsList), 
    Bin_GoodsList = list_to_binary(BinList_GoodsList),

    Data = <<
        GoodsList_Len:16, Bin_GoodsList/binary
    >>,
    {ok, pt:encode(17005, Data, Src_Data)};

write (17006,[
    Res,
    Pos,
    GtypeId,
    Num,
    Score
] = Src_Data) ->
    Data = <<
        Res:32,
        Pos:16,
        GtypeId:32,
        Num:8,
        Score:32
    >>,
    {ok, pt:encode(17006, Data, Src_Data)};

write (17007,[
    GoodsList
] = Src_Data) ->
    BinList_GoodsList = [
        item_to_bin_4(GoodsList_Item) || GoodsList_Item <- GoodsList
    ], 

    GoodsList_Len = length(GoodsList), 
    Bin_GoodsList = list_to_binary(BinList_GoodsList),

    Data = <<
        GoodsList_Len:16, Bin_GoodsList/binary
    >>,
    {ok, pt:encode(17007, Data, Src_Data)};

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

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

write (17010,[
    PoolType,
    ServerLucky
] = Src_Data) ->
    Data = <<
        PoolType:8,
        ServerLucky:16
    >>,
    {ok, pt:encode(17010, Data, Src_Data)};

write (17011,[
    Option,
    Res,
    PoolType,
    RewardStatusList
] = Src_Data) ->
    BinList_RewardStatusList = [
        item_to_bin_5(RewardStatusList_Item) || RewardStatusList_Item <- RewardStatusList
    ], 

    RewardStatusList_Len = length(RewardStatusList), 
    Bin_RewardStatusList = list_to_binary(BinList_RewardStatusList),

    Data = <<
        Option:8,
        Res:32,
        PoolType:8,
        RewardStatusList_Len:16, Bin_RewardStatusList/binary
    >>,
    {ok, pt:encode(17011, Data, Src_Data)};

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


item_to_bin_0 ({
    RoleId,
    RoleName,
    RewardCfgId,
    RecordTime,
    IsStick
}) ->
    Bin_RoleName = pt:write_string(RoleName), 

    Data = <<
        RoleId:64,
        Bin_RoleName/binary,
        RewardCfgId:16,
        RecordTime:32,
        IsStick:8
    >>,
    Data.
item_to_bin_1 ({
    RewardCfgId,
    IsStick
}) ->
    Data = <<
        RewardCfgId:16,
        IsStick:8
    >>,
    Data.
item_to_bin_2 ({
    RewardCfgId,
    GtypeId,
    GoodsNum
}) ->
    Data = <<
        RewardCfgId:16,
        GtypeId:32,
        GoodsNum:32
    >>,
    Data.
item_to_bin_3 ({
    Pos,
    GtypeId,
    Type,
    Num
}) ->
    Data = <<
        Pos:16,
        GtypeId:32,
        Type:8,
        Num:8
    >>,
    Data.
item_to_bin_4 ({
    GoodsId,
    GtypId,
    GoodsNum,
    CreateTime
}) ->
    Data = <<
        GoodsId:64,
        GtypId:32,
        GoodsNum:32,
        CreateTime:32
    >>,
    Data.
item_to_bin_5 ({
    Times,
    Status
}) ->
    Data = <<
        Times:32,
        Status:8
    >>,
    Data.
