-----------------------------------------------------
-- 英雄图鉴
-----------------------------------------------------

local Util = require("common.Util")
local Msg = require("core.Msg")
local HeroExcel = require("excel.hero")
local HeroGrid = require("hero.HeroGrid")
local HeroDefine = require("hero.HeroDefine")
local ChengjiuLogic = require("chengjiu.ChengjiuLogic")
local Grid = require("bag.Grid")
local BagLogic = require("bag.BagLogic")
local RoleSystemLogic = require("roleSystem.RoleSystemLogic")
local RoleSystemDefine = require("roleSystem.RoleSystemDefine")

CAMP_2_MSGLIST = CAMP_2_MSGLIST or {}

LIEZHUAN_REWARD_NO = 0 --不可领取
LIEZHUAN_REWARD_CAN = 1 --可领取
LIEZHUAN_REWARD_HAD = 2 --已领取

CAMP_HEROS = {}

CAMP_MAX = 5

local function getHeros(camp)
    if CAMP_HEROS[camp] then return CAMP_HEROS[camp] end
    local Heros
    local heroCf
    for id,v in Util.pairsByKeys(HeroExcel.tujian) do
        heroCf = HeroExcel.hero[id]
        if not heroCf then
           assert(nil, "id err "..id)
        end

        CAMP_HEROS[heroCf.camp] = CAMP_HEROS[heroCf.camp] or {}
        CAMP_HEROS[heroCf.camp][id] = CAMP_HEROS[heroCf.camp][id] or {}
        Heros = CAMP_HEROS[heroCf.camp][id]
        Heros[#Heros + 1] = id
    end
    return CAMP_HEROS[camp]
end

local function getRewardStateByHeroID(human, heroID, star)
	local heroBookDB = human.db.heroBook
	if heroBookDB and 
       heroBookDB.lieZhuanReward and 
       heroBookDB.lieZhuanReward[heroID] and 
       heroBookDB.lieZhuanReward[heroID][star] then
		return LIEZHUAN_REWARD_HAD
	end

	if isGet(human, heroID, star) then
		return LIEZHUAN_REWARD_CAN
	end

	return LIEZHUAN_REWARD_NO
end

local function isDotByHeroID(human,heroID, star)
    if human.db.lv < 9 then
       return false
    end

	local state = getRewardStateByHeroID(human,heroID, star)
	if state == LIEZHUAN_REWARD_CAN then
		return true
	end
end


local function getTuJianStar(human, heroID)
    -- 显示获取的最高星级
    local showStar
    local config = HeroExcel.tujian[heroID]
    if not config then return end
    local starList = config.star
    local index = 0
    for k, _star in ipairs(config.star) do
        local get = isGet(human, heroID, _star)
        if get then
           showStar = _star
           index = index + 1
        end
    end
    return showStar, index
end


local function getRewardStar(human, heroID)
    -- 显示获取的最高星级
    local star
    local config = HeroExcel.tujian[heroID]
    if not config then return end
    local starList = config.star
    local index = 0
    for k, _star in ipairs(config.star) do
        local state = getRewardStateByHeroID(human, heroID, _star)
        if state == LIEZHUAN_REWARD_CAN then
           star = _star
           index = k
           break
        end
    end

    if star == nil then
        for k, _star in ipairs(config.star) do
           local state = getRewardStateByHeroID(human, heroID, _star)
           if state == LIEZHUAN_REWARD_NO then
              star = _star
              index = k
              break
           end
        end
    end

    return star, index
end


function isGetHero(human, heroID)
   if not human then return end
   local heroCf = HeroExcel.hero[heroID]
   if not heroCf then return end
   return getRewardStateByHeroID(human, heroID, heroCf.star)
end


local function warperQuerySimpleCSList(human, heroID, showStar, listNet)
    local heroGrid = HeroGrid.getCacheHeroGridTujian(heroID)
    heroGrid.star = showStar
    HeroGrid.makeHeroSimple(listNet.heros, heroGrid, nil, human, nil, true)
    listNet.isDot = isDotByHeroID(human, heroID, showStar) and 1 or 0
end


function querySimple_CS(human, camp)
    local heros = getHeros(camp)
    local msgRet = Msg.gc.GC_HERO_BOOK_QUERY_CS
    msgRet.camp = camp
    msgRet.list[0] = 0
    local rewardState
    local listNet
    local len = 0
    for _id, heroIDs in pairs(heros) do
        len = len + 1
        local showStar = getTuJianStar(human, _id)
        listNet = msgRet.list[len]
        warperQuerySimpleCSList(human, heroIDs[1], showStar, listNet)
        if msgRet.list[0] >= HeroDefine.PAGE_HERO_COUNT then
            msgRet.list[0] = len
            Msg.send(msgRet, human.fd)
            page = page + 1					
	    end	
    end
    msgRet.list[0] = len

    len = 0  
    for index = 1, CAMP_MAX do
       local heros = getHeros(index)
       local setDot = 0
       for _id, heroIDs in pairs(heros) do
           local showStar = getTuJianStar(human, _id)
           local isDot = isDotByHeroID(human, heroIDs[1], showStar)
           if isDot == true then
              setDot = 1
              break
           end
       end

       len = len + 1
       msgRet.dot[len] = setDot
    end
    msgRet.dot[0] = len
    Msg.send(msgRet, human.fd)
end




function CG_HERO_BOOK_LIEZHUAN_QUERY(human, heroID)
	local config = HeroExcel.tujian[heroID]
	if not config then return end

    local nowStar = getTuJianStar(human, heroID)
    local showStar, index = getRewardStar(human, heroID)
	local heroGrid = HeroGrid.getCacheHeroGridTujian(heroID)
    if index == 0 then 
        index = #config.star
        showStar = config.star[index]
    end

    local nextIndex = index
    if not config.star[nextIndex] then
        return
    end

    local nextStar = config.star[nextIndex] or showStar
    showStar = showStar or nextStar
    
	local msgRet = Msg.gc.GC_HERO_BOOK_LIEZHUAN_QUERY
	msgRet.id = heroID
    heroGrid.star = showStar
	HeroGrid.makeHeroDynamic(msgRet.data, heroGrid)
    
	msgRet.lieZhuanDesc = config.desc
    msgRet.rewardState = getRewardStateByHeroID(human, heroID, showStar)
    msgRet.nextStar = nextStar
    msgRet.curStar = nowStar or 0
    if msgRet.curStar > msgRet.nextStar then
        msgRet.curStar = msgRet.nextStar 
    end 
    local itemID = config.lieZhuanReward[index][1]
	local itemCnt = config.lieZhuanReward[index][2]
	Grid.makeItem(msgRet.lieZhuanReward, itemID, itemCnt)
	Msg.send(msgRet, human.fd)
end

function isGet(human, heroID, star)
	if not human.db.heroBook then return end
	local heroConfig = HeroExcel.hero[heroID] 
	if not heroConfig then return end
    if not human.db.heroBook[heroID] then return end
	return human.db.heroBook[heroID][star]
end

function onAddHero(human, heroID, star)
	local heroConfig = HeroExcel.hero[heroID]
	if not heroConfig then return end

    
    local config = HeroExcel.tujian[heroID]
    if not config then return end

    human.db.heroBook = human.db.heroBook or {}
    human.db.heroBook[heroID] = human.db.heroBook[heroID] or {}
    for k, _star in ipairs(config.star) do
       if star >= _star and human.db.heroBook[heroID][_star] == nil then
          human.db.heroBook[heroID][_star] = true
       end
    end
end

function CG_HERO_BOOK_LIEZHUAN_GET(human, heroID)
	local heroConfig = HeroExcel.hero[heroID]
	if not heroConfig then return end

	local config = HeroExcel.tujian[heroID]
	if not config then return end

    local showStar, index = getRewardStar(human, heroID)
    if not showStar or not index then return end

    local rewardState = getRewardStateByHeroID(human, heroID, showStar)
    if rewardState ~= LIEZHUAN_REWARD_CAN then
       return
    end

	human.db.heroBook = human.db.heroBook or {}
	human.db.heroBook.lieZhuanReward = human.db.heroBook.lieZhuanReward or {}
	human.db.heroBook.lieZhuanReward[heroID] = human.db.heroBook.lieZhuanReward[heroID] or {}
    human.db.heroBook.lieZhuanReward[heroID][showStar] = true

	local itemID = config.lieZhuanReward[index][1]
	local itemCnt = config.lieZhuanReward[index][2]
    BagLogic.cleanMomentItemList()
    BagLogic.updateMomentItem(1, itemID,itemCnt)
    BagLogic.addMomentItemList(human, "hero_book_liezhuan")
    RoleSystemLogic.onDot(human, RoleSystemDefine.ROLE_SYS_ID_305)
    CG_HERO_BOOK_LIEZHUAN_QUERY(human, heroID)
end

-- 一键领取
function CG_HERO_BOOK_ONECLIEK_GET(human)
    local tConfig = HeroExcel.tujian
    if not tConfig then
        return
    end

    local tItem = {}
    for nHeroID, v in pairs(tConfig) do
        for nIndex, nStar in ipairs(v.star) do
            local rewardState = getRewardStateByHeroID(human, nHeroID, nStar)
            if rewardState == LIEZHUAN_REWARD_CAN then
                human.db.heroBook = human.db.heroBook or {}
                human.db.heroBook.lieZhuanReward = human.db.heroBook.lieZhuanReward or {}
                human.db.heroBook.lieZhuanReward[nHeroID] = human.db.heroBook.lieZhuanReward[nHeroID] or {}
                human.db.heroBook.lieZhuanReward[nHeroID][nStar] = true
    
                local itemID = v.lieZhuanReward[nIndex][1]
                local itemCnt = v.lieZhuanReward[nIndex][2]
    
                if not tItem[itemID] then
                    tItem[itemID] = 0
                end
    
                tItem[itemID] = tItem[itemID] + itemCnt
            end
        end
    end

    if nil ~= _G.next(tItem) then
        local tGoods = {}
        for nGoodsID, nGoodsNum in pairs(tItem) do
            table.insert(tGoods, {nGoodsID, nGoodsNum})
        end

        BagLogic.addItemList(human, tGoods, "hero_book_liezhuan")
        RoleSystemLogic.onDot(human, RoleSystemDefine.ROLE_SYS_ID_305)

        for camp = 1, CAMP_MAX do
            querySimple_CS(human, camp)
        end
    end
end

function isDot(human)
    for camp = 1, CAMP_MAX do
       local heros = getHeros(camp)
       for _id, heroIDs in pairs(heros) do
           local showStar = getTuJianStar(human, _id)
           local isDot = isDotByHeroID(human, heroIDs[1], showStar)
           if isDot and isDot ~= 0 then
              return 1
           end 
       end
    end
    return false
end

function queryHeroBookById(human,heroID,star)
    local msgRet = Msg.gc.GC_HERO_BOOK_SINGLE_QUERY
    local heroGrid = HeroGrid.getCacheHeroGridTujian(heroID)
    heroGrid.star = star
    HeroGrid.makeHeroSimple(msgRet.hero, heroGrid, nil, human, nil, true)
    Msg.send(msgRet, human.fd)
end

-- 查询所有激活的图鉴英雄
function HeroBook_AllHeroTujian_Query(human)
    local msgRet = Msg.gc.GC_ALL_HERO_BOOK_QUERY
    msgRet.list[0] = 0
    msgRet.isEnd = 0

    if not human.db.heroBook then
        msgRet.isEnd = 1
        return Msg.send(msgRet, human.fd)
    end

    local heroIdArr = {}
    for heroId in pairs(human.db.heroBook) do
        if type(heroId) == "number" then
            heroIdArr[#heroIdArr+1] = heroId
        end
    end

    local len = 0
    local msgMaxLen = 20
    local heroNum = #heroIdArr

    for _, heroId in ipairs(heroIdArr) do
        len = len + 1
        msgRet.list[0] = len
        local showStar = getTuJianStar(human, heroId)
        local heroGrid = HeroGrid.getCacheHeroGridTujian(heroId)
        heroGrid.star = showStar
        HeroGrid.makeHeroSimple(msgRet.list[len], heroGrid, nil, human, nil, true)

        if len >= msgMaxLen then
            heroNum = heroNum - len
            if heroNum <= 0 then
                msgRet.isEnd = 1
                return Msg.send(msgRet, human.fd)
            end

            Msg.send(msgRet, human.fd)
            len = 0
        end
    end

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