
local OpenActExcel = require("excel.openAct")
local CommonDB = require("common.CommonDB")
local Util = require("common.Util")
local Gm = require("chat.Gm")
local Msg = require("core.Msg")
local ObjHuman = require("core.ObjHuman")
local Log = require("common.Log")
local Broadcast = require("broadcast.Broadcast")
local Lang = require("common.Lang")
local ItemDefine = require("bag.ItemDefine")
local HeroLogic = require("hero.HeroLogic")
local BagLogic = require("bag.BagLogic")
local HeroGrid = require("hero.HeroGrid")
local DrawCardExcel = require("excel.drawCard").drawCard
local HeroExcel = require("excel.hero").hero
local MailExcel = require("excel.mail")
local ChatPaoMaLogic = require("chat.ChatPaoMaLogic")
local HeroBook = require("hero.HeroBook")
local AbsActLogic = require("absAct.AbsActLogic")
local AbsActDefine = require("absAct.AbsActDefine")
local YunYingLogic = require("yunying.YunYingLogic")
local PanelDefine = require("broadcast.PanelDefine")
local DrawCardLogic = require("drawCard.DrawCardLogic")
local ZhanbuLogic = require("zhanbu.ZhanbuLogic")
local MailManager = require("mail.MailManager")
local MailDefine = require("mail.MailIdDefine")

function initAfterHot()
    local oepenTime = CommonDB.getServerOpenTime()
    if oepenTime == 0 then return end
    local oneDay = 24 * 60 * 60
    for k, config in pairs(OpenActExcel.limitPerpleHero) do
        local time  =  (config.openStart - 1) * oneDay + oepenTime
        local time2 =  config.openEnd * oneDay + oepenTime
        local startTime = Util.getDayStartTime(time)
        local endTime = Util.getDayStartTime(time2) - 1
        -- ����ǵ���ͽ����Ļ
        if config.openStart == config.openEnd then
            endTime = startTime + oneDay - 1
        end
        config.startTime = startTime
        config.endTime = endTime
    end
end


function onLogin(human)
    

end

function initDB(human)
    local config = getConfigByActID()
    if human.db.limitPerpleHero then
        if human.db.limitPerpleHero.endTime ~= config.endTime then
            -- ����

            human.db.limitPerpleHero = {}
            human.db.limitPerpleHero.endTime = config.endTime
            human.db.limitPerpleHero.like = config.heroID[1]
        end
    else
        human.db.limitPerpleHero = {}
        human.db.limitPerpleHero.endTime = config.endTime
        human.db.limitPerpleHero.like = config.heroID[1]
    end
end

function checkOpen()
    local now = os.time()
    for k , config in pairs(OpenActExcel.limitPerpleHero) do
        if config.startTime and now >= config.startTime and now < config.endTime then
            return true
        end
    end
end

function isOpen(human, YYInfo, funcConfig)
    local config = OpenActExcel.limitPerpleHero[funcConfig.param]
    if not config then return end
    if not config.startTime then return end
    local now = os.time()
    if now >= config.startTime and now <= config.endTime then
        return true
    end 
end

--
function getLeftTime(human, YYInfo, funcConfig)
    if isOpen(human, YYInfo, funcConfig) then
        local config = OpenActExcel.limitPerpleHero[funcConfig.param]
        return config.endTime - os.time()
    end
    return 0
end

-- ���ػ����
function getAndSendMsg(human, param)
    local config = OpenActExcel.limitPerpleHero[param]
    if not config then return end
    local now = os.time()
    if now < config.startTime and now > config.endTime then
        return 
    end 
    -- ��������
    local msgRet = Msg.gc.GC_OA_LIMIT_PERPLEHERO_QUERY
    msgRet.startTime = config.startTime
    msgRet.endTime   = config.endTime
    initDB(human)
    local perpleHero = human.db.limitPerpleHero
    if not perpleHero.like then
        perpleHero.like = config.heroID[1]
    end
    msgRet.perpleHero = perpleHero.like
    msgRet.leftCnt = config.cnt
    if perpleHero.noTargetHeroCnt then
        msgRet.leftCnt = config.cnt - perpleHero.noTargetHeroCnt
    end

    local len = #config.heroID
    for i = 1,len do
        HeroGrid.makeHeroSimpleByID(msgRet.heroList[i], config.heroID[i],nil,nil,nil,true)
    end
    msgRet.heroList[0] = len

    Msg.send(msgRet,human.fd)

end

-- ��������Ӣ��
function setPerpleHero(human, heroID, param)
    local config = OpenActExcel.limitPerpleHero[param]
    if not config then return end
    local now = os.time()
    if now < config.startTime and now >= config.endTime then
        return 
    end 
    local check = false
    for k, id in ipairs(config.heroID) do
        if id == heroID then
            check = true
            break
        end
    end

    if not check then return end
    initDB(human)
    local perpleHero = human.db.limitPerpleHero

   if perpleHero.like  then
        if not ObjHuman.checkRMB(human, 2000) then
	    	return
	    end

        ObjHuman.decZuanshi(human,-2000,"abs_perpleHero")
    end
    perpleHero.like = heroID
end

function onDrawCardActive(human, id, op, actConfig, skip,isAct)
    local state = false
    local now = os.time()
    for k, config in pairs(OpenActExcel.limitPerpleHero) do
        if now >= config.startTime and now <= config.endTime then
            state = true 
        end 
    
    end

    -- û�л����
    if not state then
        return
    end
    
    local config = DrawCardExcel[id]
    if not config then return end

    local heroCnt = nil
    if op == DrawCardLogic.DRAWCARD_OP_1 then
        heroCnt = 1
    elseif op == DrawCardLogic.DRAWCARD_OP_2 then
        heroCnt = 10
    else
        return
    end

    if HeroLogic.getEmptyCnt(human) < heroCnt then        
        return Broadcast.sendErr(human, Lang.HERO_BAG_FULL)
    end

    if not DrawCardLogic.checkUseItem(human, id, op, config,isAct) then
        return
    end

    initDB(human)
    local perpleHero = human.db.limitPerpleHero
 
    local heroLen = 0
    local heroList = {}    
    local heroNewList = {}
    local heroIndexList = {}
    local fenjielist = {}
    local heroIDs = nil
    local perpleHero = human.db.limitPerpleHero
    local randomMin = math.random(1, heroCnt)
    
    local actConfig = getConfigByActID()
     
    local noTargetHeroCnt2 = perpleHero.noTargetHeroCnt or 0
    if op == DrawCardLogic.DRAWCARD_OP_2 then
        noTargetHeroCnt2 = noTargetHeroCnt2 + 10
    else
        noTargetHeroCnt2 = noTargetHeroCnt2 + 1
    end

    for i = 1, heroCnt do
        config = DrawCardExcel[id]
     
        
        if id == DrawCardLogic.DRAWCARD_ID2 and op == DrawCardLogic.DRAWCARD_OP_1 then
            config = DrawCardExcel[DrawCardLogic.DRAWCARD_ID6]
        end

        local indexState = i == randomMin 
        if noTargetHeroCnt2 >= actConfig.cnt then
            indexState = false
        end
        
        if id == DrawCardLogic.DRAWCARD_ID2 and op == DrawCardLogic.DRAWCARD_OP_2 and indexState then
            config = DrawCardExcel[DrawCardLogic.DRAWCARD_ID5]
        elseif  id == DrawCardLogic.DRAWCARD_ID2 and op == DrawCardLogic.DRAWCARD_OP_1 then
            config = DrawCardExcel[DrawCardLogic.DRAWCARD_ID6]
        end
        local heroID = DrawCardLogic.randHeroID(human, id, op, config, actConfig, indexState, isAct)
        local heroConfig = HeroExcel[heroID]
        local star = heroConfig.star
		local name = heroConfig.name
        local grade = heroConfig.grade
        local isNew = not HeroBook.isGet(human, heroConfig.id, heroConfig.star)
        local heroIndex, fjlist = HeroLogic.addHero(human, heroID,nil, 1, "abs_perpleHero")
        heroLen = heroLen + 1
        heroList[heroLen] = heroID
        heroNewList[heroLen] = isNew
        heroIndexList[heroLen] = heroIndex
        if fjlist and type(fjlist) == "table" then
            for fjItemID, fjItemCnt in pairs(fjlist) do
                fenjielist[fjItemID] = (fenjielist[fjItemID] or 0) + fjItemCnt
            end
        end
        if not heroIDs then
            heroIDs = heroID
        else
            heroIDs = heroIDs .. "|" .. heroID
        end

     
        perpleHero.noTargetHeroCnt = perpleHero.noTargetHeroCnt or 0
        perpleHero.noTargetHeroCnt = perpleHero.noTargetHeroCnt + 1


        if heroID == perpleHero.like then
            perpleHero.noTargetHeroCnt = 0 
        end

        if id == DrawCardLogic.DRAWCARD_ID1 then
			ChatPaoMaLogic.broadcast(human, ChatPaoMaLogic.PAOMA_TYPE_BROAD_TYPE3, grade, heroID)
        elseif id == DrawCardLogic.DRAWCARD_ID2 then
			ChatPaoMaLogic.broadcast(human, ChatPaoMaLogic.PAOMA_TYPE_BROAD_TYPE1, grade, heroID)
        elseif id == DrawCardLogic.DRAWCARD_ID3 then			
			ChatPaoMaLogic.broadcast(human, ChatPaoMaLogic.PAOMA_TYPE_BROAD_TYPE2, grade, heroID)
        elseif id == DrawCardLogic.DRAWCARD_ID6 then
            ChatPaoMaLogic.broadcast(human, ChatPaoMaLogic.PAOMA_TYPE_BROAD_TYPE21, grade, heroID)
        end
    end

    DrawCardLogic.sendDrawOp(human, id, op, heroList, fenjielist, heroNewList,heroIndexList,isAct)
	for k, v in pairs(funcID) do
	    YunYingLogic.sendGroupUpdate(YYInfo[k], human, PanelDefine.PANEL_ID_3608)
        break
    end
    --Log.write(Log.LOGID_OSS_DRAWCARD, human.db._id, human.db.account, human.db.name, human.db.lv, id, heroIDs, itemID or 0, cnt or 0, zuanshi or 0, 0)
    Log.write(Log.LOGID_OSS_DRAWCARD, human.db._id, human.db.newUniqueTag, human.db.name, human.db.lv, id, heroIDs, itemID or 0, cnt or 0, zuanshi or 0, 0)

    perpleHero.drawCardCnt = perpleHero.drawCardCnt or 0
    perpleHero.drawCardCnt = perpleHero.drawCardCnt + heroCnt
    if perpleHero.drawCardCnt >= 10 then
        perpleHero.drawCardCnt = perpleHero.drawCardCnt - 10
        ZhanbuLogic.addZhanbuItem(human,1)
    end
    
    YunYingLogic.onCallBack(human, "onDrawCard", heroCnt, nil, id)   
end

function getConfigByActID()
    local id 
    local now = os.time()
    for k, config in pairs(OpenActExcel.limitPerpleHero) do
        if now >= config.startTime and now <= config.endTime then
            id = k 
            break
        end 
    end

    return OpenActExcel.limitPerpleHero[id], id
end

function getLuckDraw(human, op)
    local actConfig = getConfigByActID()
    local randomNum = math.random(1,10000)
    local luckDraw = false
    if randomNum <= actConfig.rate then
        luckDraw = true
    end

    local perpleHero = human.db.limitPerpleHero
    local noTargetHeroCnt = perpleHero.noTargetHeroCnt or 0
    noTargetHeroCnt = noTargetHeroCnt + 1

    if luckDraw == false and noTargetHeroCnt >= actConfig.cnt then
        luckDraw = true
    end
   
    if luckDraw then 
        return perpleHero.like 
    end
end


function heroCombat(human,heroID)
    Gm.d3.combat2(human,heroID)
end

function isRed(human, YYInfo, funcConfig)
    if BagLogic.getItemCnt(human, ItemDefine.ITEM_DRAWCARD_JINGYING_ID) >= 10 then
        return true
    end
end

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