--- 英雄皮肤
local Grid = require("bag.Grid")
local BuyExcel = require("excel.buy").buy
local BagLogic = require("bag.BagLogic")
local HeroSkinExcel = require("excel.present").heroSkin
local SkinExcel = require("excel.skin").skin
local SkinLogic = require("skin.SkinLogic")
local HeadFrameExcel = require("excel.role").headFrame
local RoleHeadLogic = require("role.RoleHeadLogic")
local Msg = require("core.Msg")
local Json = require("common.Json")
local BuyLogic = require("topup.BuyLogic")

local GC_SKIN_QUERY = 54

local function genSkinUnit(human,id)
    local cfg = HeroSkinExcel[id]
    local buyCfg = BuyExcel[cfg.buyId]
    local region = human.region or "CN"
    local buyItem = {}
    BuyLogic.fontBuyItem(human, buyItem, cfg.buyId)
    local skinId = cfg.content[1]
    local skinCfg = SkinExcel[skinId]
    local attrs = {}
    for _,v in pairs(skinCfg.attrs) do 
        attrs[#attrs + 1] = {
            key = v[1],
            value = v[2],
        }
    end
    local iconframeId = cfg.content[2]
    local iconframeCfg = HeadFrameExcel[iconframeId]
    local headNet = {}
	headNet.id = iconframeId 
	headNet.desc = iconframeCfg.desc or ""
	headNet.keepTime = iconframeCfg.keepTime
	headNet.name = iconframeCfg.name or ""
	headNet.camp = iconframeCfg.camp or 0
    local len = #iconframeCfg.attrs
    headNet.attrs = {}
    for i = 1,len do
        headNet.attrs[i] = {
            key = iconframeCfg.attrs[i][1],
            value = iconframeCfg.attrs[i][2],
        }
    end
    return {
        id = id,
        buyItem = buyItem,
        skinData = {
            Id = skinId,
            attrs = attrs
        },
        Iconframe = headNet
    }
end


local function populateMsg(human, net, id)
    local cfg = HeroSkinExcel[id]
    local skinId = cfg.content[1]
    local skinCfg = SkinExcel[skinId]
    local iconframeId = cfg.content[2]
    local iconframeCfg = HeadFrameExcel[iconframeId]

    net.id = id
    net.skinId = skinId
    net.headId = iconframeId
    net.headDesc = iconframeCfg.desc or ""
    net.headKeepTime = iconframeCfg.keepTime
    net.headName = iconframeCfg.name or ""
    net.headCamp = iconframeCfg.camp or 0

    BuyLogic.fontBuyItem(human, net.buyItem, cfg.buyId)

    net.skinAttr[0] = #skinCfg.attrs
    for i, attrInfo in ipairs(skinCfg.attrs) do
        net.skinAttr[i].key = attrInfo[1]
        net.skinAttr[i].value = attrInfo[2]
    end

    net.headAttr[0] = #iconframeCfg.attrs
    for i, attrInfo in ipairs(iconframeCfg.attrs) do
        net.headAttr[i].key = attrInfo[1]
        net.headAttr[i].value = attrInfo[2]
    end
end


local function populateMsg2(human, net, id)
    local cfg = HeroSkinExcel[id]
    local skinId = cfg.content[1]
    local skinCfg = SkinExcel[skinId]

    net.id = id
    BuyLogic.fontBuyItem(human, net.buyItem, cfg.buyId)
    net.skinData.Id = skinId
    net.skinData.attrs[0] = #skinCfg.attrs
    for i, v in ipairs(skinCfg.attrs) do
        net.skinData.attrs[i].key = v[1]
        net.skinData.attrs[i].value = v[2]
    end

    local iconframeId = cfg.content[2]
    local iconframeCfg = HeadFrameExcel[iconframeId]

    net.Iconframe.id = iconframeId
    net.Iconframe.desc = iconframeCfg.desc or ""
    net.Iconframe.keepTime = iconframeCfg.keepTime or 0
    net.Iconframe.name = iconframeCfg.name or ""
    net.Iconframe.camp = iconframeCfg.camp or 0
    net.Iconframe.attrs[0] = #iconframeCfg.attrs

    for i, v in ipairs(iconframeCfg.attrs) do
        net.Iconframe.attrs[i].key = v[1]
        net.Iconframe.attrs[i].value = v[2]
    end
end


-----------------------------------------

-- 检查玩家是否已拥有指定皮肤
local function hasSkin(human, skinId)
    if not human.db.skinBag then
        return false
    end
    for i = 1, #human.db.skinBag do
        local skin = human.db.skinBag[i]
        if skin and skin.id == skinId then
            return true
        end
    end
    return false
end

function query(human,panelId)
    local retLen = 0
    local ret = {}
    for id,cfg in pairs(HeroSkinExcel) do
        if cfg.panelId == panelId then
            -- 检查玩家是否已拥有该皮肤，如果已拥有则跳过
            local skinId = cfg.content[1]
            if not hasSkin(human, skinId) then
                -- ret[#ret+1] = genSkinUnit(human,id)
                retLen = retLen + 1
                ret[retLen] = id
            end
        end
    end
    -- return {
    --     list = ret
    -- }

    local msgRet = Msg.gc.GC_SKIN_DATA_QUERY
    -- msgRet.ret = GC_SKIN_QUERY
    msgRet.ret = 1
    msgRet.isEnd = 0
    msgRet.skinData[0] = 0
    if retLen == 0 then
        msgRet.isEnd = 1
        return Msg.send(msgRet,human.fd)
    end

    local len, msgMaxLen = 0, 20

    for _, id in ipairs(ret) do
        len = len + 1
        populateMsg(human, msgRet.skinData[len], id)

        if len >= msgMaxLen then
            msgRet.skinData[0] = len
            retLen = retLen -  len

            if retLen <= 0 then
                msgRet.isEnd = 1
                return Msg.send(msgRet,human.fd)
            end

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

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

end

function buy(human,buyId)
    local skinCfg
    for _,cfg in pairs(HeroSkinExcel) do
        if cfg.buyId == buyId then 
            skinCfg = cfg
            break
        end
    end
    assert(skinCfg,"invalid buyId")
    -- 解锁皮肤和头像
    SkinLogic.skinUnlock(human,skinCfg.content[1]) --BagLogic.addItemList(human,skinCfg.content,"skin_buy")
    RoleHeadLogic.active(human,RoleHeadLogic.HEAD_TYPE_2,skinCfg.content[2])
    --刷新
    -- local data=query(human,skinCfg.panelId)
    -- RefreshClient(human,GC_SKIN_QUERY,data)
    query(human,skinCfg.panelId)
end


function RefreshClient(human,type,data) 
    local msgRet = Msg.gc.GC_ROLE_CHANGE_BASEINFO
    msgRet.ret = type
    msgRet.tip = Json.Encode(data)
    Msg.send(msgRet,human.fd)
end

function SendSkinInfoByMoney(human, nMoney, nItemID)
    if not human or 0 >= nMoney then
        return
    end

    -- local ret = {}
    -- for id,cfg in pairs(HeroSkinExcel) do
    --     local nBuyID = cfg.buyId
    --     local tBuyCfg = BuyExcel[nBuyID]
    --     if tBuyCfg then
    --         local region = human.region or "CN"
    --         local nTrueMoney = tBuyCfg[region]
    --         if nMoney == nTrueMoney then
    --             ret[#ret+1] = genSkinUnit(human,id)
    --         end
    --     end
    -- end

    -- local data = {
    --     list = ret
    -- }
    -- local msgRet = Msg.gc.GC_ITEM_SKIN_INFO
    -- msgRet.nItemID = nItemID
    -- msgRet.data = Json.Encode(data)

    -- Msg.send(msgRet,human.fd)


    local msgRet = Msg.gc.GC_ITEM_SKIN_INFO
    msgRet.nItemID = nItemID
    msgRet.data[0] = 0
    msgRet.isEnd = 0
    msgRet.isStart = 1

    local skinArr = {}
    for id,cfg in pairs(HeroSkinExcel) do
        local nBuyID = cfg.buyId
        local tBuyCfg = BuyExcel[nBuyID]
        if tBuyCfg then
            local region = human.region or "CN"
            local nTrueMoney = tBuyCfg[region]
            if nMoney == nTrueMoney then
                skinArr[#skinArr+1] = id
            end
        end
    end

    local skinNum = #skinArr
    local len, msgOneceLen = 0, 10

    for _, skinId in ipairs(skinArr) do
        len = len + 1
        msgRet.data[0] = len
        populateMsg2(human, msgRet.data[len], skinId)

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

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

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

function BuySkinBySuiPian(human, nSkinID, nItemID)
    local skinCfg
    for _,cfg in pairs(HeroSkinExcel) do
        if cfg.content[1] == nSkinID then 
            skinCfg = cfg
            break
        end
    end

    assert(skinCfg,"invalid buyId")

    SkinLogic.skinUnlock(human,skinCfg.content[1]) --BagLogic.addItemList(human,skinCfg.content,"skin_buy")
    RoleHeadLogic.active(human,RoleHeadLogic.HEAD_TYPE_2,skinCfg.content[2])
    --刷新
    -- local data=query(human,skinCfg.panelId)
    -- RefreshClient(human,GC_SKIN_QUERY,data)
    query(human,skinCfg.panelId)

    BagLogic.delItem(human, nItemID, 1, "item_use")
end

-- function GetRemainNum(human, nBuyID)
--     local skinCfg = nil
--     for _,cfg in pairs(HeroSkinExcel) do
--         if cfg.buyId == nBuyID then 
--             skinCfg = cfg
--             break
--         end
--     end

--     if not skinCfg then
--         return 0
--     end

--     if not human.db.skinBag then
--         return 1
--     end

--     local isUnlock = false
--     for i = 1,#human.db.skinBag do
--         local skin = human.db.skinBag[i]
--         if skin.id == skinCfg.content[1] then 
--             isUnlock =true
--             break
--         end
--     end

--     return (isUnlock == false) and 1 or 0
-- end