// Copyright Fuzamei Corp. 2018 All Rights Reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package client_test

import (
	"github.com/33cn/chain33/queue"
	"github.com/33cn/chain33/types"
)

type mockP2P struct {
}

func (m *mockP2P) SetQueueClient(q queue.Queue) {
	go func() {
		p2pKey := "p2p"
		client := q.Client()
		client.Sub(p2pKey)
		for msg := range client.Recv() {
			switch msg.Ty {
			case types.EventPeerInfo:
				msg.Reply(client.NewMessage(p2pKey, types.EventPeerList, &types.PeerList{}))
			case types.EventGetNetInfo:
				msg.Reply(client.NewMessage(p2pKey, types.EventPeerList, &types.NodeNetInfo{}))
			case types.EventDialPeer, types.EventClosePeer:
				setpeer, ok := msg.GetData().(*types.SetPeer)
				if !ok || setpeer.GetPeerAddr() == "" {
					msg.Reply(client.NewMessage(p2pKey, types.EventReply, &types.Reply{IsOk: false, Msg: []byte(types.ErrInvalidParam.Error())}))
					break
				}
				msg.Reply(client.NewMessage(p2pKey, types.EventReply, &types.Reply{IsOk: true, Msg: []byte("success")}))
			default:
				msg.ReplyErr("Do not support", types.ErrNotSupport)
			}
		}
	}()
}

func (m *mockP2P) Close() {
}
