
--[[
    absAct[od] = {
        coustom[id] = {
            selectItem[1] = 
            selectItem[2] = 
            selectItem[3] = 
            state = 
            cnt = 
        }
    }

]]

local AbsActLogic = require("absAct.AbsActLogic")
local AbsActExcel = require("excel.absAct")
local BuyExcel = require("excel.buy")
local Util = require("common.Util")
local Msg = require("core.Msg")
local Grid = require("bag.Grid")
local BagLogic = require("bag.BagLogic")
local BuyLogic = require("topup.BuyLogic")
local ObjHuman = require("core.ObjHuman")
local Lang  = require("common.Lang")
local Broadcast = require("broadcast.Broadcast")
local Log = require("common.Log")


local SPECIALCISTOMACTID = 31004        -- 活动ID
local SPECIALCISTOMNOBUYID = 20001          -- 不使用的投放ID

function isOpen(human, YYInfo, funcConfig)
    local state, endTime, startTime = AbsActLogic.isStarted(human, funcConfig.funcID)
    if not state then return end
    local absAct = human.db.absAct[funcConfig.funcID]
    if absAct and absAct.dayCnt and absAct.dayCnt <= 0 and #absAct.item <= 0 then
        return false
    end
    return true, endTime, startTime
end

function isActive(human, YYInfo, funcConfig)
    return not isOpen(human, YYInfo, funcConfig)
end


--发送定制礼包数据
-- id 活动id
function getAndSendMsg(human,id,actID)
     -- 活动未开，返回
    local state, endTime, starTime = AbsActLogic.isStarted(human, id)
    if not state then return end
    local absConfig = AbsActExcel.absActivity[id]
    if not absConfig then return end

    local absAct = human.db.absAct[id]
    if not absAct then
        return
    end
      -- 初始化custom数据
    absAct.custom = absAct.custom or {}
    -- 构造数据
    local msgRet = Msg.gc.GC_ABS_ND_CUSTOM_QUERY
    local config = AbsActExcel.custom
    local len = 0
    for k,v in pairs(config) do
        if len >= 3 then 
            msgRet.giftList[0] =len
            msgRet.isEnd = 0
            Msg.send(msgRet,human.fd)
            len = 0
            msgRet = Msg.gc.GC_ABS_ND_CUSTOM_QUERY
        end
        if v.actId == absConfig.actId then
            len = len + 1
            local net = msgRet.giftList[len]
            net.id = k
            net.limitCnt = v.limitCnt
            net.price = v.price
            net.name = v.name
            net.nowCnt = absAct.custom[k] and absAct.custom[k].cnt or 0
            net.state = absAct.custom[k] and absAct.custom[k].state or 0
            net.buyItem[0] = 1
            if v.buyID ~= 0 then
                BuyLogic.fontBuyItem(human, net.buyItem[1], v.buyID)
            else
                net.buyItem[0] = 0
            end
            local warehouseLen = #v.fixed
            for j = 1,warehouseLen do
                local itemID = v.fixed[j][1]
                local itemCnt = v.fixed[j][2]
                Grid.makeItem(net.fixed[j],itemID,itemCnt)
            end
            net.fixed[0] = warehouseLen
            if net.nowCnt >= net.limitCnt then
                net.first[0] = 0
                net.second[0] = 0
                net.third[0] = 0
            else
                warehouseLen = #v.first
                for j = 1,warehouseLen do
                    local itemID = v.first[j][1]
                    local itemCnt = v.first[j][2]
                    Grid.makeItem(net.first[j],itemID,itemCnt)
                end
                net.first[0] = warehouseLen
                warehouseLen = #v.second
                for j = 1,warehouseLen do
                    local itemID = v.second[j][1]
                    local itemCnt = v.second[j][2]
                    Grid.makeItem(net.second[j],itemID,itemCnt)
                end
                net.second[0] = warehouseLen
                warehouseLen = #v.third
                for j = 1,warehouseLen do
                    local itemID = v.third[j][1]
                    local itemCnt = v.third[j][2]
                    Grid.makeItem(net.third[j],itemID,itemCnt)
                end
                net.third[0] = warehouseLen
            end
           -- 状态为0，表示未选择物品，发送物品库
            if net.state == 0 then
                net.one[0] = 0
                net.two[0] = 0
                net.three[0] = 0
            else
               -- 状态不为0，表示已卖完或已选择物品，展示已选择物品
                Grid.makeItem(net.one[1],absAct.custom[k].selectItem[1][1],absAct.custom[k].selectItem[1][2])
                Grid.makeItem(net.two[1],absAct.custom[k].selectItem[2][1],absAct.custom[k].selectItem[2][2])
                net.one[0] = 1
                net.two[0] = 1
                net.three[0] = 0
                if absAct.custom[k].selectItem[3] then
                    Grid.makeItem(net.three[1],absAct.custom[k].selectItem[3][1],absAct.custom[k].selectItem[3][2])
                    net.three[0] = 1
                end
            end
        end
    end
    msgRet.giftList[0] = len
    msgRet.isEnd = 1
    Msg.send(msgRet,human.fd)
    
end

-- 选择物品
function selectItem(human,id,giftId,first,second,third)
    local msgRet = Msg.gc.GC_ABS_ND_CUSTOM_SELECT
    msgRet.ret = 1
    -- 活动未开，无法选择物品
    local state, endTime, starTime = AbsActLogic.isStarted(human, id)
    if not state then 
        msgRet.ret = 0
        Msg.send(msgRet,human.fd)
        return 
    end

    local absConfig = AbsActExcel.absActivity[id]
    if not absConfig then 
        msgRet.ret = 0
        Msg.send(msgRet,human.fd)
        return 
    end

    local absAct = human.db.absAct[id]
    if not absAct then
        msgRet.ret = 0
        Msg.send(msgRet,human.fd)
        return
    end
    
     -- 礼包已卖完无法选择物品
    if absAct.custom and absAct.custom[giftId] and absAct.custom[giftId].state == 2 then
        msgRet.ret = 0
        Msg.send(msgRet,human.fd)
        return
    end

    -- 校验物品是否存在
    absAct.custom = absAct.custom or {}
    absAct.custom[giftId] = absAct.custom[giftId] or {}
    absAct.custom[giftId].cnt = absAct.custom[giftId].cnt or 0
    absAct.custom[giftId].state = absAct.custom[giftId].state or 0
    absAct.custom[giftId].selectItem = absAct.custom[giftId].selectItem or {}
    
   -- 将已选择物品存入数据库
    local config = AbsActExcel.custom[giftId]
    if config == nil 
       or config.first[first] == nil 
       or config.second[second] == nil 
       or (config.third[third] == nil and third ~= 0) then
        msgRet.ret = 0
        Msg.send(msgRet,human.fd)
        return
    end

    if (first == 0 and #config.first ~= 0) or
       (second == 0 and #config.second ~= 0) or
       (third == 0 and #config.third ~= 0) then
        return
    end
    
    -- 并非所有礼包都能选择3个物品
    absAct.custom[giftId].selectItem[1] = {}
    absAct.custom[giftId].selectItem[1][1] = config.first[first][1]
    absAct.custom[giftId].selectItem[1][2] = config.first[first][2]
    absAct.custom[giftId].selectItem[2] = {}
    absAct.custom[giftId].selectItem[2][1] = config.second[second][1]
    absAct.custom[giftId].selectItem[2][2] = config.second[second][2]
    
    -- 并非所有礼包都能选择3个物品
    if third ~= 0 then
        absAct.custom[giftId].selectItem[3] = {}
        absAct.custom[giftId].selectItem[3][1] = config.third[third][1]
        absAct.custom[giftId].selectItem[3][2] = config.third[third][2]
    end
    Msg.send(msgRet,human.fd)
    
    absAct.custom[giftId].state = 1
end

-- 购买礼包
function custom(human,funcID, conf, isFirst, cnt, buyNum)
    customBuy(human,conf.args[1],funcID, true, buyNum)
end

function customBuy(human,args,id, bFromPay, buyNum)
     -- 活动未开，返回
    local state, endTime, starTime = AbsActLogic.isStarted(human, id)
    if not state then return end
    local absConfig = AbsActExcel.absActivity[id]
    if not absConfig then return end

    local absAct = human.db.absAct[id]
    if not absAct then
        return
    end

    buyNum = buyNum or 1

    -- 检查来源
    local tConfig = AbsActExcel.custom[args]
    if not tConfig then
        return
    end

    -- 付费奖励
    if tConfig.buyID ~= 0 then
        if not bFromPay or true ~= bFromPay then
            local szText = "[SpecialCustonLogic_customBuy] 付费奖励, 协议请求领取 来源不正确 name = "..human.db.name.." _id = "..human.db._id.." 礼包ID = "..args
            Log.write(Log.LOGID_OSS_BT, szText)
            return
        end
    end


    -- 购买状态不为可购买，返回
    if not absAct.custom or not absAct.custom[args] or absAct.custom[args].state ~= 1 then
        return
    end

    -- 未选择物品，返回
    if #absAct.custom[args].selectItem <= 0 then
        return
    end

    -- 已达到限购次数，返回
    local config = AbsActExcel.custom[args]
    -- if absAct.custom[args].cnt >= config.limitCnt then
    --     return
    -- end

    if buyNum + absAct.custom[args].cnt > config.limitCnt then
        return
    end

    -- 钻石价格不为0时，表示钻石购买，则需判断钻石是否足够
    if config.price ~= 0 then
	    if not ObjHuman.checkRMB(human, config.price) then
	    	return Broadcast.sendErr(human, Lang.COMMON_NO_ZUANSHI)
	    end	
	    ObjHuman.decZuanshi(human, -config.price, "abs_custom")
    end

    -- 购买次数增加一次
    -- absAct.custom[args].cnt = absAct.custom[args].cnt + 1
    absAct.custom[args].cnt = absAct.custom[args].cnt + buyNum

     -- 状态设置为已卖完，此时，为防止后续报错，导致可无限购买，故先设置为无法继续购买
    absAct.custom[args].state = 2

     -- 统计物品，并发放
    local item = {}
    for i = 1, #config.fixed do
        -- item[#item+1] = {config.fixed[i][1],config.fixed[i][2]}
        item[#item+1] = {config.fixed[i][1],config.fixed[i][2] * buyNum}
    end
    for i = 1,#absAct.custom[args].selectItem do
        -- item[#item+1] = {absAct.custom[args].selectItem[i][1],absAct.custom[args].selectItem[i][2]}
        item[#item+1] = {absAct.custom[args].selectItem[i][1],absAct.custom[args].selectItem[i][2]*buyNum}
    end
    BagLogic.addItemList(human, item, "abs_custom")   

     -- 判断是否达到限购次数，未达到则将购买状态设为选择物品
    if absAct.custom[args].cnt < config.limitCnt then 
        --absAct.custom[args].selectItem = {}
        -- absAct.custom[args].state = 0
        absAct.custom[args].state = 1
    else
        absAct.custom[args].state = 2
    end

    getAndSendMsg(human,id)
end

-- 是否有红点
function isRed(human, YYInfo, funcConfig)
    local tConfig = AbsActExcel.custom
    if not isRed then
        return false
    end

    local tActDBData = human.db.absAct[SPECIALCISTOMACTID]
    if not tActDBData then
        return false
    end

    local bRed = false
    for k, v in pairs(tConfig) do
        if 0 == v.buyID and k < SPECIALCISTOMNOBUYID then
            if not tActDBData.custom or not  tActDBData.custom[k] then
                bRed = true
            else
                if tActDBData.custom[k].state then
                    bRed = tActDBData.custom[k].state ~= 2 and true or false
                else
                    bRed = true
                end
            end

            if bRed == true then
                break
            end
        end
    end

    return bRed
end

function GetRemainNum(human, nBuyID)
    local tConfig = AbsActExcel.custom
    local nChoseID, nLimitNum = nil, nil
    for nID, v in pairs(tConfig) do
        if v.buyID == nBuyID and nID < SPECIALCISTOMNOBUYID then
            nChoseID = nID
            nLimitNum = v.limitCnt
            break
        end
    end

    local tActDBData = human.db.absAct[SPECIALCISTOMACTID]
    if not tActDBData then
        return 0
    end

    if not nChoseID then
        return 0
    end

    if tActDBData.custom and tActDBData.custom[nChoseID] and tActDBData.custom[nChoseID].cnt then
        return tActDBData.custom[nChoseID].cnt >= nLimitNum and 0 or (nLimitNum - tActDBData.custom[nChoseID].cnt)
    else
        return nLimitNum
    end
end