-------------------------------------------------------
-- VIP逻辑

-- db.vipLv 
-- db.vipExp 
-- db.vipLibao
-- db.vipYueka
-------------------------------------------------------

local VipExcel = require("excel.vip")
local Util = require("common.Util")
local Lang = require("common.Lang")
local Msg = require("core.Msg")
local ObjHuman = require("core.ObjHuman")
local Grid = require("bag.Grid")
local BagLogic = require("bag.BagLogic")
local Broadcast = require("broadcast.Broadcast")
local ChatPaoMaLogic = require("chat.ChatPaoMaLogic")
local HeroLogic = require("hero.HeroLogic")
local RoleHeadLogic = require("role.RoleHeadLogic")
local RoleSystemLogic = require("roleSystem.RoleSystemLogic")
local RoleSystemDefine = require("roleSystem.RoleSystemDefine")
local MonthCard = require("present.MonthCard")

-- vip特权
VIP_POWER1 = 1		-- x皇冠联赛(冠军联赛)次数购买x次
VIP_POWER2 = 2 		-- o快速作战(征战扫荡)次数x次
VIP_POWER3 = 3 		-- o通天塔(恶魔之塔)购买次数x次
VIP_POWER4 = 4		-- o黑市刷新次数x次
VIP_POWER5 = 5		-- x圣树挑战每天复活x次
VIP_POWER6 = 6		-- x女神精力值上限x点
VIP_POWER7 = 7		-- o征战挂机经验+x%
VIP_POWER8 = 8		-- o征战挂机金币+x%
VIP_POWER9 = 9		-- o征战挂机武将经验+x%
VIP_POWER10 = 10	-- o离线挂机收益时长x小时
VIP_POWER11 = 11	-- o摇钱树(点金手)额外金币加成+x%
VIP_POWER12 = 12	-- o英雄背包数量上限+x
VIP_POWER13 = 13	-- o日常副本(黄金圣树)购买各加x次
VIP_POWER14 = 14	-- o道具商城高级召唤券1折购买(某些商品限定vip等级，在shop表中配置)
VIP_POWER15 = 15	-- o开启许愿池(寻宝)多连抽
VIP_POWER16 = 16	-- o激活专属头像
VIP_POWER17 = 17	-- o激活专属头像框
VIP_POWER18 = 18	-- x至尊段位赛战斗跳过
VIP_POWER19 = 19	-- o征战挂机情报+x%
-- VIP_POWER20 = 21	-- o激活专属背景
VIP_POWER21 = 21	-- o抽卡 卷轴折扣特权
VIP_POWER22 = 22    -- o抽卡 钻石特权

VIP_POWER23 = 23	-- 100抽卡 卷轴折扣特权
VIP_POWER24 = 24    -- 100抽卡 钻石特权
-- 礼包状态
local LIBAO_STATE_CANT = 0 		-- 不可购买
local LIBAO_STATE_CAN = 1 		-- 可购买
local LIBAO_STATE_HAD = 2 		-- 已购买

-- 月卡额外奖励状态
local YUEKAITEM_STATE_NONE = 0  -- 未激活
local YUEKAITEM_STATE_CAN = 1 	-- 可领取
local YUEKAITEM_STATE_HAD = 2 	-- 已领取

-- 获取vip等级
function getVipLv(human)
	return human.db.vipLv or 0 -- 
end

-- 加vip经验
function addExp(human, exp)
	do return end
	local oldLv = getVipLv(human)
	local newLv = 0
	local newExp = (human.db.vipExp or 0) + exp
	for lv, config in ipairs(VipExcel.vip) do
		if newExp < config.exp then
			break
		end
		newLv = lv
	end
	human.db.vipExp = newExp
	human.db.vipLv = newLv	
	if oldLv ~= newLv then
		onVipLvChange(human, oldLv, newLv)
	end
	sendQuery(human)
end

-- vip等级改变回调
function onVipLvChange(human, oldLv, newLv)
	-- 激活专属头像/头像框
	for lv = oldLv + 1, newLv do 
		--[[local headID = getPowerArgsByLv(VIP_POWER16, lv)
		if headID and headID > 0 then
			RoleHeadLogic.active(human, RoleHeadLogic.HEAD_TYPE_1, headID)
		end
		local headFrameID = getPowerArgsByLv(VIP_POWER17, lv)
		if headFrameID and headFrameID > 0 then
			RoleHeadLogic.active(human, RoleHeadLogic.HEAD_TYPE_2, headFrameID)
		end
        local backGroundID = getPowerArgsByLv(VIP_POWER20,lv)
        if backGroundID and backGroundID > 0 then
			RoleHeadLogic.active(human, RoleHeadLogic.HEAD_TYPE_6, backGroundID)
		end]]
	end
	HeroLogic.sendHeroBagCap(human)
--	ChatPaoMaLogic.broadcast(human, ChatPaoMaLogic.PAOMA_TYPE_BROAD_TYPE18, newLv)
end

-- 是否购买礼包
function isBuyLibao(human, lv)
	if not human.db.vipLibao then
		return
	end
	return Util.getBit(human.db.vipLibao, lv) > 0 
end

-- 设置已购买
function setBuyLibao(human, lv)
	human.db.vipLibao = human.db.vipLibao or 0
	human.db.vipLibao = Util.setBit(human.db.vipLibao, lv)
end

-- 获取礼包状态
function getLibaoState(human, lv)
	if getVipLv(human) < lv then
		return LIBAO_STATE_CANT
	end
	if isBuyLibao(human, lv) then
		return LIBAO_STATE_HAD
	end
	return LIBAO_STATE_CAN
end

-- 获取月卡每日奖励状态
function getYuekaItemState(human)
	-- todo 是否激活月卡
	-- YUEKAITEM_STATE_NONE

	if human.db.vipYueka then
		return YUEKAITEM_STATE_HAD
	end
	return YUEKAITEM_STATE_CAN
end

function getPowerArgsByLv(human, powerType, vipLv)
	local config = VipExcel.vip[0]
	if not config then return end

	local vipNum = config[powerType]
	local monthNum = MonthCard.getPowerArgs(human, powerType)

	return vipNum + monthNum
end

-- 特权参数
function getPowerArgs(human, powerType)
	local vipLv = getVipLv(human)
	return getPowerArgsByLv(human, powerType, vipLv) 
end

-- 激活特权需要VIP等级
local POWERTYPE_2_NEEDLV = nil
function getPowerNeedLv(powerType)
	if not POWERTYPE_2_NEEDLV then
		POWERTYPE_2_NEEDLV = {}
		for vipLv, config in ipairs(VipExcel.vip) do
			for ptype, value in pairs(config) do
				if type(ptype) == "number" and type(value) == "number" then
					POWERTYPE_2_NEEDLV[vipLv] = POWERTYPE_2_NEEDLV[vipLv] or {}
                    POWERTYPE_2_NEEDLV[vipLv][ptype] = POWERTYPE_2_NEEDLV[vipLv][ptype] or value
				end
			end
		end
	end

    for vipLv, v in pairs(POWERTYPE_2_NEEDLV) do
       if v[powerType] == 1 then
          return vipLv
       end
    end

    return 0
end

-- 获取下级提升属性所需vip等级
function getUpPowerNeedLv(human, powerType)
	local nowVipLv = getVipLv(human)
	local nowValue = getPowerArgsByLv(human, powerType, nowVipLv)
	if type(nowValue) ~= "number" then return end

	local maxVipLv = #VipExcel.vip
	for i = nowVipLv + 1, maxVipLv do
		local value = getPowerArgsByLv(human, powerType, i)
		if value > nowValue then
			return i
		end
	end
end

----------------------------------- msg ---------------------------------------------
-- vip基础信息
sendQueryNet = nil
function sendQuery(human)
	local vipLv = getVipLv(human)
	local nextConfig = VipExcel.vip[vipLv + 1]
	local msgRet = Msg.gc.GC_VIP_INFO
	msgRet.lv = vipLv
	msgRet.maxLv = #VipExcel.vip
	msgRet.curExp = human.db.vipExp or 0
	msgRet.nextExp = nextConfig and nextConfig.exp or 0
    if not sendQueryNet then
       local config = VipExcel.vip
       local len = 0
       for k, v in pairs(config) do
          len = len + 1
          local net = msgRet.vipList[len]
          net.lv = k
          net.vipTab = v.vipTab
          net.exp = v.exp
       end
       msgRet.vipList[0] = len
       sendQueryNet = 1
    end
	Msg.send(msgRet, human.fd)
end

-- VIP特权详情
function sendPowerQuery(human,lv)
    local config = VipExcel.vip[lv]
    if config == nil then
        return
    end
	local msgRet = Msg.gc.GC_VIP_POWER_QUERY
    local len = #VipExcel.vip
    local net = msgRet.data
	net.lv = lv
	net.descs[0] = #config.descs
	for i = 1, net.descs[0] do
		local descNet = net.descs[i]
		descNet.desc = config.descs[i]
        descNet.isNew = config.news[i]
	end
	net.libaoItems[0] = #config.libaoItems
	for i = 1, net.libaoItems[0] do
		local itemID = config.libaoItems[i][1]
		local itemCnt = config.libaoItems[i][2]
		Grid.makeItem(net.libaoItems[i], itemID, itemCnt)
	end
	
    net.libaoPriceNew = config.libaoPriceNew
	net.libaoPriceOld = config.libaoPriceOld
    
    net.yuekaItems[0] = #config.yuekaItems
	for i = 1, net.yuekaItems[0] do
		local itemID = config.yuekaItems[i][1]
		local itemCnt = config.yuekaItems[i][2]
		Grid.makeItem(net.yuekaItems[i], itemID, itemCnt)
	end

	net.libaoState = getLibaoState(human, lv)
	net.yuekaState = getYuekaItemState(human)
    for i = 0,len - 1 do
	    msgRet.dot[i + 1] = getLibaoState(human, i)
    end
    msgRet.dot[0] = len
	Msg.send(msgRet, human.fd)
end

-- 购买VIP礼包
function buyLibao(human, lv)
	local config = VipExcel.vip[lv]
	if not config then return end

	local state = getLibaoState(human, lv)
	if state == LIBAO_STATE_CANT then
		return Broadcast.sendErr(human, Lang.YUNYING_BUY_ERR_VIP)
	end
	if state == LIBAO_STATE_HAD then
		return Broadcast.sendErr(human, Lang.YUNYING_BUY_ERR_HAD)
	end
	if not ObjHuman.checkRMB(human, config.libaoPriceNew) then
		return
	end

	setBuyLibao(human, lv)
	ObjHuman.decZuanshi(human, -config.libaoPriceNew, "vip_libao")	
	for _, item in ipairs(config.libaoItems) do
		local itemID = item[1]
		local itemCnt = item[2]
		BagLogic.addItem(human, itemID, itemCnt, "vip_libao")
	end
    BagLogic.sendItemGetList(human, config.libaoItems, "vip_libao")
	sendPowerQuery(human, lv)
    RoleSystemLogic.onDot(human, RoleSystemDefine.ROLE_SYS_ID_105)
end

-- 领取月卡每日奖励
function getYuekaItem(human)
	local vipLv = getVipLv(human)
	local config = VipExcel.vip[vipLv]
	if not config then return end
	
	local state = getYuekaItemState(human)
	if state == YUEKAITEM_STATE_NONE then
		return Broadcast.sendErr(human, Lang.VIP_YUEKA_ITEM_ERR_GET)
	end
	if state == YUEKAITEM_STATE_HAD then
		return Broadcast.sendErr(human, Lang.YUNYING_GET_ERR_HADGET)
	end
	human.db.vipYueka = true
	for _, item in ipairs(config.yuekaItems) do
		local itemID = item[1]
		local itemCnt = item[2]
		BagLogic.addItem(human, itemID, itemCnt, "vip_yueka")
	end
    BagLogic.sendItemGetList(human, config.yuekaItems, "vip_yueka")
	sendPowerQuery(human, vipLv)	
end

-- 登陆回调
function onLogin(human)
	sendQuery(human)
end

-- 每日刷新
function updateDaily(human)
	human.db.vipYueka = nil 
end

function isDot(human)
    if isBuyLibao(human, 0) then
        return
    end
    return true
end
