import (
	"context"

	"github.com/pip-services3-go/pip-services3-commons-go/refer"
	"github.com/pip-services3-gox/pip-services3-commons-gox/config"
)

type MyCustomPersistence struct {
	// Custom implementation using any persistence framework
}

func NewMyCustomPersistence() *MyCustomPersistence {
	return &MyCustomPersistence{}
}

type MyCustomPersistenceWrapper struct {
	config      *config.ConfigParams
	persistence *MyCustomPersistence
}

func NewMyCustomPersistenceWrapper() *MyCustomPersistenceWrapper {
	return &MyCustomPersistenceWrapper{
		config: config.NewConfigParams(),
	}
}

func (c *MyCustomPersistenceWrapper) Configure(ctx context.Context, config *config.ConfigParams) {
	// Store config parameters
	if config != nil {
		c.config = config
	}
}

func (c *MyCustomPersistenceWrapper) SetReferences(ctx context.Context, references refer.IReferences) {
	// Retrieve whatever references you may need
}

func (c *MyCustomPersistenceWrapper) IsOpen() bool {
	return c.persistence != nil
}

func (c *MyCustomPersistenceWrapper) Open(ctx context.Context, correlationId string) (err error) {
	if c.persistence != nil {
		return nil
	}

	// Create custom persistence
	c.persistence = NewMyCustomPersistence()

	// Configure custom persistence
	// ...

	// Open and connect to the database
	c.persistence.Connect()
}

func (c *MyCustomPersistenceWrapper) Close(ctx context.Context, correlationId string) (err error) {
	if c.persistence == nil {
		return
	}

	// Disconnect from the database and close
	c.persistence.Disconnect()
	c.persistence = nil
}