/*
 * Stairs
 * Copyright 2017 (c) Mark van Renswoude
 *
 * https://git.x2software.net/pub/Stairs
*/
const express = require('express');
const bodyParser = require('body-parser');

const app = express();

app.use(bodyParser.json());
app.use(express.static('web'));
app.use(express.static('web/dist'));

app.get('/api/status', function(req, res)
{
  res.send({
    systemID: 'dev-server',
    version: 'dev-server',
    time: 1518467160,
    timeOffset: 3600,
    resetReason: 2,
    stackTrace: true
  });
});

app.get('/api/connection', function(req, res)
{
  res.send({
    hostname: 'dev-server',
    accesspoint: true,
    station: true,
    ssid: 'MyWiFiSSID',
    password: 'supersecret',
    dhcp: true,
    ip: '192.168.1.234',
    subnetmask: '255.255.255.0',
    gateway: '192.168.1.0'
  });
});

app.get('/api/connection/status', function(req, res)
{
  res.send({
    "ap": {
      "enabled": true,
      "ip": "192.168.4.1"
    },
    "station": {
      "enabled": true,
      "status": 1,
      "ip": "0.0.0.0"
    }
  });
});

app.post('/api/connection', function(req, res)
{
  res.sendStatus(200);
});

app.post('/api/firmware', function(req, res)
{
  res.sendStatus(200);
});


var system = {
  ntpServer: "eu.pool.ntp.org",
  ntpInterval: 300,
  lat: 52.370216,
  lng: 4.895168,
  pins: {
    ledAP: 4,
    ledSTA: 5,
    apButton: 2,
    pwmSDA: 13,
    pwmSCL: 12
  },
  pwmAddress: 64,
  pwmFrequency: 1600,
  mapsAPIKey: ""
};

app.get('/api/system', function(req, res)
{
  res.send(system);
});

app.post('/api/system', function(req, res)
{
  var body = req.body;
  if (body)
  {
    system.lat = body.lat || system.lat;
    system.lng = body.lng || system.lng;

    system.pins.ledAP = body.pins.ledAP || system.pins.ledAP;
    system.pins.ledSTA = body.pins.ledSTA || system.pins.ledSTA;
    system.pins.apButton = body.pins.apButton || system.pins.apButton;
    system.pins.pwmSDA = body.pins.pwmSDA || system.pins.pwmSDA;
    system.pins.pwmSCL = body.pins.pwmSCL || system.pins.pwmSCL;

    system.pwmAddress = body.pwmAddress || system.pwmAddress;
    system.pwmFrequency = body.pwmFrequency || system.pwmFrequency;
    system.mapsAPIKey = body.mapsAPIKey || system.mapsAPIKey;

    res.sendStatus(200);
  }
  else
    res.sendStatus(400);
});


var steps = {
  count: 14,
  useCurve: true,
  ranges: [
    { start: 0, end: 4095 },
    { start: 0, end: 4095 },
    { start: 0, end: 4095 },
    { start: 0, end: 4095 },
    { start: 0, end: 4095 },
    { start: 0, end: 4095 },
    { start: 0, end: 4095 },
    { start: 0, end: 4095 },
    { start: 0, end: 4095 },
    { start: 0, end: 4095 },
    { start: 0, end: 4095 },
    { start: 0, end: 4095 },
    { start: 0, end: 4095 },
    { start: 0, end: 4095 }
  ]
};



app.get('/api/steps', function(req, res)
{
  res.send(steps);
});

app.post('/api/steps', function(req, res)
{
  steps = req.body;
  res.sendStatus(200);
});




var stepsValues = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];

app.get('/api/steps/values', function(req, res)
{
  res.send(stepsValues);
});

app.post('/api/steps/values', function(req, res)
{
  var body = req.body;
  if (body && body.hasOwnProperty('values'))
  {
    for (var i = 0; i < Math.min(stepsValues.length, body.values.length); i++)
      stepsValues[i] = parseInt(body.values[i], 10) || 0;

    res.sendStatus(200);
  }
  else
    res.sendStatus(400);
});


var timeTriggers = {
  "enabled": true,
  "transitionTime": 1000,
  "triggers": [
    {
       "time": 480,
       "daysOfWeek": 127,
       "brightness": 0,
       "triggerType": 0,
       "enabled": true
    },
    {
       "time": 1200,
       "daysOfWeek": 127,
       "brightness": 255,
       "triggerType": 0,
       "enabled": true
    }
  ]
};

app.get('/api/triggers/time', function(req, res)
{
  res.send(timeTriggers);
});

app.post('/api/triggers/time', function(req, res)
{
  res.sendStatus(200);
});



var motionTriggers = {
  enabled: true,
  enabledDuringTimeTrigger: 0,
  enabledDuringDay: true,
  transitionTime: 1000,
  delay: 30000,
  triggers: [
    {
       pin: 14,
       brightness: 64,
       direction: 2,
       enabled: true
    },
    {
       pin: 15,
       brightness: 64,
       direction: 3,
       enabled: true
    }
  ]
};

app.get('/api/triggers/motion', function(req, res)
{
  res.send(motionTriggers);
});

app.post('/api/triggers/motion', function(req, res)
{
  res.sendStatus(200);
});


app.get('/api/stacktrace/get', function(req, res)
{
  res.send("Nothing to see here, move along!");
});

app.get('/api/stacktrace/delete', function(req, res)
{
  res.sendStatus(200);
});


app.listen(3000, function()
{
  console.log('Development server listening on port 3000')
  console.log('Press Ctrl-C to stop')
});