package blockstore

import (
	"context"
	"sync"

	blocks "github.com/ipfs/go-block-format"
	"github.com/ipfs/go-cid"
)

// NewMemorySync returns a thread-safe in-memory blockstore.
func NewMemorySync() *SyncBlockstore {
	return &SyncBlockstore{bs: make(MemBlockstore)}
}

// SyncBlockstore is a terminal blockstore that is a synchronized version
// of MemBlockstore.
type SyncBlockstore struct {
	mu sync.RWMutex
	bs MemBlockstore // specifically use a memStore to save indirection overhead.
}

func (m *SyncBlockstore) DeleteBlock(k cid.Cid) error {
	m.mu.Lock()
	defer m.mu.Unlock()
	return m.bs.DeleteBlock(k)
}

func (m *SyncBlockstore) DeleteMany(ks []cid.Cid) error {
	m.mu.Lock()
	defer m.mu.Unlock()
	return m.bs.DeleteMany(ks)
}

func (m *SyncBlockstore) Has(k cid.Cid) (bool, error) {
	m.mu.RLock()
	defer m.mu.RUnlock()
	return m.bs.Has(k)
}

func (m *SyncBlockstore) View(k cid.Cid, callback func([]byte) error) error {
	m.mu.RLock()
	defer m.mu.RUnlock()

	return m.bs.View(k, callback)
}

func (m *SyncBlockstore) Get(k cid.Cid) (blocks.Block, error) {
	m.mu.RLock()
	defer m.mu.RUnlock()
	return m.bs.Get(k)
}

func (m *SyncBlockstore) GetSize(k cid.Cid) (int, error) {
	m.mu.RLock()
	defer m.mu.RUnlock()
	return m.bs.GetSize(k)
}

func (m *SyncBlockstore) Put(b blocks.Block) error {
	m.mu.Lock()
	defer m.mu.Unlock()
	return m.bs.Put(b)
}

func (m *SyncBlockstore) PutMany(bs []blocks.Block) error {
	m.mu.Lock()
	defer m.mu.Unlock()
	return m.bs.PutMany(bs)
}

func (m *SyncBlockstore) AllKeysChan(ctx context.Context) (<-chan cid.Cid, error) {
	m.mu.RLock()
	defer m.mu.RUnlock()
	// this blockstore implementation doesn't do any async work.
	return m.bs.AllKeysChan(ctx)
}

func (m *SyncBlockstore) HashOnRead(enabled bool) {
	// noop
}