package sarama

// DeleteAclsRequest is a delete acl request
type DeleteAclsRequest struct {
	Version int
	Filters []*AclFilter
}

func (d *DeleteAclsRequest) setVersion(v int16) {
	d.Version = int(v)
}

func (d *DeleteAclsRequest) encode(pe packetEncoder) error {
	if err := pe.putArrayLength(len(d.Filters)); err != nil {
		return err
	}

	for _, filter := range d.Filters {
		filter.Version = d.Version
		if err := filter.encode(pe); err != nil {
			return err
		}
	}

	pe.putEmptyTaggedFieldArray()
	return nil
}

func (d *DeleteAclsRequest) decode(pd packetDecoder, version int16) (err error) {
	d.Version = int(version)
	n, err := pd.getArrayLength()
	if err != nil {
		return err
	}

	d.Filters = make([]*AclFilter, n)
	for i := 0; i < n; i++ {
		d.Filters[i] = new(AclFilter)
		d.Filters[i].Version = int(version)
		if err := d.Filters[i].decode(pd, version); err != nil {
			return err
		}
	}

	_, err = pd.getEmptyTaggedFieldArray()
	return err
}

func (d *DeleteAclsRequest) key() int16 {
	return apiKeyDeleteAcls
}

func (d *DeleteAclsRequest) version() int16 {
	return int16(d.Version)
}

func (d *DeleteAclsRequest) headerVersion() int16 {
	if d.Version >= 2 {
		return 2
	}
	return 1
}

func (d *DeleteAclsRequest) isValidVersion() bool {
	return d.Version >= 0 && d.Version <= 2
}

func (d *DeleteAclsRequest) isFlexible() bool {
	return d.isFlexibleVersion(int16(d.Version))
}

func (d *DeleteAclsRequest) isFlexibleVersion(version int16) bool {
	return version >= 2
}

func (d *DeleteAclsRequest) requiredVersion() KafkaVersion {
	switch d.Version {
	case 2:
		return V2_5_0_0
	case 1:
		return V2_0_0_0
	default:
		return V0_11_0_0
	}
}
