
var express = require('express');
var app = express();
var cors = require('cors');
var bodyParser = require('body-parser')
var fs = require('fs');
app.use(cors());

app.use(bodyParser.json());

app.get('/comments', function(req, res){
    fs.readFile('comments.txt', function(err, data){
        return res.status(200).send(JSON.parse(data));
    }) 
});
app.post('/addComment', function(req,res){
    var body = req.body;
    fs.readFile('comments.txt', function(err, data){
        let mainComments = JSON.parse(data);
        mainComments.unshift(body);
        fs.writeFile('comments.txt',JSON.stringify(mainComments) ,function(err, data){
            return res.status(200).send(body);
        })
    })  
});
app.post('/commentReply', function(req,res){
    var body = req.body;
    fs.readFile('comments.txt', function(err, data){
        let mainComments = JSON.parse(data);
        for (let i = 0; i < mainComments.length; i++) {
            if (mainComments[i].id === body.mainId) {
                mainComments[i].secondComments.unshift(body);
            }
        }
        fs.writeFile('comments.txt',JSON.stringify(mainComments) ,function(err, data){
            return res.status(200).send(body);
        })
    }) 
});
app.delete('/deleteComment/:id', function(req ,res){
    var itemId = +req.params.id
    fs.readFile('comments.txt', function(err, data){
        let mainComments = JSON.parse(data);
        for (let i = 0; i < mainComments.length; i++) {
            if(mainComments[i].id === itemId){
                mainComments.splice(i, 1);
            }
        }
        fs.writeFile('comments.txt',JSON.stringify(mainComments) ,function(err, data){
            return res.status(200).send(data);
        })
    })  
    
});
app.delete('/deleteSecondComment/:mainId/:secondId', function(req ,res){
    var mainId = +req.params.mainId;
    var secondId = +req.params.secondId;
    fs.readFile('comments.txt', function(err, data){
        let mainComments = JSON.parse(data);
        for (let i = 0; i < mainComments.length; i++) {
            for (let j = 0; j < mainComments[i].secondComments.length; j++) {
                if (mainComments[i].secondComments[j].mainId === mainId) {
                    if (mainComments[i].secondComments[j].id === secondId) {
                        mainComments[i].secondComments.splice(j,1);
                    }
                }
                    
            }
        }
        fs.writeFile('comments.txt',JSON.stringify(mainComments) ,function(err, data){
            return res.status(200).send(data);
        })
    })
});
app.put('/editComment', function(req ,res){
    var body = req.body
    fs.readFile('comments.txt', function(err, data){
        let mainComments = JSON.parse(data);
        for (let i = 0; i < mainComments.length; i++) {
            if (mainComments[i].id === body.id) {
                mainComments.splice(i,1,body);
            }  
        }
        fs.writeFile('comments.txt',JSON.stringify(mainComments) ,function(err, data){
            return res.status(200).send(body);
        })
    })
});
app.put('/editSecondComment', function(req ,res){
    var body = req.body
    fs.readFile('comments.txt', function(err, data){
        let mainComments = JSON.parse(data);
        for (let i = 0; i < mainComments.length; i++) {
            for (let j = 0; j < mainComments[i].secondComments.length; j++) {
                if (mainComments[i].secondComments[j].mainId === body.mainId) {
                    if (mainComments[i].secondComments[j].id === body.id) {
                        mainComments[i].secondComments.splice(j,1,body);
                    }
                }
            }  
        }
        fs.writeFile('comments.txt',JSON.stringify(mainComments) ,function(err, data){
            return res.status(200).send(body);
        })
    }) 
});
app.post('/img', function(req ,res){
    var body = req.body;
    fs.writeFile('img.jpg',JSON.stringify(body), function(err, data){
        res.status(200).send(body);
    })
    
})
app.listen(3001);