-- 新商业化活动2—— 烹饪嘉年华
-- 玩法: 从配置中随机一条配置，让玩家消耗特定道具后, 选择材料进行合成食物, 只有索引未1~5的材料才是正确的，玩家选对多少个材料就得到几星奖励
--db
--[=[
    human.db.absAct[id] = {
            nowFoodIdx = nil,
            fiveStarFoodArray = nil, { {配置idx, 玩家选择的食材(字符串)}  }    -- 玩家合成的五星级食物列表，活动结束也不删除
            isTenTimes = nil,   -- 新增需求, 是否选择了10倍消耗, 为true表示选择了10倍消耗, 为其他值则表示没有
        }
]=]--

local Msg = require("core.Msg")
local Grid = require("bag.Grid")
local Util = require("common.Util")
local Lang = require("common.Lang")
local BagLogic = require("bag.BagLogic")
local Broadcast = require("broadcast.Broadcast")
local CycleActivityLogic = require("yunying.CycleActivity")
local actVariableCfg = require("excel.commercializationActivity").variable[4]
local makeFoodCfg = require("excel.commercializationActivity").makeFood


-- 本活动日志标识
local LOGTAG = "cycleAct_makeFood"


local function getActData(human, actId)
    return human.db.absAct[actId]
end

local function updateActData(human, actId, newIdx)
    local actData = getActData(human, actId)
    actData.nowFoodIdx = newIdx
end

local function update5StarFoodArray(human, actId, foodIdx, foodStr)
    local actData = getActData(human, actId)
    actData.fiveStarFoodArray = actData.fiveStarFoodArray or {}
    actData.fiveStarFoodArray[#actData.fiveStarFoodArray+1] = {foodIdx, foodStr}
end


-- 随机出一条配置
local function randConfig()
    return math.random(1, #makeFoodCfg)
end

-- 计算一个table中有多少个值与给定值相同
local function calcSameValCnt(t, val)
    if not t or not val then
        return 0
    end

    local cnt = 0
    for _, v in pairs(t) do
        if v == val then
            cnt = cnt + 1
        end
    end

    return cnt
end

local function isRepeatRecord(human, actId, nowFoodIdx)
    local actData = getActData(human, actId)
    if not actData then
        return false
    end

    for _, recordInfo in ipairs(actData.fiveStarFoodArray or {}) do
        if recordInfo[1] == nowFoodIdx then
            return true
        end
    end

    return false
end



function isOpen(human, YYInfo, funcConfig)
    local state, endTime, startTime = CycleActivityLogic.isStarted(human, funcConfig.funcID)
    if not state then return end

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


function Query(human, actId)
    local actData = getActData(human, actId)
    if not actData then
        return Broadcast.sendErr(human, Lang.YUNYING_ERR_TIME)
    end

    local nowFoodIdx = actData.nowFoodIdx
    if not nowFoodIdx then
        nowFoodIdx = randConfig()
        updateActData(human, actId, nowFoodIdx)
    end

    local msgRet = Msg.gc.GC_CYCLMAKEFOOD_QUERY
    local foodCfg = makeFoodCfg[nowFoodIdx]
    msgRet.isTenTimes = 0

    local mul = 1
    if actData.isTenTimes then
        mul = 10
        msgRet.isTenTimes = 1
    end

    msgRet.tagetFoodName = foodCfg.targetFoodName
    Grid.makeItem(msgRet.itemInfo, actVariableCfg.va1[1], actVariableCfg.va1[2] * mul)
    local foodList = msgRet.foodList

    for k,v in ipairs(foodCfg.iconList) do
        foodList[0] = k
        foodList[k] = v
    end

    Msg.send(msgRet, human.fd)
end

function MakeFood(human, actId, foodStr)
    local actData = getActData(human, actId)
    if not actData then
        return Broadcast.sendErr(human, Lang.YUNYING_ERR_TIME)
    end

    local selectFoodVec = Util.split(foodStr, ",", true)
    local selectFoodCnt = #selectFoodVec

    if selectFoodCnt <= 0 then
        return Broadcast.sendErr(human, Lang.MUST_SELECT_ONE)
    end

    -- 最多选择5个材料
    if selectFoodCnt > 5 then
        return Broadcast.sendErr(human, Lang.NUM_EXCEED)
    end

    local rightNum = 0

    for _, val in ipairs(selectFoodVec) do
        local num = calcSameValCnt(selectFoodVec, val)
        if num >= 2 then
            return Broadcast.sendErr(human, Lang.COMMON_ARGUMENT_ERROR)
        end

        -- 索引1~5的食材是正确的
        if val >= 1 and val <= 5 then
            rightNum = rightNum + 1
        end
    end


    local mul = 1
    if actData.isTenTimes then
        mul = 10
    end

    -- 消耗道具检测
    local itemId, itemCnt = actVariableCfg.va1[1], actVariableCfg.va1[2]
    if BagLogic.getItemCnt(human, itemId) < itemCnt * mul then
        return Broadcast.sendErr(human, Lang.COMMON_ITEM_NOT_ENOUGH)
    end
    BagLogic.delItem(human, itemId, itemCnt*mul, LOGTAG)


    local msgRet = Msg.gc.GC_CYCLMAKEFOOD_MAKEFOOD
    msgRet.stars = rightNum
    local itemArray = msgRet.itemArray


    -- 发放奖励
    -- local len = 0
    -- local rewardList = {}
    local nowFoodIdx = actData.nowFoodIdx
    local foodCfg = makeFoodCfg[nowFoodIdx]

    -- 五星级食物制作成功则保存记录
    if rightNum >= 5 and not isRepeatRecord(human, actId, nowFoodIdx) then
        update5StarFoodArray(human, actId, nowFoodIdx, foodStr)
    end


    rightNum = rightNum + 1 -- 奖励数组中，index为1的是0星奖励, index为6的是5星奖励
    itemArray[0] = 0

    for idx, itemCfg in ipairs(foodCfg.reward[rightNum] or {}) do
        -- len = len + 1
        -- rewardList[len] = { itemCfg[1], itemCfg[2] }
        BagLogic.addItem(human, itemCfg[1], itemCfg[2] * mul, LOGTAG)

        itemArray[0] = idx
        Grid.makeItem(itemArray[idx], itemCfg[1], itemCfg[2] * mul)
    end
    -- BagLogic.addItemList(human, rewardList, LOGTAG)


    -- 通知客户端结果

    Msg.send(msgRet, human.fd)


    -- 随机出下一轮目标食物
    local nextFoodIdx = randConfig()
    updateActData(human, actId, nextFoodIdx)

    -- 推送最新数据给客户端
    -- Query(human, actId)
end

function Get5StarFoodArray(human, actId)
    local actData = getActData(human, actId)
    if not actData then
        return Broadcast.sendErr(human, Lang.YUNYING_ERR_TIME)
    end

    local msgRet = Msg.gc.GC_CYCLMAKEFOOD_GET_5STARfOODLIST
    local foodList = msgRet.foodList
    foodList[0] = 0
    msgRet.isEnd = 0

    local fiveStarFoodArray = actData.fiveStarFoodArray
    if not fiveStarFoodArray then
        msgRet.isEnd = 1
        return Msg.send(msgRet, human.fd)
    end

    local len = 0
    local msgMaxCnt = 50
    local nowFoodCnt = #fiveStarFoodArray

    for _, foodInfo in ipairs(fiveStarFoodArray) do
        len = len + 1
        local foodIdx = foodInfo[1]
        local foodCfg = makeFoodCfg[foodIdx]

        foodList[len].tagetFoodName = foodCfg.targetFoodName
        local selectFoodVec = Util.split(foodInfo[2], ",", true)
        for l, iconIdx in ipairs(selectFoodVec) do
            foodList[len].foodArray[0] = l
            foodList[len].foodArray[l] = foodCfg.iconList[iconIdx]
        end

        if len >= msgMaxCnt then
            nowFoodCnt = nowFoodCnt - len
            if nowFoodCnt <= 0 then
                msgRet.isEnd = 1
            end
            foodList[0] = len
            Msg.send(msgRet, human.fd)
            len = 0
        end
    end

    if len > 0 then
        msgRet.isEnd = 1
        foodList[0] = len
        Msg.send(msgRet, human.fd)
    end

end

function SelectTenTimes(human, actId, opType)
    local actData = getActData(human, actId)
    if not actData then
        return Broadcast.sendErr(human, Lang.YUNYING_ERR_TIME)
    end

    if opType == 0 then
        actData.isTenTimes = false
    elseif opType == 1 then
        actData.isTenTimes = true
    else
        return Broadcast.sendErr(human, Lang.COMMON_ARGUMENT_ERROR)
    end

    Query(human, actId)
end