var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var lessMiddleware = require('less-middleware');
var logger = require('morgan');
var session = require('express-session');

var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');
var adminpanel = require('./routes/admin_panel');
const importRoute = require('./routes/import');

var app = express();

var bodyParser = require('body-parser');
app.use(bodyParser.json());

//support parsing of application/x-www-form-urlencoded post 
app.use(bodyParser.urlencoded({
    parameterLimit: 100000,
    limit: '200mb',
    extended: true
}));

// view engine setup
//app.set('views', path.join(__dirname, 'views'));
//app.set('view engine', 'jade');
var cons = require('consolidate');
app.engine('html', cons.swig);
app.set('view engine', 'html');

// Add Gujarati digit filter
const swig = require('swig');
swig.setFilter('gujarati', function (input) {
    const gujaratiDigits = ['૦', '૧', '૨', '૩', '૪', '૫', '૬', '૭', '૮', '૯'];
    return String(input).replace(/\d/g, d => gujaratiDigits[+d]);
});

//load all static files. like,images,css,jquery
app.use('/public', express.static('public'));

//session manage
//app.use(session({secret: 'ssshhhhh'}));
app.use(session({secret: 'ssshhhhh', saveUninitialized: true, resave: false}));

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({extended: false}));
app.use(cookieParser());
app.use(lessMiddleware(path.join(__dirname, 'public')));
app.use(express.static(path.join(__dirname, 'public')));

var connection = require('express-myconnection');
var mysql = require('mysql');
app.use(
        connection(mysql, {
            host: 'localhost',
            user: 'root',
            password: '',
            port: 3306, //port mysql
            database: 'rwkwmulp1_adimjuth_survey_data',
            connectionLimit: 100,
            dateStrings: true
        }, 'pool') //or single
        );
//
app.connect(function (err) {
    if (err)
        throw err;
    console.log("Connected!");
});

app.use('/api', importRoute);
//app.get('/import_excel_file', function (req, res) {
//  res.render('import');
//});

app.use('/', indexRouter);
app.use('/users', usersRouter);

app.use('/adminpanel/check_login', adminpanel.check_login);
app.use('/dashboard', adminpanel);
app.use('/adminpanel/select_taluka', adminpanel.select_taluka);
app.use('/adminpanel/select_village', adminpanel.select_village);
app.use('/adminpanel/view_village_summary', adminpanel.view_village_summary);

app.use('/project_progress/set_session_user_language', adminpanel.set_session_user_language);

app.use('/view_village_summary_detail', adminpanel.view_village_summary_detail);

app.use('/import_excel_file', adminpanel.import_excel_file);

// catch 404 and forward to error handler
app.use(function (req, res, next) {
    next(createError(404));
});

// error handler
app.use(function (err, req, res, next) {
    // set locals, only providing error in development
    res.locals.message = err.message;
    res.locals.error = req.app.get('env') === 'development' ? err : {};

    // render the error page
    res.status(err.status || 500);
    res.render('error');
});

module.exports = app;
