package nt

import (
	"../zkeeper"
	"github.com/samuel/go-zookeeper/zk"
	"go.uber.org/zap"
)

// zookeeper 事件子回调
type CallbackPath func(zkc *zkeeper.ZkClient, path string, ev zk.EventType)

var (
	MapPathCb map[string]CallbackPath = make(map[string]CallbackPath)
)

// zk 回调池与分流
func OnZkNotify(c *zkeeper.ZkClient, path string, ev zk.EventType) {
	if ev == zk.EventNodeDataChanged {
		var err error
		switch path {
		case ZkP.Vip:
			_, err = DynConf.ReloadVipConf()
			if err != nil {
				Log.Error("reload vip", zap.Error(err))
			} else {
				Log.Info("reload vip")
			}
		case ZkP.Games:
			_, err = DynConf.ReloadGamesConf()
			if err != nil {
				Log.Error("reload games", zap.Error(err))
			} else {
				Log.Info("reload games")
			}
		case ZkP.Channels:
			_, err = DynConf.ReloadChannelsConf()
			if err != nil {
				Log.Error("reload channels", zap.Error(err))
			} else {
				Log.Info("reload channels")
			}
		case ZkP.ClientAddrs:
			err = DynConf.ReloadClientAddrs()
			if err != nil {
				Log.Error("reload client addrs", zap.Error(err))
			} else {
				Log.Info("reload client addrs")
			}
		case ZkP.Kv:
			err = DynConf.ReloadKvConf()
			if err != nil {
				Log.Error("reload kv", zap.Error(err))
			} else {
				Log.Info("reload kv")
			}
		case ZkP.Wallet:
			err = DynConf.ReloadWalletConf()
			if err != nil {
				Log.Error("reload wallet", zap.Error(err))
			} else {
				Log.Info("reload wallet")
			}
		case ZkP.Checkin7:
			err := DynConf.ReloadCheckin7()
			if err != nil {
				Log.Error("reload checkin7", zap.Error(err))
			} else {
				Log.Info("reload checkin7")
			}
		case ZkP.GameTask:
			_, err := DynConf.ReloadGameTasksConf()
			if err != nil {
				Log.Error("reload gameTask", zap.Error(err))
			} else {
				Log.Info("reload gameTask")
			}
		case ZkP.Turntable:
			_, err := DynConf.ReloadTurntableConf()
			if err != nil {
				Log.Error("reload turntable", zap.Error(err))
			} else {
				Log.Info("reload turntable")
			}
		}
	} else if ev == zk.EventNodeChildrenChanged {
		if path == ZkP.Lobbies {
			before := len(DynConf.Lobbies)
			_, err := DynConf.ReloadLobbiesConf()
			if err != nil {
				Log.Error("reload lobbies", zap.Error(err))
				return
			}
			after := len(DynConf.Lobbies)
			Log.Info("lobbies changed", zap.Int("before", before), zap.Int("after", after))
		}
	}

	if cb, ok := MapPathCb[path]; ok {
		cb(c, path, ev)
	}
}
