local FuwenExcel = require("excel.fuwen")
local Util = require("common.Util")
local FuwenLogic = require("fuwen.FuwenLogic")
local PfLogic = require("platform.PfLogic")
local FuwenDefine = require("fuwen.FuwenDefine")
local Broadcast = require("broadcast.Broadcast")
local Lang = require("common.Lang")


FUWEN_SKILL_PRIM = FUWEN_SKILL_PRIM or {}   --符文初级技能列表
FUWEN_SKILL_INTE = FUWEN_SKILL_INTE or {}   --符文中级技能列表    
FUWEN_SKILL_HIGH = FUWEN_SKILL_HIGH or {}   --符文高级技能列表
FUWEN_SKILL_TOP = FUWEN_SKILL_TOP or {}     --符文顶级技能列表
FUWEN_SKILL_RARE = FUWEN_SKILL_RARE or {}   --符文稀有技能列表


function getFuwenSkillMaxCnt(fuwenConfig)
    local nowNum, maxNum = 0, 0

    for _, skillInfo in ipairs(fuwenConfig.skillRate) do
        nowNum = skillInfo[1] + skillInfo[2] + skillInfo[3] + skillInfo[4]

        if nowNum > maxNum then
            maxNum = nowNum
        end
    end

    return maxNum
end


function initAfterHot()	
    Util.initTable(FUWEN_SKILL_PRIM)
    Util.initTable(FUWEN_SKILL_INTE)
    Util.initTable(FUWEN_SKILL_HIGH)
    Util.initTable(FUWEN_SKILL_TOP)
    Util.initTable(FUWEN_SKILL_RARE)
    for fuwenID, config in pairs(FuwenExcel.skill) do
        if config.isFuwenSkill == 1 then -- 符文技能
            if config.lv == 1 then
                FUWEN_SKILL_PRIM[#FUWEN_SKILL_PRIM + 1] = fuwenID
            elseif config.lv == 2 then
                FUWEN_SKILL_INTE[#FUWEN_SKILL_INTE + 1] = fuwenID
            elseif config.lv == 3 then
                FUWEN_SKILL_HIGH[#FUWEN_SKILL_HIGH + 1] = fuwenID
            elseif config.lv == 4 then
                FUWEN_SKILL_TOP[#FUWEN_SKILL_TOP + 1] = fuwenID
            end
            if config.rare == 1 then
                FUWEN_SKILL_RARE[#FUWEN_SKILL_RARE + 1] = fuwenID
            end
        end
    end
end

function create(id)
	local tb = {}
	tb.id = id
    tb.attr = {}
    tb.skill = nil
    -- 生成随机属性和技能
    genFuwenAttr(tb,nil)
    tb.attrTemp = {}
    tb.skillTemp = nil
	return tb
end



function genFuwenAttr(fuwenGrid, luckRefresh)
    -- Util.initTable(fuwenGrid.attr)
    Util.initTable(fuwenGrid.skill)

    local choseSkill = {}
    local fuwenConfig = FuwenExcel.fuwen[fuwenGrid.id]
    if fuwenConfig == nil then
        return
    end

    -- 绝对值属性抽选 必定有且仅有1条
    local cnt = #fuwenConfig.absAttrPool
    -- local randIndex = math.random(1, cnt)
    -- local attrKey = fuwenConfig.absAttrPool[randIndex][1]
    -- local attrVal = fuwenConfig.absAttrPool[randIndex][2]
    -- fuwenGrid.attr[1] = {attrKey, attrVal}
    if not fuwenGrid.attr[1] or not fuwenGrid.attr[1][3] or fuwenGrid.attr[1][3] ~= 1 then
        -- local cnt = #fuwenConfig.absAttrPool
        local randIndex = math.random(1, cnt)
        local attrKey = fuwenConfig.absAttrPool[randIndex][1]
        local attrVal = fuwenConfig.absAttrPool[randIndex][2]
        fuwenGrid.attr[1] = {attrKey, attrVal}
    end

	
    -- 百分比属性抽选 不一定有
    cnt = #fuwenConfig.percentAttrPool
    if cnt > 0 then
        -- local randIndex = math.random(1, cnt)
        -- local attrKey = fuwenConfig.percentAttrPool[randIndex][1]
        -- local attrVal = fuwenConfig.percentAttrPool[randIndex][2]
        -- fuwenGrid.attr[2] = {attrKey, attrVal}
        -- if fuwenGrid.id == 80006 then
        --     randIndex = math.random(1, cnt)
        --     attrKey = fuwenConfig.percentAttrPool[randIndex][1]
        --     attrVal = fuwenConfig.percentAttrPool[randIndex][2]
        --     fuwenGrid.attr[3] = {attrKey, attrVal}
        -- end
        if not fuwenGrid.attr[2] or not fuwenGrid.attr[2][3] or fuwenGrid.attr[2][3] ~= 1 then
            local randIndex = math.random(1, cnt)
            local attrKey = fuwenConfig.percentAttrPool[randIndex][1]
            local attrVal = fuwenConfig.percentAttrPool[randIndex][2]
            fuwenGrid.attr[2] = {attrKey, attrVal}
        end

        if fuwenGrid.id == 80006 and (not fuwenGrid.attr[3] or not fuwenGrid.attr[3][3] or fuwenGrid.attr[3][3] ~= 1) then
            local randIndex = math.random(1, cnt)
            local attrKey = fuwenConfig.percentAttrPool[randIndex][1]
            local attrVal = fuwenConfig.percentAttrPool[randIndex][2]
            fuwenGrid.attr[3] = {attrKey, attrVal}
        end
    end

    -- 锁定技能的处理
    local allSkillCnt = 0
    local subTb = {}
    local skillLockList = fuwenGrid.skillLockList

    if skillLockList and next(skillLockList) then
        fuwenGrid.skill = fuwenGrid.skill or {}

        for skillId in pairs(skillLockList) do
            allSkillCnt = allSkillCnt + 1
            fuwenGrid.skill[allSkillCnt] = skillId

            local skillCfg = FuwenExcel.skill[skillId]
            subTb[skillCfg.lv] = (subTb[skillCfg.lv] or 0) - 1

            choseSkill[skillCfg.groupID] = 1
        end

        local skillMaxNum = getFuwenSkillMaxCnt(fuwenConfig)
        if #fuwenGrid.skill >= skillMaxNum then
            for fuwenID, config in pairs(FuwenExcel.skill) do
                if config.isFuwenSkill == 1 then -- 符文技能
                    subTb[config.lv] = -999
                end
            end
        end
    end


    --幸运重铸技能抽选
    if luckRefresh == 1 then
        cnt = #fuwenConfig.skillRate
        fuwenGrid.skill = fuwenGrid.skill or {}
        local skillConfig = FuwenExcel.skill
        local total = 0
        for i = 1,cnt do
            if fuwenConfig.skillRate[i][3] >= 1 or fuwenConfig.skillRate[i][4] >= 1 then
                total = total + fuwenConfig.skillRate[i][5]
            end
        end
        local randomNum = math.random(1, total)
        local skillGroup = nil
        for i = 1,cnt do
            if fuwenConfig.skillRate[i][3] >= 1 or fuwenConfig.skillRate[i][4] >= 1 then
                if randomNum <= fuwenConfig.skillRate[i][5] then
                    skillGroup = fuwenConfig.skillRate[i]
                    break
                else
                    randomNum = randomNum - fuwenConfig.skillRate[i][5]
                end
            end
        end
        if skillGroup ~= nil then
            local primSkillCnt = (skillGroup[1] or 0) + (subTb[1] or 0)
            local inteSkillCnt = (skillGroup[2] or 0) + (subTb[2] or 0)
            local highSkillCnt = (skillGroup[3] or 0) + (subTb[3] or 0)
            local topSkillCnt = (skillGroup[4] or 0) + (subTb[4] or 0)
            --local isHighRare = true
            --if highSkillCnt >= 1 and topSkillCnt >= 1 then
            --    local randCnt = math.random(1, 2)
            --    if randCnt == 2 then
            --        isHighRare = false
            --    end
            --elseif topSkillCnt > 1 then
            --    isHighRare = false
            --end

            -- local allSkillCnt = 0
            local skillRandomIndex = 0
            for i = 1,primSkillCnt do
                local maxPrimSkillCnt = #FUWEN_SKILL_PRIM
                while true do
                    skillRandomIndex = math.random(1,maxPrimSkillCnt)
                    local newSkill = FUWEN_SKILL_PRIM[skillRandomIndex]
                    if choseSkill[skillConfig[newSkill].groupID] == nil then
                        allSkillCnt = allSkillCnt + 1
                        fuwenGrid.skill[allSkillCnt] = newSkill
                        choseSkill[skillConfig[newSkill].groupID] = 1
                        break
                    end 
                end
            end
            for i = 1,inteSkillCnt do
                local maxInteSkillCnt = #FUWEN_SKILL_INTE
                while true do
                    skillRandomIndex = math.random(1,maxInteSkillCnt)
                    local newSkill = FUWEN_SKILL_INTE[skillRandomIndex]
                    if choseSkill[skillConfig[newSkill].groupID] == nil then
                        allSkillCnt = allSkillCnt + 1
                        fuwenGrid.skill[allSkillCnt] = newSkill
                        choseSkill[skillConfig[newSkill].groupID] = 1
                        break
                    end 
                end
            end
            for i = 1,highSkillCnt do
                local maxHighSkillCnt = 0
                --if i == 1 and isHighRare then
                --    maxHighSkillCnt = #FUWEN_SKILL_RARE
                --    while true do
                --        skillRandomIndex = math.random(1,maxHighSkillCnt)
                --        local newSkill = FUWEN_SKILL_RARE[skillRandomIndex]
                --        if choseSkill[skillConfig[newSkill].groupID] == nil then
                --            allSkillCnt = allSkillCnt + 1
                --            fuwenGrid.skill[allSkillCnt] = newSkill
                --            choseSkill[skillConfig[newSkill].groupID] = 1
                --            break
                --        end 
                --    end
                --else
                    maxHighSkillCnt = #FUWEN_SKILL_HIGH
                    while true do
                        skillRandomIndex = math.random(1,maxHighSkillCnt)
                        local newSkill = FUWEN_SKILL_HIGH[skillRandomIndex]
                        if choseSkill[skillConfig[newSkill].groupID] == nil then
                            allSkillCnt = allSkillCnt + 1
                            fuwenGrid.skill[allSkillCnt] = newSkill
                            choseSkill[skillConfig[newSkill].groupID] = 1
                            break
                        end 
                    end
                --end
            end
            for i = 1,topSkillCnt do
                local maxTopSkillCnt = 0
                if i == 1 and not isHighRare then
                    maxTopSkillCnt = #FUWEN_SKILL_RARE
                    while true do
                        skillRandomIndex = math.random(1,maxTopSkillCnt)
                        local newSkill = FUWEN_SKILL_RARE[skillRandomIndex]
                        if choseSkill[skillConfig[newSkill].groupID] == nil then
                            allSkillCnt = allSkillCnt + 1
                            fuwenGrid.skill[allSkillCnt] = newSkill
                            choseSkill[skillConfig[newSkill].groupID] = 1
                            break
                        end 
                    end
                else
                    maxTopSkillCnt = #FUWEN_SKILL_TOP
                    skillRandomIndex = math.random(1,maxTopSkillCnt)
                    while true do
                        skillRandomIndex = math.random(1,maxTopSkillCnt)
                        local newSkill = FUWEN_SKILL_TOP[skillRandomIndex]
                        if choseSkill[skillConfig[newSkill].groupID] == nil then
                            allSkillCnt = allSkillCnt + 1
                            fuwenGrid.skill[allSkillCnt] = newSkill
                            choseSkill[skillConfig[newSkill].groupID] = 1
                            break
                        end 
                    end
                end
            end
        end
        return
    end

    -- 抽选技能组合
    cnt = #fuwenConfig.skillRate
    if cnt > 0 then
        fuwenGrid.skill = fuwenGrid.skill or {}
        local skillConfig = FuwenExcel.skill
        local total = 0
        for i = 1,cnt do
            total = total + fuwenConfig.skillRate[i][5]
        end
        local randomNum = math.random(1, total)
        local skillGroup = nil
        for i = 1,cnt do
            if randomNum <= fuwenConfig.skillRate[i][5] then
                skillGroup = fuwenConfig.skillRate[i]
                break
            else
                randomNum = randomNum - fuwenConfig.skillRate[i][5]
            end
        end
        if skillGroup ~= nil then
            local primSkillCnt = (skillGroup[1] or 0) + (subTb[1] or 0)
            local inteSkillCnt = (skillGroup[2] or 0) + (subTb[2] or 0)
            local highSkillCnt = (skillGroup[3] or 0) + (subTb[3] or 0)
            local topSkillCnt = (skillGroup[4] or 0) + (subTb[4] or 0)
            -- local allSkillCnt = 0
            local skillRandomIndex = 0
            for i = 1,primSkillCnt do
                local maxPrimSkillCnt = #FUWEN_SKILL_PRIM
                while true do
                    skillRandomIndex = math.random(1,maxPrimSkillCnt)
                    local newSkill = FUWEN_SKILL_PRIM[skillRandomIndex]
                    if choseSkill[skillConfig[newSkill].groupID] == nil then
                        allSkillCnt = allSkillCnt + 1
                        fuwenGrid.skill[allSkillCnt] = newSkill
                        choseSkill[skillConfig[newSkill].groupID] = 1
                        break
                    end 
                end
            end
            for i = 1,inteSkillCnt do
                local maxInteSkillCnt = #FUWEN_SKILL_INTE
                while true do
                    skillRandomIndex = math.random(1,maxInteSkillCnt)
                    local newSkill = FUWEN_SKILL_INTE[skillRandomIndex]
                    if choseSkill[skillConfig[newSkill].groupID] == nil then
                        allSkillCnt = allSkillCnt + 1
                        fuwenGrid.skill[allSkillCnt] = newSkill
                        choseSkill[skillConfig[newSkill].groupID] = 1
                        break
                    end 
                end
            end
            for i = 1,highSkillCnt do
                local maxHighSkillCnt = #FUWEN_SKILL_HIGH
                while true do
                    skillRandomIndex = math.random(1,maxHighSkillCnt)
                    local newSkill = FUWEN_SKILL_HIGH[skillRandomIndex]
                    if choseSkill[skillConfig[newSkill].groupID] == nil then
                        allSkillCnt = allSkillCnt + 1
                        fuwenGrid.skill[allSkillCnt] = newSkill
                        choseSkill[skillConfig[newSkill].groupID] = 1
                        break
                    end 
                end
            end
            for i = 1,topSkillCnt do
                local maxTopSkillCnt = #FUWEN_SKILL_TOP
                while true do
                    skillRandomIndex = math.random(1,maxTopSkillCnt)
                    local newSkill = FUWEN_SKILL_TOP[skillRandomIndex]
                    if choseSkill[skillConfig[newSkill].groupID] == nil then
                        allSkillCnt = allSkillCnt + 1
                        fuwenGrid.skill[allSkillCnt] = newSkill
                        choseSkill[skillConfig[newSkill].groupID] = 1
                        break
                    end 
                end
            end
        end
    end
end

function setFuwenSkillAttrByGM(human,fuwenID,attr1,val1,attr2,val2,skill1,skill2)
    local fuwen = {}
    fuwen.id = fuwenID
    fuwen.attr = {}
    fuwen.skill = {}
    fuwen.attr[1] = {attr1,val1}
    fuwen.attr[2] = {attr2,val2}
    fuwen.skill[1] = skill1
    fuwen.skill[2] = skill2
    FuwenLogic.addByGrid(human,fuwen,"gm")
end

-- 返回符文背包空余格子数
function getEmptyCnt(human)
	local emptyCnt = 0
	for i = 1, FuwenDefine.FUWEN_BAG_MAX_CNT do
		if human.db.fuwenBag[i] == nil then
			emptyCnt = emptyCnt + 1	
		end
	end
	return emptyCnt
end

-- 获得符文背包第一个空格子下标
function getEmptyIndex(human)
	for index = 1, FuwenDefine.FUWEN_BAG_MAX_CNT do
		local grid = human.db.fuwenBag[index]
		if grid == nil then			
			return index
		end
	end
end

function checkEmptyCnt(human,itemCnt)
    if itemCnt > getEmptyCnt(human) then
        return Broadcast.sendErr(human,Lang.COMMON_BAG_FULL)
    end
    return true
end