package ii

import (
	"git.simanc.com/software/golib/v4/features/mo"
)

func (c *ItemInfo) Convert(data mo.M, k string) (any, error) {
	field, ok := c.Field(k)
	if !ok {
		return nil, errUnknownFieldCall(c.Name, k)
	}
	v, ok := data[k]
	if !ok {
		return nil, errCovertReturn(&field, nil)
	}
	return field.Convert(v)
}

func (c *ItemInfo) ConvertDouble(data mo.M, k string) (float64, error) {
	field, ok := c.Field(k)
	if !ok {
		return 0, errUnknownFieldCall(c.Name, k)
	}
	v, ok := data[k]
	if !ok {
		return 0, errCovertReturn(&field, nil)
	}
	return field.convertDouble(v)
}

func (c *ItemInfo) ConvertString(data mo.M, k string) (string, error) {
	field, ok := c.Field(k)
	if !ok {
		return "", errUnknownFieldCall(c.Name, k)
	}
	v, ok := data[k]
	if !ok {
		return "", errCovertReturn(&field, nil)
	}
	return field.convertString(v)
}

func (c *ItemInfo) ConvertObject(data mo.M, k string) (mo.D, error) {
	field, ok := c.Field(k)
	if !ok {
		return nil, errUnknownFieldCall(c.Name, k)
	}
	v, ok := data[k]
	if !ok {
		return nil, errCovertReturn(&field, nil)
	}
	return field.convertObject(v)
}

func (c *ItemInfo) CovertObjectWith(data mo.M, k string, val any) error {
	field, ok := c.Field(k)
	if !ok {
		return errUnknownFieldCall(c.Name, k)
	}
	v, ok := data[k]
	if !ok {
		return errCovertReturn(&field, nil)
	}
	return field.convertObjectWith(v, val)
}

func (c *ItemInfo) ConvertArray(data mo.M, k string) (mo.A, error) {
	field, ok := c.Field(k)
	if !ok {
		return nil, errUnknownFieldCall(c.Name, k)
	}
	v, ok := data[k]
	if !ok {
		return nil, errCovertReturn(&field, nil)
	}
	return field.convertArray(v)
}

func (c *ItemInfo) ConvertBinary(data mo.M, k string) (mo.Binary, error) {
	field, ok := c.Field(k)
	if !ok {
		return mo.Binary{}, errUnknownFieldCall(c.Name, k)
	}
	v, ok := data[k]
	if !ok {
		return mo.Binary{}, errCovertReturn(&field, nil)
	}
	return field.convertBinary(v)
}

func (c *ItemInfo) ConvertObjectID(data mo.M, k string) (mo.ObjectID, error) {
	field, ok := c.Field(k)
	if !ok {
		return mo.NilObjectID, errUnknownFieldCall(c.Name, k)
	}
	v, ok := data[k]
	if !ok {
		return mo.NilObjectID, errCovertReturn(&field, nil)
	}
	return field.convertObjectID(v)
}

func (c *ItemInfo) ConvertBoolean(data mo.M, k string) (bool, error) {
	field, ok := c.Field(k)
	if !ok {
		return false, errUnknownFieldCall(c.Name, k)
	}
	v, ok := data[k]
	if !ok {
		return false, errCovertReturn(&field, nil)
	}
	return field.convertBoolean(v)
}

func (c *ItemInfo) ConvertDate(data mo.M, k string) (mo.DateTime, error) {
	field, ok := c.Field(k)
	if !ok {
		return 0, errUnknownFieldCall(c.Name, k)
	}
	v, ok := data[k]
	if !ok {
		return 0, errCovertReturn(&field, nil)
	}
	return field.convertDate(v)
}

func (c *ItemInfo) ConvertInt32(data mo.M, k string) (int32, error) {
	field, ok := c.Field(k)
	if !ok {
		return 0, errUnknownFieldCall(c.Name, k)
	}
	v, ok := data[k]
	if !ok {
		return 0, errCovertReturn(&field, nil)
	}
	return field.convertInt32(v)
}

func (c *ItemInfo) ConvertInt64(data mo.M, k string) (int64, error) {
	field, ok := c.Field(k)
	if !ok {
		return 0, errUnknownFieldCall(c.Name, k)
	}
	v, ok := data[k]
	if !ok {
		return 0, errCovertReturn(&field, nil)
	}
	return field.convertInt64(v)
}
