package ii

import (
	"encoding/xml"
	"fmt"
)

type Model uint8

const (
	TypeDefault Model = 0 // 原始值
	TypeString  Model = 1 // 解析为字符串
	TypeDate    Model = 2 // 不带小时的日期
	TypeTime    Model = 3 // 带小时的日期
	TypeDouble  Model = 4 // 保留两位小数
	TypeInt64   Model = 5 // 转换为数字
	TypeLookup  Model = 6 // 关联转换
)

var nameType = map[Model]string{
	0: "default",
	1: "string",
	2: "date",
	3: "time",
	4: "double",
	5: "int64",
	6: "lookup",
}

var typeName = map[string]Model{
	"default": 0,
	"string":  1,
	"date":    2,
	"time":    3,
	"double":  4,
	"int64":   5,
	"lookup":  6,
}

func (c *Model) UnmarshalXMLAttr(attr xml.Attr) error {
	if t, ok := typeName[attr.Value]; ok {
		*c = t
		return nil
	}
	return fmt.Errorf("unknown type: %s", attr.Value)
}

func (c Model) String() string {
	return fmt.Sprintf("ii.Model(%s)", nameType[c])
}
