package datagen

import (
	"math"
	"strconv"
	"sync/atomic"

	"github.com/pingcap/tidb/parser/ast"
)

type uniqueValueGen interface {
	genUniqueV(*ast.ColumnDef) string
}

// auto increment 1 and generate from start if exceed the max value of column
func newEtUniqueUint32Gen() *etUniqueUint32Gen {
	return &etUniqueUint32Gen{
		v: 0,
	}
}

type etUniqueUint32Gen struct {
	v uint32
}

func (e *etUniqueUint32Gen) genUniqueV(g *ast.ColumnDef) string {
	return strconv.FormatUint(uint64(atomic.AddUint32(&e.v, 1)), 10)
}

func newEtUniqueInt32Gen() *etUniqueInt32Gen {
	return &etUniqueInt32Gen{
		v: math.MinInt32,
	}
}

type etUniqueInt32Gen struct {
	v int32
}

func (e *etUniqueInt32Gen) genUniqueV(g *ast.ColumnDef) string {
	return strconv.FormatInt(int64(atomic.AddInt32(&e.v, 1)), 10)
}

func newEtUniqueUint64Gen() *etUniqueUint64Gen {
	return &etUniqueUint64Gen{
		v: 0,
	}
}

type etUniqueUint64Gen struct {
	v uint64
}

func (e *etUniqueUint64Gen) genUniqueV(g *ast.ColumnDef) string {
	return strconv.FormatUint(atomic.AddUint64(&e.v, 1), 10)
}

func newEtUniqueInt64Gen() *etUniqueInt64Gen {
	return &etUniqueInt64Gen{
		v: math.MinInt64,
	}
}

type etUniqueInt64Gen struct {
	v int64
}

func (e *etUniqueInt64Gen) genUniqueV(g *ast.ColumnDef) string {
	return strconv.FormatInt(atomic.AddInt64(&e.v, 1), 10)
}
