package tester

import (
	"sync"

	"github.com/fiskerinc/cloud-services/pkg/redis"
)

func NewMockClientPool(args ...interface{}) redis.ClientPoolInterface {
	result := &MockClientPool{}

	for i := range args {
		if pool, ok := args[i].(redis.Pool); ok {
			result.pool = pool
		} else if client, ok := args[i].(redis.Client); ok {
			result.client = client
		}
	}

	return result
}

type MockClientPool struct {
	once     sync.Once
	oncePool sync.Once
	client   redis.Client
	pool     redis.Pool
}

func (f *MockClientPool) getClient() redis.Client {
	f.once.Do(func() {
		if f.client == nil {
			f.client = NewRedisMock()
		}
	})

	return f.client
}

func (f *MockClientPool) GetPool() redis.Pool {
	f.oncePool.Do(func() {
		if f.pool == nil {
			f.pool = redis.GetMockPool()
		}
	})

	return f.pool
}

func (f *MockClientPool) SetPool(pool redis.Pool) {
	f.pool = pool
}

func (f *MockClientPool) GetFromPool() redis.Client {
	return f.getClient()
}
