package scratchcard

import (
	"active/tools"
	"encoding/json"
	"fmt"
	"git.jiaxianghudong.com/webs/pkg/hall"
	"log"
	"sync"
)

var (
	sendPool = sync.Pool{
		New: func() interface{} { return new(MsgFormat) },
	}
	hallSend chan *MsgFormat
)

type MsgFormat struct {
	NoticeList []uint32    `json:"-"`
	Data       interface{} `json:"data"`
}

type Formt struct {
	Notice     int64  `json:"notice"`
	Name       string `json:"name"`
	CardNumber int64  `json:"card_number"`
}

type HallConfig struct {
	HallConfig []*hall.Server `json:"hall_config" yaml:"hall_config"`
}

func SendOrchardMsg(msg Formt, userid uint32) {
	msgFormat := sendPool.Get().(*MsgFormat)
	msgFormat.NoticeList = []uint32{userid}
	msgFormat.Data = msg
	return
	hallSend <- msgFormat
}

func LoopSend() {

	config := HallConfig{}
	hallSend = make(chan *MsgFormat)
	tools.ReloadConfig("./active.yaml", &config)
	err := hall.InitClientCluster(&hall.Config{
		Servers: config.HallConfig,
	})
	if err != nil {
		fmt.Println("没有配置", err)
		return
	}

	fmt.Println(config.HallConfig)
	for {
		select {
		case msg := <-hallSend:
			//log.Println("发送: ",msg)
			listJson, _ := json.Marshal(msg)
			listJson = append(listJson, 0)
			err := hall.UserNotifier().Go(msg.NoticeList, "gbwdd", "", listJson)
			if err != nil {
				log.Println(err)
			}
			msg.NoticeList = make([]uint32, 0)
			sendPool.Put(msg)
		}
	}
}
