---------------------------------------------------
-- 开服有礼
---------------------------------------------------

local Util = require("common.Util")
local Lang = require("common.Lang")
local Broadcast = require("broadcast.Broadcast")
local OpenAct = require("present.OpenAct")
local PresentExcel = require("excel.present")
local OpenActExcel = require("excel.openAct")
local MailExcel = require("excel.mail")
local Msg = require("core.Msg")
local ObjHuman = require("core.ObjHuman")
local MailManager = require("mail.MailManager")
local BagLogic = require("bag.BagLogic")
local Grid = require("bag.Grid")
local KingWorldLogic = require("present.KingWorldLogic")
local YunYingLogic = require("yunying.YunYingLogic")
local PanelDefine = require("broadcast.PanelDefine")
local SevenDayGiftLogic = require("present.SevenDayGiftLogic")
local CommonDB = require("common.CommonDB")
local BuyLogic = require("topup.BuyLogic")
local GuideLogic = require("guide.GuideLogic")

GIFT_STATE_NONE = 0 		-- 不可领取
GIFT_STATE_GET = 1 			-- 可领取
GIFT_STATE_BUY = 2 		    -- 可购买
GIFT_STATE_HADBUY = 3 		-- 已购买

-- 初始db
local function initDB(human)
	human.db.openServerGift = {}
    human.db.openServerGift.free = {}
    human.db.openServerGift.cost = {}
end

local function getDB(human)
	if not human.db.openServerGift then
		initDB(human)
	end
	return human.db.openServerGift
end

-- 免费礼包状态
function getFreeState(human, day)
	if not human.db.openServerGift then
		return
	end
	local data = human.db.openServerGift
    return data.free[day]
end

-- 付费礼包状态
function getCostState(human, day)
	if not human.db.openServerGift then
		return
	end
	local data = human.db.openServerGift
    local limitCnt = OpenActExcel.openServerGift[day].limitCnt
    if data.cost[day] == nil or data.cost[day] < limitCnt then
        return 
    else
        return 0
    end
end

local function setFreeState(human, day)
	local data = getDB(human)
	data.free[day] = GIFT_STATE_HADBUY
end

local function setCostState(human, day)
	local data = getDB(human)
	data.cost[day] = data.cost[day] or 0
	data.cost[day] = data.cost[day] + 1
end

-- 获取礼包状态
function getGiftState(human, day)
	local freeState = getFreeState(human, day)
	local costState = getCostState(human, day)
    local flag = OpenAct.getOpenActTime(OpenAct.OPEN_ACT_SERVER_GIFT)
    local openDay = CommonDB.getServerOpenDay()
    if flag == nil or openDay == nil or openDay < day then
        return GIFT_STATE_NONE
    end

    if freeState == nil then
        return GIFT_STATE_GET
    elseif costState == nil then
        return GIFT_STATE_BUY
    end
	
	return GIFT_STATE_HADBUY
end

-- 查询
function query(human)
	local msgRet = Msg.gc.GC_PRESENT_OPEN_SERVER_QUERY
	msgRet.list[0] = #OpenActExcel.openServerGift
	for day = 1, msgRet.list[0] do
		local cf = OpenActExcel.openServerGift[day]	
		local net = msgRet.list[day]
		net.day = day
		net.state = getGiftState(human, day)
        local itemConfig = nil
        net.nowCnt = 0
        net.limitCnt = 0
        if net.state == GIFT_STATE_BUY then
            itemConfig = cf.freeGift
            --itemConfig = cf.costGift
            net.nowCnt = human.db.openServerGift.cost[day] or 0
            net.limitCnt = cf.limitCnt
        else
            itemConfig = cf.freeGift
        end

		net.item[0] = #itemConfig
		for j = 1, net.item[0] do
			local itemID = itemConfig[j][1]
			local itemCnt = itemConfig[j][2]
			Grid.makeItem(net.item[j], itemID, itemCnt)
		end
        net.buyItem[0] = 0
        --BuyLogic.fontBuyItem(human, net.buyItem, cf.buyID)
	end

	Msg.send(msgRet, human.fd)
end

-- 领取奖励
function gift(human, day)
	local cf = OpenActExcel.openServerGift[day]	
	if not cf then 
		return Broadcast.sendErr(human, Lang.ITEM_BUY_ERROR)
	end

	local state = getGiftState(human, day)
	if state ~= GIFT_STATE_GET then
		if state == GIFT_STATE_NONE then
			return Broadcast.sendErr(human, Lang.PRESENT_SEVENLOGINDAY_GET_DAYERR)
		end
		return Broadcast.sendErr(human, Lang.YUNYING_GET_ERR_HADGET)
	end

	setFreeState(human, day)
	--发放物品
    local itemConfig = OpenActExcel.openServerGift[day].freeGift

    local len = #itemConfig
    for i = 1,len do
        local itemID = itemConfig[i][1]
        local itemCnt = itemConfig[i][2]
        BagLogic.addItem(human,itemID,itemCnt,"open_server_gift")
    end

	BagLogic.sendItemGetList(human, itemConfig, "open_server_gift")
	for k, v in pairs(funcID) do
	    YunYingLogic.updateIcon(YYInfo[k], human)
	    YunYingLogic.sendGroupUpdate(YYInfo[k], human, PanelDefine.PANEL_ID_3602)
        break
    end
	if day == 2 then
        KingWorldLogic.setState(human,2)
    end
    GuideLogic.setDoSpecialGuide(human, GuideLogic.SKIPTYPE_JUMP_OPEN_SERVER_GIFT)
	query(human)
end

-- 红点
function isRed(human)
    for day in ipairs(OpenActExcel.openServerGift) do
		local state = getGiftState(human, day)	
		if state == GIFT_STATE_NONE then
			break
		end
		if state == GIFT_STATE_GET then
			return true
		end
    end	
end

function buyGift(human,day)
    -- 校验状态是否为能买
    local state = getGiftState(human, day)	
    if state ~= GIFT_STATE_BUY then
        return
    end

    -- 设置购买数量
    setCostState(human,day)

    -- 发放道具
    local itemConfig = OpenActExcel.openServerGift[day].costGift

    local len = #itemConfig
    for i = 1,len do
        local itemID = itemConfig[i][1]
        local itemCnt = itemConfig[i][2]
        BagLogic.addItem(human,itemID,itemCnt,"open_server_gift")
    end
    BagLogic.sendItemGetList(human, itemConfig, "open_server_gift")
    query(human)
end

function isBaseOpen(human,noSend)
	local openLv = YunYingLogic.getOpenLvByPanelID(PanelDefine.PANEL_ID_3602)
    if human.db.lv < openLv then
        if not noSend then
            local str = Util.format(Lang.ROLE_LEV_ERROR,openLv)
            return Broadcast.sendErr(human,str)
        end
    end
    local flag = OpenAct.getOpenActTime(OpenAct.OPEN_ACT_SERVER_GIFT)
    if not flag then
		return
	end

    return true
end

function isOpen(human)
	if isBaseOpen(human,true) then
		return true
	end
end

function getLeftTime()
    local openDay = CommonDB.getServerOpenDay()
   if openDay == nil then
	  return 0
   end

   local OpenActConfig = OpenActExcel.openAct[1]
   if not OpenActConfig then return 0 end

   local sDay = OpenActConfig.sDay;
   local eDay = OpenActConfig.eDay;

   if openDay >= sDay and openDay <= eDay then
      local time = os.time();
   	  local openTime = CommonDB.getServerOpenTime()
      if openTime == 0 then
		 return 0
	  end

      local endTime = Util.getDayStartTime(openTime) + eDay * 86400
      return endTime - time
   end

   return 0
end