package redis

import (
	"sync"
)

func NewClientPool(args ...Pool) ClientPoolInterface {
	result := &ClientPool{}

	if len(args) > 0 {
		result.pool = args[0]
	}

	return result
}

type ClientPoolInterface interface {
	GetPool() Pool
	SetPool(pool Pool)
	GetFromPool() Client
}

type ClientPool struct {
	oncePool sync.Once
	pool     Pool
}

func (f *ClientPool) GetPool() Pool {
	f.oncePool.Do(func() {
		if f.pool == nil {
			f.pool = NewPool()
		}
	})

	return f.pool
}

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

func (f *ClientPool) GetFromPool() Client {
	return NewClient(f.GetPool().Get())
}
