require "3rd/pblua/login_pb"
require "3rd/pbc/protobuf"

local sproto = require "3rd/sproto/sproto"
local core = require "sproto.core"
local print_r = require "3rd/sproto/print_r"

UIPromptCtrl = {}
local this = UIPromptCtrl

local prompt
local transform
local gameObject
local prefabItem

function UIPromptCtrl.Awake()
	logWarn("UIPromptCtrl.Awake--->>")
	panelMgr:CreatePanel(self, UILayer.Common, UiNames.Prompt, this.OnCreateOK)
end

--启动事件--
function UIPromptCtrl.OnCreateOK(obj)
	gameObject = obj
	transform = obj.transform

    prompt = transform:GetComponent('LuaBehaviour')
    prefabItem = self.prefabItem
    prompt:AddClick(self.btnOpen, this.OnClick)
    
    this.InitPanel()

	logWarn("Start lua--->>"..gameObject.name)
end

--初始化面板--
function UIPromptCtrl.InitPanel()
	local count = 100 
	local parent = self.gridParent
	for i = 1, count do
		local go = newObject(prefabItem)
		go.name = 'Item'..tostring(i)
		go.transform:SetParent(parent)
		go.transform.localScale = Vector3.one
        go.transform.localPosition = Vector3.zero
        go.gameObject:SetActive(true)

        local button = go:GetComponent('Button')
        prompt:AddClick(button, this.OnItemClick)

	    local label = go.transform:Find('Text')
	    label:GetComponent('Text').text = tostring(i)
	end
end

--滚动项单击--
function UIPromptCtrl.OnItemClick(go)
    log(go.name)
end

--单击事件--
function UIPromptCtrl.OnClick(go)
    --[[

	if TestProtoType == ProtocalType.BINARY then
		this.TestSendBinary()
	end
	if TestProtoType == ProtocalType.PB_LUA then
		this.TestSendPblua()
	end
	if TestProtoType == ProtocalType.PBC then
		this.TestSendPbc()
	end
	if TestProtoType == ProtocalType.SPROTO then
		this.TestSendSproto()
    end
    ]]
    panelMgr:ClosePanel('Prompt')
	logWarn("OnClick---->>>"..go.name)
end

--测试发送SPROTO--
function UIPromptCtrl.TestSendSproto()
    local sp = sproto.parse [[
    .Person {
        name 0 : string
        id 1 : integer
        email 2 : string

        .PhoneNumber {
            number 0 : string
            type 1 : integer
        }

        phone 3 : *PhoneNumber
    }

    .AddressBook {
        person 0 : *Person(id)
        others 1 : *Person
    }
    ]]

    local ab = {
        person = {
            [10000] = {
                name = "Alice",
                id = 10000,
                phone = {
                    { number = "123456789" , type = 1 },
                    { number = "87654321" , type = 2 },
                }
            },
            [20000] = {
                name = "Bob",
                id = 20000,
                phone = {
                    { number = "01234567890" , type = 3 },
                }
            }
        },
        others = {
            {
                name = "Carol",
                id = 30000,
                phone = {
                    { number = "9876543210" },
                }
            },
        }
    }
    local code = sp:encode("AddressBook", ab)
    ----------------------------------------------------------------
    local buffer = ByteBuffer.New()
    buffer:WriteShort(Protocal.Message)
    buffer:WriteByte(ProtocalType.SPROTO)
    buffer:WriteBuffer(code)
    networkMgr:SendMessage(buffer)
end

--测试发送PBC--
function UIPromptCtrl.TestSendPbc()
    local path = Util.DataPath.."lua/3rd/pbc/addressbook.pb"

    local addr = io.open(path, "rb")
    local buffer = addr:read "*a"
    addr:close()
    protobuf.register(buffer)

    local addressbook = {
        name = "Alice",
        id = 12345,
        phone = {
            { number = "1301234567" },
            { number = "87654321", type = "WORK" },
        }
    }
    local code = protobuf.encode("tutorial.Person", addressbook)
    ----------------------------------------------------------------
    local buffer = ByteBuffer.New()
    buffer:WriteShort(Protocal.Message)
    buffer:WriteByte(ProtocalType.PBC)
    buffer:WriteBuffer(code)
    networkMgr:SendMessage(buffer)
end

--测试发送PBLUA--
function UIPromptCtrl.TestSendPblua()
    local login = login_pb.LoginRequest()
    login.id = 2000
    login.name = 'game'
    login.email = 'jarjin@163.com'
    local msg = login:SerializeToString()
    ----------------------------------------------------------------
    local buffer = ByteBuffer.New()
    buffer:WriteShort(Protocal.Message)
    buffer:WriteByte(ProtocalType.PB_LUA)
    buffer:WriteBuffer(msg)
    networkMgr:SendMessage(buffer)
end

--测试发送二进制--
function UIPromptCtrl.TestSendBinary()
    local buffer = ByteBuffer.New()
    buffer:WriteShort(Protocal.Message)
    buffer:WriteByte(ProtocalType.BINARY)
    buffer:WriteString("ffff我的ffffQ靈uuu")
    buffer:WriteInt(200)
    networkMgr:SendMessage(buffer)
end

--关闭事件--
function UIPromptCtrl.Close()
	panelMgr:ClosePanel(UiNames.Prompt)
end

return this