/*
 * node-dhcp-manager
 *
 * Copyright 2013-2014 Jason Gerfen
 * All rights reserved.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES LOSS OF USE, DATA, OR PROFITS OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

var dbconn = require('./dbconn')

/**
 * @function store
 * @description Decision make for session store functionality
 *
 * @param {Object} opts Configuration options for timeout etc
 * @param {Object} express The express framework object
 * @param {Object} client Object of extended request attributes
 */
exports.store = function(opts, express, client){
  switch(true){
    case (/Redis/.test(opts.App.sessions)):
      var RedisStore = require('connect-redis-crypto')(express)
        , SessionStore = new RedisStore({
            ttl: opts.App.timeout,
            secret: opts.App.secret
          })
      break
    case (/MySQL/.test(opts.App.sessions)):
      var conn = dbconn.connect('admin')
        , MySQLStore = require('connect-mysql')(express)
        , SessionStore = new MySQLStore({
            client: conn,
            secret: opts.App.secret
          })
      break
    default:
      var NativeStore = require('./dbsessions')
        , SessionStore = new NativeStore(express, opts.Db, client,
                                         opts.App.timeoout)
      break
  }
  return SessionStore
}
