-- ABS 积分召唤活动

local Config = require("Config")
local AbsActExcel = require("excel.absAct")
local Lang = require("common.Lang")
local Util = require("common.Util")
local Msg = require("core.Msg")
local AbsActDefine = require("absAct.AbsActDefine")
local ObjHuman = require("core.ObjHuman")
local Broadcast = require("broadcast.Broadcast")
local MangHeLogic = require("absAct.MangHeLogic")
local MailExcel = require("excel.mail")
local MailManager = require("mail.MailManager")
local CommonDB = require("common.CommonDB")
local AbsActLogic = require("absAct.AbsActLogic")
local HeroGrid = require("hero.HeroGrid")
local DrawCardLogic = require("drawCard.DrawCardLogic")
local Log = require("common.Log")
local HeroBook = require("hero.HeroBook")
local HeroExcel = require("excel.hero")
local HeroLogic = require("hero.HeroLogic")
local ChatPaoMaLogic = require("chat.ChatPaoMaLogic")

function isOpen(human, YYInfo, funcConfig)
    return AbsActLogic.isStarted(human, funcConfig.funcID)
end

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

function isRed(human, YYInfo, funcConfig)
    return false
end

function genAbsActData(config)
   return {
      actId = config.actId,
   }
end

function getAndSendMsg(human, id, config)
    local absAct = human.db.absAct[id]
    if not absAct then 
        print("not found human.db.absAct")
        return 
    end

    local actId = config.actId
    local msgRet = Msg.gc.GC_ABS_ACT_JIFEN_DRAW_QUERY
    local jifenDrawConfig = AbsActExcel.absJifenDraw[actId]
    if not jifenDrawConfig then 
        print("not found jifenDrawConfig",actId)
        return 
    end
    if actId ~= absAct.actId then
      local finish = absAct.finish or os.time()
      human.db.absAct[id] = {
         actId = actId,
         finish = finish,
      }
      absAct = human.db.absAct[id]
   end
    local len = 0
    for k, config in ipairs(jifenDrawConfig.heroList) do
       len = len + 1
       if len > #msgRet.list then

          break
       end

       local net = msgRet.list[len]
       if net then
          net.id = k
          net.cur = absAct[k] or 0 
          net.max = config[3]
          net.cost = config[2]
          HeroGrid.makeHeroSimpleByID(net.hero, config[1])
       end
    end
    msgRet.list[0] = len
    msgRet.randomCost = DrawCardLogic.MAX_JIFEN
    msgRet.curJifen = DrawCardLogic.getJifen(human)
    Msg.send(msgRet, human.fd)
end

-- 积分购买
function buy(human, id, actID)
   local state,endTime, starTime = AbsActLogic.isStarted(human, actID)
   if not state then return end

   local config = AbsActExcel.absActivity[actID]
   if not config then return end

   local jifenDrawConfig = AbsActExcel.absJifenDraw[config.actId]
   if not jifenDrawConfig then return end

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

   local heroConfig = jifenDrawConfig.heroList[id]
   if not heroConfig then return end

   -- 扣除积分
   if DrawCardLogic.getJifen(human) < heroConfig[2] then
      return Broadcast.sendErr(human, Lang.DRAWCARD_ERR_NOJIFEN)  
   end
   
   absAct[id] = absAct[id] or 0
   if absAct[id] >= heroConfig[3] then
      return
   end

   DrawCardLogic.updateJifen(human, -heroConfig[2])
   absAct[id] = absAct[id] + 1

   local heroID = heroConfig[1]
   local drawConfig = HeroExcel.hero[heroID]
   ChatPaoMaLogic.broadcast(human, ChatPaoMaLogic.PAOMA_TYPE_BROAD_TYPE4, drawConfig.grade, heroID)
   local isNew = not HeroBook.isGet(human, drawConfig.id, drawConfig.star)
   local heroIndex, fenjielist = HeroLogic.addHero(human, heroID, nil, 1, "abs_jifenDraw")
   --Log.write(Log.LOGID_OSS_DRAWCARD, human.db._id, human.db.account, human.db.name, human.db.lv, DrawCardLogic.DRAWCARD_ID4, heroID, 0, 0, 0, DrawCardLogic.getJifen(human))
   Log.write(Log.LOGID_OSS_DRAWCARD, human.db._id, human.db.newUniqueTag, human.db.name, human.db.lv, DrawCardLogic.DRAWCARD_ID4, heroID, 0, 0, 0, DrawCardLogic.getJifen(human))
   DrawCardLogic.sendDrawOp(human, DrawCardLogic.DRAWCARD_ID4, 0, {heroID}, fenjielist, {isNew}, {heroIndex}, AbsActDefine.ABS_ACT_TYPE_7)
end