'use strict'

// SERVER SETUP
var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io').listen(server);

// SERVER LISTENING
var serverPort = process.env.PORT || 3000;
server.listen(serverPort, function() {
    console.log("Server up at port " + serverPort);
});


// CLIENT TO SERVER FILE TRANSFER
var ss = require('socket.io-stream');
var path = require('path');
var fs = require('fs-extra');

// RENDER STATIC FILES
app.use(express.static('public'));


// ROUTES
app.get('/', function(req, res) {
    res.sendFile(__dirname + '/public/views/index.html');
});

app.get('/buyer-chat', function(req, res) {
    res.sendFile(__dirname + '/public/views/buyer-chat.html')
});

app.get('/seller-home', function(req, res) {
    res.send('seller home');
});

// SOCKET
io.on('connection', function(socket) {
    var cartTotal = 0;

    // CREATE CONNECTION TO CLIENT
    console.log('new user w/ secure connection... ');
    socket.on('disconnect', function() {
        console.log('user disconnected');
    });

    // SENDING WELCOME MESSAGE TO CLIENT
    socket.on('buyer-chat-active', function() {
        socket.emit('welcome-message');
    });

    // CLIENT UI INPUT HANDLER
    socket.on('nav', function(data) {
        switch (data.ui) {
            case "learn":
                socket.emit('message', {
                    server: 'This app helps you buy right now! Tap the button below to take a photo of the name card by the product. We will take care of the rest.'
                });
                break;
            case "camera":
                // FILE UPLOAD FROM CLIENT
                ss(socket).on('file-upload', function(stream, data) {
                    var filename = data.name
                    stream.pipe(fs.createWriteStream(filename));
                    cartTotal += 1;
                    if (cartTotal < 2) {
                        socket.emit('message', {
                            server: 'Image recieved! Your cart has one item.'
                        });
                    } else {
                        socket.emit('message', {
                            server: 'Image recieved! Your cart has ' + cartTotal + ' items.'
                        });
                    }
                });
                break;
            case "account":
                socket.emit('message', {
                    server: 'We can handle that later, go ahead and take a picture!'
                });
                break;
            default:

        }

    });


});

@@ -1894,266 +1894,33 @@
     
-if (cartTotal %3C 2) %7B%0A                        socket.emit('message', %7B%0A                            server: 'Image recieved! Your cart has one item.'%0A                        %7D);%0A                    %7D else %7B%0A                        socket.emit('message', %7B%0A    
+socket.emit('message', %7B%0A
     
@@ -2000,17 +2000,19 @@
 + ' item
-s
+(s)
 .'%0A     
@@ -2030,38 +2030,12 @@
     
-  
-  
 %7D);%0A
-                    %7D%0A
     

modify logic for showing cart total