package rest

import (
	"git.nspix.com/golang/micro/gateway/http"
	"gorm.io/gorm"
)

const (
	PositionBeforeQuery     = "BeforeQuery"
	PositionEachColumnQuery = "EachColumnQuery"
	PositionAfterQuery      = "AfterQuery"
	PositionBeforeCreate    = "BeforeCreate"
	PositionAfterCreate     = "AfterCreate"
	PositionBeforeUpdate    = "BeforeUpdate"
	PositionAfterUpdate     = "AfterUpdate"
	PositionBeforeDelete    = "BeforeDelete"
	PositionAfterDelete     = "AfterDelete"
)

type (
	//查询
	BeforeQueryFunc     func(ctx *http.Context, query *Query) (err error)
	EachColumnQueryFunc func(ctx *http.Context, query *Query, schema *Schema) (err error)
	AfterQueryFunc      func(ctx *http.Context, query *Query) (err error)

	//创建的回调函数
	BeforeCreateFunc func(ctx *http.Context, tx *gorm.DB, model interface{}) (err error)
	AfterCreateFunc  func(ctx *http.Context, tx *gorm.DB, model interface{}, attrs []*DiffAttr) (err error)

	//更新的回调函数
	BeforeUpdateFunc func(ctx *http.Context, tx *gorm.DB, model interface{}) (err error)
	AfterUpdateFunc  func(ctx *http.Context, tx *gorm.DB, model interface{}, attrs []*DiffAttr) (err error)
	//函数的回调函数
	BeforeDeleteFunc func(ctx *http.Context, tx *gorm.DB, model interface{}) (err error)
	AfterDeleteFunc  func(ctx *http.Context, tx *gorm.DB, model interface{}) (err error)

	Callback struct {
		BeforeQueries     []BeforeQueryFunc
		EachColumnQueries []EachColumnQueryFunc
		AfterQueries      []AfterQueryFunc
		BeforeCreates     []BeforeCreateFunc
		AfterCreates      []AfterCreateFunc
		BeforeUpdates     []BeforeUpdateFunc
		AfterUpdates      []AfterUpdateFunc
		BeforeDeletes     []BeforeDeleteFunc
		AfterDeletes      []AfterDeleteFunc
	}
)

func newCallback() *Callback {
	return &Callback{
		BeforeQueries:     make([]BeforeQueryFunc, 0),
		EachColumnQueries: make([]EachColumnQueryFunc, 0),
		AfterQueries:      make([]AfterQueryFunc, 0),
		BeforeCreates:     make([]BeforeCreateFunc, 0),
		AfterCreates:      make([]AfterCreateFunc, 0),
		BeforeUpdates:     make([]BeforeUpdateFunc, 0),
		AfterUpdates:      make([]AfterUpdateFunc, 0),
		BeforeDeletes:     make([]BeforeDeleteFunc, 0),
		AfterDeletes:      make([]AfterDeleteFunc, 0),
	}
}

func (c *Callback) RegisterBeforeQuery(cb BeforeQueryFunc) {
	c.BeforeQueries = append(c.BeforeQueries, cb)
}

func (c *Callback) RegisterEachColumnQuery(cb EachColumnQueryFunc) {
	c.EachColumnQueries = append(c.EachColumnQueries, cb)
}

func (c *Callback) RegisterAfterQuery(cb AfterQueryFunc) {
	c.AfterQueries = append(c.AfterQueries, cb)
}

func (c *Callback) RegisterBeforeCreate(cb BeforeCreateFunc) {
	c.BeforeCreates = append(c.BeforeCreates, cb)
}

func (c *Callback) RegisterAfterCreate(cb AfterCreateFunc) {
	c.AfterCreates = append(c.AfterCreates, cb)
}

func (c *Callback) RegisterBeforeUpdate(cb BeforeUpdateFunc) {
	c.BeforeUpdates = append(c.BeforeUpdates, cb)
}
func (c *Callback) RegisterAfterUpdate(cb AfterUpdateFunc) {
	c.AfterUpdates = append(c.AfterUpdates, cb)
}
func (c *Callback) RegisterBeforeDelete(cb BeforeDeleteFunc) {
	c.BeforeDeletes = append(c.BeforeDeletes, cb)
}
func (c *Callback) RegisterAfterDelete(cb AfterDeleteFunc) {
	c.AfterDeletes = append(c.AfterDeletes, cb)
}
