lua -- 商店的数据管理类

module (..., package.seeall)
local ShopM = {}
local SystemPrompt = require(__APP_PACKAGE_NAME__ .. ".scenes.common.SystemPrompt")
local Item = require("res.scripts.data.model.Item");

function ShopM:init()
    -- 每个数据类都有一个对应的id
    self.mid = getNextID();
    self.refeshCountDown = 0
    self.CurrentItem = {};
    self.NowItem = nil;
    -- 从配置表中获取配置数据,这里是以json的形式
    self.shopprop = DB.getTable("shopprop")[1]
    self.shopitemprop = DB.getTable("shopitemprop")
    if self.shopprop == nil then            
        printf("shopprop is nil");        
    end
    if self.shopitemprop == nil then
        printf("shopitemprop is nil");                
    end    
    -- 注册网络消息,用于接收服务器返回的数据,并对数据进行处理
    NetController:registerMsg(MsgID.msgid_P_OPSHOP_ACK, self.mid, function ( dwSessionID, wMsgSID, wMsgID, pszMsg )
        print("==============打开背包界面==============")
        -- 将json数据转成表的格式
        self.openNetData = CJson.decode(pszMsg);
        if 0 ~= self.openNetData.ErrCode then
            SystemPrompt.new(Errdef.ERR_ALL[self.openNetData.ErrCode]);
            SceneM.destroyNetLayer();
            return
        end
        -- 处理数据
        self:setData(self.openNetData);
        -- 发送本地的事件消息
        NotificationCenter:postEvent(MsgID.msgid_P_OPSHOP_ACK, {});
    end, MAXPRIORITY);

    NetController:registerMsg(MsgID.msgid_P_BITEM_ACK, self.mid, function ( dwSessionID, wMsgSID, wMsgID, pszMsg )
        print("==============购买道具==============")
        self.openNetData = CJson.decode(pszMsg);
        if 0 ~= self.openNetData.ErrCode then
             UISystemTips:SystemTips(self.openNetData.ErrCode);
            return
        end
        self:setDataAfterBuy(self.openNetData);
        NotificationCenter:postEvent(MsgID.msgid_P_BITEM_ACK, {});
    end, MAXPRIORITY);    

    NetController:registerMsg(MsgID.msgid_P_RITEM_ACK, self.mid, function ( dwSessionID, wMsgSID, wMsgID, pszMsg )
        print("==============刷新道具==============")
        self.openNetData = CJson.decode(pszMsg);
        if 0 ~= self.openNetData.ErrCode then
            SystemPrompt.new(Errdef.ERR_ALL[self.openNetData.ErrCode]);
            SceneM.destroyNetLayer();
            return
        end
        self:setData(self.openNetData);
        NotificationCenter:postEvent(MsgID.msgid_P_RITEM_ACK, {});
    end, MAXPRIORITY);        
    
end

function ShopM:cleanup(  )
    
end

function ShopM:setData( data )
    self.itemList = {};
    self.gridList = {};
    local pos = 1;
    local itemInfo = data.ItemInfo
    for i = 1, #itemInfo do
        print("========ShopM:setData========" .. itemInfo[i].KindID);
        local kindID = itemInfo[i].KindID
        local num = itemInfo[i].Num
        local itemType = math.modf(kindID/10000);
        local itemData={ID=kindID, Idx=0, Level=0, Num=num}
        self.itemList[pos] = Item.new(itemType, pos, itemData)    
        if kindID == 0 then
            self.itemList[pos].sell = -1;
        end    
        pos = pos+1
    end

    printf("ShopM:setData RCD:" .. data.RCD)
    self.refeshCountDown = data.RCD
    PlayerM:getBaseData().coin = data.CurCoin;
    PlayerM:getBaseData().gold = data.CurGold;    
end

function ShopM:setDataAfterBuy( data )
    printf(" before buy num:" .. #self.itemList)
    if self.itemList[data.GridIdx] == nil then
        printf(" item[" .. data.GridIdx .. "] is nil")
    end
    self.itemList[data.GridIdx].sell = -1;
    printf(" after buy num:" .. #self.itemList)
    PlayerM:getBaseData().coin = data.CurCoin;
    PlayerM:getBaseData().gold = data.CurGold;    
end


function ShopM:getItemLst(  )
    return self.itemList
end

function ShopM:getShowGridNum(  )
    return 12 -- todo
end

function ShopM:getRefeshCountDown( )
    return self.refeshCountDown
end

function ShopM:getRefeshGold(  )    
    if self.shopprop == nil then
        return 65535
    end
    return self.shopprop.NoCDGold
end
function ShopM:getRefeshCoin(  )
    printf("getRefeshCoin")
    if self.shopprop == nil then
        printf("getRefeshCoin2")
        return 65535
    end
    printf("getRefeshCoin3")
    return self.shopprop.RefreshCoin
end

function ShopM:getBuyGold( gridIdx )
    printf("getBuyGold:" .. gridIdx)
    if self.shopitemprop == nil then
        printf("getBuyGold2")
        return 65535
    end

    local gridProp = self.shopitemprop[gridIdx]
    if gridProp == nil then
        printf("getBuyGold3")
        return 65535
    end
    printf("======gridProp.BuyGold=======" .. gridProp.BuyGold);
    return gridProp.BuyGold
end

function ShopM:getBuyCoin( gridIdx )
    if self.shopitemprop == nil then
        return 65535
    end

    local gridProp = self.shopitemprop[gridIdx]
    if gridProp == nil then
        return 65535
    end
    return gridProp.BuyCoin
end

-- 这里要有一个return
return ShopM;