//const db = require('../conn')
const config = require('../config')
const pre = config.database.pre
const QueryBuilder = require('node-querybuilder');
const pool = new QueryBuilder(config.settings, 'mysql', 'pool');

/* function exe_query(sqlque) {
    return new Promise(resolve => {
        db.query(sqlque, function (err, result, fields){
            if (err){throw err}else{
              resolve(result);
            }
          })
    });
  }
  
  async function get_user_by_username(username) {
    var sql ="select * from "+pre+"_adm_user where username='" + username +"' ";
    let result = await exe_query(sql);
    return result;
  }

  async function get_homepage_section1() {
    var sql ="select * from "+pre+"_homepage_section1";
    let result = await exe_query(sql);
    return result[0];
  }

*/

  async function get_homepage_section1() {
    try {
        let qb = await pool.get_connection();
        let response = await qb.select('*').get(pre+'_homepage_section1');
        //console.log("Query Ran: " + qb.last_query());
        qb.release();
        return response[0];
    } catch (err) {
        //console.error(err.msg)
        return "Fail";
    }
  }


  async function get_homepage_section2() {
    try {
        let qb = await pool.get_connection();
        let response = await qb.select('*').get(pre+'_homepage_section2');
        qb.release();
        return response;
    } catch (err) {
        return "Fail";
    }
  }

  async function get_homepage_section2_heading() {
    try {
        let qb = await pool.get_connection();
        let response = await qb.select('*').get(pre+'_homepage_section2_heading');
        qb.release();
        return response[0];
    } catch (err) {
        return "Fail";
    }
  }

  async function get_homepage_section3() {
    try {
        let qb = await pool.get_connection();
        let response = await qb.select('*').get(pre+'_homepage_section3');
        qb.release();
        return response;
    } catch (err) {
        return "Fail";
    }
  }

  async function get_homepage_section3_heading() {
    try {
        let qb = await pool.get_connection();
        let response = await qb.select('*').get(pre+'_homepage_section3_title');
        qb.release();
        return response[0];
    } catch (err) {
        return "Fail";
    }
  }


  async function get_homepage_section4() {
    try {
        let qb = await pool.get_connection();
        let response = await qb.select('*').where('deleted', '0').get(pre+'_faq');
        qb.release();
        return response;
    } catch (err) {
        return "Fail";
    }
  }

  async function get_homepage_section4_heading() {
    try {
        let qb = await pool.get_connection();
        let response = await qb.select('*').get(pre+'_homepage_section4_title');
        qb.release();
        return response[0];
    } catch (err) {
        return "Fail";
    }
  }

  async function get_homepage_section5() {
    try {
        let qb = await pool.get_connection();
        let response = await qb.select('*').where('deleted', '0').get(pre+'_homepage_section5');
        qb.release();
        return response;
    } catch (err) {
        return "Fail";
    }
  }

  async function get_homepage_section5_heading() {
    try {
        let qb = await pool.get_connection();
        let response = await qb.select('*').get(pre+'_homepage_section5_title');
        qb.release();
        return response[0];
    } catch (err) {
        return "Fail";
    }
  }

  async function get_homepage_section6() {
    try {
        let qb = await pool.get_connection();
        let response = await qb.select('*').where('deleted', '0').get(pre+'_homepage_section6');
        qb.release();
        return response;
    } catch (err) {
        return "Fail";
    }
  }

  async function get_homepage_section6_heading() {
    try {
        let qb = await pool.get_connection();
        let response = await qb.select('*').get(pre+'_homepage_section6_title');
        qb.release();
        return response[0];
    } catch (err) {
        return "Fail";
    }
  }

  async function get_homepage_section7() {
    try {
        let qb = await pool.get_connection();
        let response = await qb.select('*').where('deleted', '0').get(pre+'_homepage_section7');
        qb.release();
        return response[0];
    } catch (err) {
        return "Fail";
    }
  }

  async function sleep(){
    await wait(10000);
  }

  function wait(time) {
    return new Promise(resolve => {
        setTimeout(() => {
            resolve();
        }, time);
    });
}

module.exports = {
    get_homepage_section1,sleep,get_homepage_section2,get_homepage_section2_heading,get_homepage_section3,get_homepage_section3_heading,get_homepage_section4,get_homepage_section4_heading,get_homepage_section5,get_homepage_section5_heading,get_homepage_section6,get_homepage_section6_heading,get_homepage_section7
}

/* 

INSTRUCTIONS
===================

1. Query Builder Reference : https://www.npmjs.com/package/node-querybuilder#how-to-install

*/