const queryBuilder = require('./QueryBuilder')
let parameters = require('./Parameters')
let _table = ''

/**
 * [addMoreConditions description]
 * @param {Object} conditions [description]
 * @param {String} andOr      [If before has another where clause then is possible to choose AND or OR]
 */
function addMoreConditions (conditions, andOr) {
  andOr = andOr.toLowerCase()
  if (andOr !== 'and' && andOr !== 'or') {
    throw new Error('The andOr variable is required to be AND or OR!')
  }
  let method = `${andOr}Where`
  for (let key in conditions) {
    if (conditions.hasOwnProperty(key)) {
      queryBuilder[method](key, '=', conditions[key])
    }
  }
}

/**
 * [setModel description]
 * @param {String} table [Database table for that schema]
 */
module.exports.setTable = function (table) {
  if (typeof table === 'string') {
    _table = table
    return this
  } else {
    throw new Error('The table is not set!')
  }
}

/**
 * [Add new record on database]
 * @param  {Object} params [All columns with their values]
 */
module.exports.add = (params) => {
  if (_table === '') {
    throw new Error('The table is not set!')
  }
  if (typeof params !== 'object') {
    throw new Error('The params is need to be object with key column and value value of column!')
  }

  queryBuilder.table(_table).add(Object.keys(params))
  parameters.prepare()
  parameters.params = Object.valueOf(params)
}

/**
 * [Make query for can take the first record from table]
 * @param  {String} [columns='*'] [All columns which You want to get from database]
 */
module.exports.getFirst = (columns = '*') => {
  if (_table === '') {
    throw new Error('The table is not set!')
  }
  if (!columns) {
    throw new Error('The columns is not set!')
  }
  queryBuilder.from(_table)
    .select(columns)
    .take(1)
  parameters.prepare()
}

/**
 * [Make query for can take the last record from table]
 * @param  {String} [columns='*'] [All columns which You want to get from database]
 */
module.exports.getLast = (columns = '*') => {
  if (_table === '') {
    throw new Error('The table is not set!')
  }
  if (!columns) {
    throw new Error('The columns is not set!')
  }
  queryBuilder.from(_table)
    .select(columns)
    .orderBy('id', true)
    .take(1)
  parameters.prepare()
}

/**
 * [Make query for can take all records from table]
 * @param  {String} [columns='*'] [All columns which You want to get from database]
 */
module.exports.getAll = (columns = '*') => {
  if (_table === '') {
    throw new Error('The table is not set!')
  }
  if (!columns) {
    throw new Error('The columns is not set!')
  }
  queryBuilder.from(_table).select(columns)
  parameters.prepare()
}

/**
 * [Make query for can find record by id on table]
 * @param  {String} id            [The Id of the record]
 * @param  {String} [columns='*'] [All columns which You want to get from database]
 */
module.exports.findById = (id, columns = '*') => {
  if (_table === '') {
    throw new Error('The table is not set!')
  }
  if (!columns) {
    throw new Error('The columns is not set!')
  }
  if (!id) {
    throw new Error('The id is not set!')
  }

  queryBuilder.from(_table)
    .select(columns)
    .where('id', '=', id)
  parameters.prepare()
}

/**
 * [Find records on database by many search conditions or (only one)]
 * @param  {Object} conditions       [Columns on database. For example { id: '5'} means find element with id 5]
 * @param {String} columns           [All columns which You want to get from database]
 * @param  {String} [andOr='AND']    [Choose do You want every condition to be true or only one is enough]
 */
module.exports.findByFields = (conditions, columns = '*', andOr = 'AND') => {
  if (_table === '') {
    throw new Error('The table is not set!')
  }
  if (!columns) {
    throw new Error('The columns is not set!')
  }
  if (!conditions) {
    throw new Error('The id is not set!')
  }

  let firstKey = Object.keys(conditions)[0]
  queryBuilder.from(_table)
    .select(columns).where(firstKey, '=', conditions[firstKey])

  delete conditions[firstKey]
  if (conditions) {
    addMoreConditions(conditions, andOr)
  }

  parameters.prepare()
}

@@ -106,23 +106,25 @@
 /**%0A * %5B
-a
+A
 dd
+ 
 More
+ 
 Conditio
@@ -126,27 +126,32 @@
 ditions 
-description
+to the SQL Query
 %5D%0A * @pa
@@ -833,16 +833,32 @@
 'string'
+ && table !== ''
 ) %7B%0A    
@@ -1750,32 +1750,33 @@
  not set!')%0A  %7D%0A
+%0A
   queryBuilder.f
@@ -1807,21 +1807,16 @@
 columns)
-%0A    
 .take(1)
@@ -1808,32 +1808,33 @@
 olumns).take(1)%0A
+%0A
   parameters.pre
@@ -2170,32 +2170,33 @@
  not set!')%0A  %7D%0A
+%0A
   queryBuilder.f
@@ -2266,16 +2266,17 @@
 take(1)%0A
+%0A
   parame
@@ -2623,16 +2623,17 @@
 !')%0A  %7D%0A
+%0A
   queryB
@@ -3982,32 +3982,32 @@
 er.from(_table)%0A
-
     .select(colu
@@ -4010,16 +4010,21 @@
 columns)
+%0A    
 .where(f

Make more clear code