// Copyright 2017 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
	"fmt"

	"github.com/siddontang/go-mysql/mysql"
)

type opType byte

const (
	insert opType = iota + 1
	update
	del
	ddl
	xid
	flush
)

func (t opType) String() string {
	switch t {
	case insert:
		return "insert"
	case update:
		return "update"
	case del:
		return "delete"
	case ddl:
		return "ddl"
	case xid:
		return "xid"
	case flush:
		return "flush"
	}

	return ""
}

type job struct {
	tp         opType
	sql        string
	args       []interface{}
	key        string
	retry      bool
	pos        mysql.Position
	gtidSet    GTIDSet
	annotation string

	// is the last job in an binlog event, only used for ddl now
	isLast bool
}

func newJob(tp opType, sql string, args []interface{}, key string, retry bool, pos mysql.Position, gtidSet GTIDSet, annotation string, isLast bool) *job {
	var gs GTIDSet
	if gtidSet != nil {
		gs = gtidSet.clone()
	}
	return &job{tp: tp, sql: sql, args: args, key: key, retry: retry, pos: pos, gtidSet: gs, annotation: annotation, isLast: isLast}
}

func newXIDJob(pos mysql.Position, gtidSet GTIDSet) *job {
	var gs GTIDSet
	if gtidSet != nil {
		gs = gtidSet.clone()
	}
	return &job{tp: xid, pos: pos, gtidSet: gs, isLast: true}
}

func newFlushJob() *job {
	return &job{tp: flush, isLast: true}
}

// put queues into bucket to monitor them
func queueBucketName(queueID int) string {
	return fmt.Sprintf("bucket_%d", queueID%defaultBucketQueueCount)
}
