package memory

import (
	"bytes"
	"context"

	"github.com/google/gapid/core/data/endian"
	"github.com/google/gapid/core/data/id"
	"github.com/google/gapid/core/os/device"
	"github.com/google/gapid/gapis/database"
)

// Store encodes and stores the value v to the database d, returning the
// memory range and new resource identifier. Data can be used to as a helper
// to AddRead and AddWrite methods on commands.
func Store(ctx context.Context, l *device.MemoryLayout, at Pointer, v ...interface{}) (Range, id.ID) {
	buf := &bytes.Buffer{}
	e := NewEncoder(endian.Writer(buf, l.GetEndian()), l)
	Write(e, v)
	id, err := database.Store(ctx, buf.Bytes())
	if err != nil {
		panic(err)
	}
	return Range{Base: at.Address(), Size: uint64(len(buf.Bytes()))}, id
}
