// dbif.go
package dbif

import (
	"pool/internal/db"
	"pool/internal/gbt/coin"
	"time"
)

func NotifyBlkDetailSuccess(gbt *coin.GbtContext, height int64, hash string, nonce string, subidx int64) {
	var msg db.BlkDetail_db_msg
	msg.Id = 0
	msg.Msg = "blk_detail"
	msg.Date = time.Now()
	msg.MinerType = gbt.Coin
	msg.Height = height
	msg.Hash = hash
	msg.Success = 1
	msg.Nonce = nonce
	msg.SubIdx = subidx

	//gbt.DbCtx.BlkDetail_ch <- msg
	db.Save_blk_detail(gbt.DbCtx, &msg)
}

func NotifyAlphBlkDetailSuccess(gbt *coin.GbtContext, fromGroup uint32, toGroup uint32, height int64, hash string, nonce string, subidx int64) {
	var msg db.BlkDetail_db_msg
	msg.Id = 0
	msg.Msg = "blk_detail"
	msg.Date = time.Now()
	msg.MinerType = gbt.Coin
	msg.FromGroup = fromGroup
	msg.ToGroup = toGroup
	msg.Height = height
	msg.Hash = hash
	msg.Success = 1
	msg.Nonce = nonce
	msg.SubIdx = subidx

	//gbt.DbCtx.BlkDetail_ch <- msg
	db.Save_blk_detail(gbt.DbCtx, &msg)
}

/*func NotifyMinerSuccess(gbt *coin.GbtContext, user string, miner string, minerid string, height int64, hash string, nonce string, subidx int64, reward float64, fee float64) {
	var msg db.Miner_db_msg
	msg.Id = 0
	msg.Msg = "miner"
	msg.Date = time.Now()
	msg.MinerType = gbt.Coin

	msg.User = user
	msg.Miner = miner
	msg.Index = minerid

	msg.Height = height
	msg.Hash = hash
	msg.Success = 1
	msg.Nonce = nonce
	msg.SubIdx = subidx

	msg.Reward = reward
	msg.Fee = fee

	gbt.DbCtx.Miner_ch <- msg
}*/

/*func NotifyUsersBlkStatsSuccess(gbt *coin.GbtContext, user string, height int64, hash string, nonce string, subidx int64, reward float64, fee float64) {
	var msg db.UsersBlkStats_db_msg
	msg.Id = 0
	msg.Msg = "users_blkstats"
	msg.Date = time.Now()
	msg.MinerType = gbt.Coin

	msg.User = user

	msg.Height = height
	msg.Hash = hash
	msg.Success = 1
	msg.Nonce = nonce
	msg.SubIdx = subidx

	msg.Reward = reward
	msg.Fee = fee

	gbt.DbCtx.UsersBlkStats_ch <- msg
}*/

func NotifyPoolBlkStatsSuccess(gbt *coin.GbtContext, height int64, hash string, nonce string, subidx int64, reward float64, fee float64) {
	var msg db.PoolBlkStats_db_msg
	msg.Id = 0
	msg.Msg = "pool_blkstats"
	msg.Date = time.Now()
	msg.MinerType = gbt.Coin

	msg.Height = height
	msg.Hash = hash
	msg.Success = 1
	msg.Nonce = nonce
	msg.SubIdx = subidx
	msg.Submit = ""

	msg.Reward = reward
	msg.Fee = fee

	gbt.DbCtx.PoolBlkStats_ch <- msg
}

func AlphNotifyPoolBlkStatsSuccess(gbt *coin.GbtContext, fromGroup uint32, toGroup uint32, height int64, hash string, nonce string, subidx int64, reward float64, fee float64) {
	var msg db.PoolBlkStats_db_msg
	msg.Id = 0
	msg.Msg = "pool_blkstats"
	msg.Date = time.Now()
	msg.MinerType = gbt.Coin
	msg.FromGroup = fromGroup
	msg.ToGroup = toGroup
	msg.Height = height
	msg.Hash = hash
	msg.Success = 1
	msg.Nonce = nonce
	msg.SubIdx = subidx
	msg.Submit = ""

	msg.Reward = reward
	msg.Fee = fee

	gbt.DbCtx.PoolBlkStats_ch <- msg
}

/*func NotifyMinerSubmitResult(gbt *coin.GbtContext, user string, miner string, minerid string, height int64, hash string, result string, nonce string, subidx int64) {
	var msg db.Miner_db_msg
	msg.Id = 0
	msg.Msg = "miner"
	msg.Date = time.Now()
	msg.MinerType = gbt.Coin

	msg.User = user
	msg.Miner = miner
	msg.Index = minerid

	msg.Height = height
	msg.Hash = hash
	msg.Submit = result
	msg.Nonce = nonce
	msg.SubIdx = subidx

	gbt.DbCtx.Miner_ch <- msg
}*/

/*func NotifyUsersBlkStatsSubmitResult(gbt *coin.GbtContext, user string, height int64, hash string, result string, nonce string, subidx int64) {
	var msg db.UsersBlkStats_db_msg
	msg.Id = 0
	msg.Msg = "users_blkstats"
	msg.Date = time.Now()
	msg.MinerType = gbt.Coin

	msg.User = user

	msg.Height = height
	msg.Hash = hash
	msg.Submit = result
	msg.Nonce = nonce
	msg.SubIdx = subidx

	gbt.DbCtx.UsersBlkStats_ch <- msg
}*/

func NotifyPoolBlkStatsSubmitResult(gbt *coin.GbtContext, height int64, hash string, result string, nonce string, subidx int64) {
	var msg db.PoolBlkStats_db_msg
	msg.Id = 0
	msg.Msg = "pool_blkstats"
	msg.Date = time.Now()
	msg.MinerType = gbt.Coin

	msg.Height = height
	msg.Hash = hash
	msg.Submit = result
	msg.Nonce = nonce
	msg.SubIdx = subidx

	gbt.DbCtx.PoolBlkStats_ch <- msg
}

func NotifyAlphPoolBlkStatsSubmitResult(gbt *coin.GbtContext, fromGrouop uint32, toGroup uint32, height int64, hash string, nonce string, subidx int64) {
	var msg db.PoolBlkStats_db_msg
	msg.Id = 0
	msg.Msg = "pool_blkstats"
	msg.Date = time.Now()
	msg.MinerType = gbt.Coin
	msg.FromGroup = fromGrouop
	msg.ToGroup = toGroup
	msg.Height = height
	msg.Hash = hash
	// msg.Submit = result
	msg.Nonce = nonce
	msg.SubIdx = subidx

	gbt.DbCtx.PoolBlkStats_ch <- msg
}

func NotifyBlkNewDb(gbt *coin.GbtContext, height int64, hash string, success bool, nonce string, subidx int64) {
	var msg db.BlkNew_db_msg
	msg.Id = 0
	msg.Msg = "blk_new"
	msg.Date = time.Now()
	msg.MinerType = gbt.Coin
	msg.Height = height
	msg.Hash = hash
	if success {
		msg.Success = 1
	} else {
		msg.Success = 0
	}
	msg.Nonce = nonce
	msg.SubIdx = subidx

	gbt.DbCtx.BlkNew_ch <- msg
}

func NotifyAlphBlkNewDb(gbt *coin.GbtContext, fromGroup uint32, toGroup uint32, height int64, hash string, success bool, nonce string, subidx int64) {
	var msg db.BlkNew_db_msg
	msg.Id = 0
	msg.Msg = "blk_new"
	msg.Date = time.Now()
	msg.MinerType = gbt.Coin
	msg.FromGroup = fromGroup
	msg.ToGroup = toGroup
	msg.Height = height
	msg.Hash = hash
	if success {
		msg.Success = 1
	} else {
		msg.Success = 0
	}
	msg.Nonce = nonce
	msg.SubIdx = subidx

	gbt.DbCtx.BlkNew_ch <- msg
}

/*func NotifyBlockStat(gbt *coin.GbtContext, user string, miner string, minerid string, reward float64, fee float64) {
	var miners_msg db.Miners_db_msg
	miners_msg.Id = 0
	miners_msg.Msg = "miners"
	miners_msg.Date = time.Now()
	miners_msg.MinerType = gbt.Coin

	miners_msg.User = user
	miners_msg.Miner = miner
	miners_msg.Index = minerid

	gbt.DbCtx.Miners_ch <- miners_msg

	var users_msg db.Users_db_msg
	users_msg.Id = 0
	users_msg.Msg = "users"
	users_msg.Date = time.Now()
	users_msg.MinerType = gbt.Coin

	users_msg.User = user

	gbt.DbCtx.Users_ch <- users_msg

	var pool_msg db.Pool_db_msg
	pool_msg.Id = 0
	pool_msg.Msg = "pool"
	pool_msg.Date = time.Now()
	pool_msg.MinerType = gbt.Coin

	pool_msg.Reward = reward
	pool_msg.Fee = fee

	gbt.DbCtx.Pool_ch <- pool_msg
}*/
