package partner

import (
	"fmt"

	"git.rosy.net.cn/jx-callback/business/model/dao"

	"git.rosy.net.cn/jx-callback/business/jxutils/jxcontext"
	"git.rosy.net.cn/jx-callback/business/model"
)

const (
	PrinterStatusUnknown        = 0
	PrinterStatusOffline        = 1
	PrinterStatusOnlineOK       = 2
	PrinterStatusOnlineAbnormal = 3
)

const (
	PrintResultSuccess   = 0 // 成功
	PrintResultNoPrinter = 1 // 没有配置网络打印机
)

const (
	PrinterFontSizeNormal  = int8(0) //正常大小一份
	PrinterFontSizeBig     = int8(1) //两倍大小一份,大一
	PrinterFontSizeNormal2 = int8(2) //正常字体两份
	PrinterFontSizeBig2    = int8(3) //大字体两份，大二
	PrinterFontSizeMiddle  = int8(4) //中字体1份
	PrinterFontSizeMiddle2 = int8(5) //中字体2份
)

var (
	PrinterStatusName = map[int]string{
		PrinterStatusUnknown:        "未知",
		PrinterStatusOffline:        "离线",
		PrinterStatusOnlineOK:       "正常",
		PrinterStatusOnlineAbnormal: "异常",
	}
)

type PrinterStatus struct {
	PrintResult   int `json:"printResult"`
	PrinterStatus int `json:"printerStatus"`
	Printed       int `json:"printed"` // 已经打印的单数
	Waiting       int `json:"waiting"` // 等待打印的单数，超过1一般不太正常
}

type BindPrinterResult struct {
	PrinterSN   string `json:"printerSN"`
	PrinterKey  string `json:"printerKey"`
	PrinterKey2 string `json:"printerKey2"`
	ExpiresAt   int64  `json:"expiresAt"`
}

type IPrinterHandler interface {
	GetVendorID() int
	PrintMsg(ctx *jxcontext.Context, id1, id2, msgTitle, msgContent string) (printerStatus *PrinterStatus, err error)
	GetPrinterStatus(ctx *jxcontext.Context, id1, id2 string) (printerStatus *PrinterStatus, err error)

	RegisterPrinter(ctx *jxcontext.Context, id1, id2, printerName string, soreId int64) (newID1, newID2 string, err error)
	UnregisterPrinter(ctx *jxcontext.Context, id1, id2 string) (err error)

	BindPrinter(ctx *jxcontext.Context, mapData map[string]interface{}) (bindResult *BindPrinterResult, err error)
	RebindPrinter(ctx *jxcontext.Context, lastBindResult *BindPrinterResult) (bindResult *BindPrinterResult, err error)

	PrintOrder(ctx *jxcontext.Context, store *model.Store, storeDetail *dao.StoreDetail, order *model.GoodsOrder, printType string, asfOrder *model.AfsOrder) (printerStatus *PrinterStatus, err error)
	PrintStore(ctx *jxcontext.Context, store *model.Store, storeDetail *dao.StoreDetail, vendorId int) (printerStatus *PrinterStatus, err error)
	PrintCancelOrRefundOrder(ctx *jxcontext.Context, printType int, store *model.Store, storeDetail *dao.StoreDetail, order *model.GoodsOrder) (printerStatus *PrinterStatus, err error)

	EmptyPrintList(ctx *jxcontext.Context, id1, id2 string) (err error)
	PlayText(ctx *jxcontext.Context, id1, id2, orderID, text string) (printerStatus *PrinterStatus, err error)
	SetSound(ctx *jxcontext.Context, id1, id2, sound string) (err error)
}

var (
	PrinterPlatformHandlers map[int]IPrinterHandler
)

func init() {
	PrinterPlatformHandlers = make(map[int]IPrinterHandler)
}

func RegisterPrinterPlatform(handler IPrinterHandler) {
	vendorID := handler.GetVendorID()
	if !(model.IsPrinterVendorExist(vendorID)) {
		panic(fmt.Sprintf("printer vendor:%d is illegal", vendorID))
	}
	if _, ok := PrinterPlatformHandlers[vendorID]; ok {
		panic(fmt.Sprintf("printer vendor:%d, already exists", vendorID))
	}
	PrinterPlatformHandlers[vendorID] = handler
}

func GetPrinterPlatformFromVendorID(vendorID int) IPrinterHandler {
	return PrinterPlatformHandlers[vendorID]
}
