const http = require("http");
const  url = require("url");
const   fs = require("fs");

let contents = "";
let nextID = 101;
let people = [{name: "Homer", id:nextID++, age: 26, photo:"/images/homer.jpg", messages:[]},
			  {name: "Marge", id:nextID++, age: 25, photo:"/images/marge.jpg", messages:[]},
			  {name: "Ling", id:nextID++, age: 27, photo:"/images/ling.jpg", messages:[]},
			  {name: "Ahmed", id:nextID++, age: 24, photo:"/images/ahmed.jpg", messages:[]}];
let anonImg = "/images/unknown_person.jpg";

// Helper function for sending 404 message
function send404(response) {
}

// Helper function for sending back the main homepage
function respondMainPage(res) {
}

// Helper function for sending back the profile page
function respondWithProfile(res, id) {
}

// Send back the specific file requested (html, JavaScript, css, jpg, or plain text)
function serveStaticFile(filename, res) {
}

// Listener for incoming client requests
function handleRequest(req, res) {
	const parsed = url.parse(req.url, true); // get the query params (for profile pages)
	const pathname = parsed.pathname;        // get path without query

	// Handle all GET requests
	if (req.method === "GET") {
		if (pathname === "/" || pathname === "/home") {// Return the main page
			respondMainPage(res);

		} else if (pathname.startsWith("/profile")) { // Return a specific profile
			respondWithProfile(res, parsed.query.id);

		} else if (pathname.startsWith("/about")) {    // Return the about page
			serveStaticFile("./about.html", res);

		} else if (pathname === "/clientscript.js") {  // Return the client-side javascript
			serveStaticFile("./clientscript.js", res);

		} else if (pathname.startsWith("/images/")) {  // Return an image for a page
			serveStaticFile("." + pathname, res);

		} else if (pathname === "/style.css") {        // Return the style file
  			serveStaticFile("./style.css", res);
            
		} else { // If anything else ... respond with 404 error
			send404(res);
		}
	//Any other combination of method/URL is not supported
	} else { // If no matches, send 405
		res.writeHead(405);
		res.end("Unsupported method");
	}
}

http.createServer(handleRequest).listen(3000);
console.log("Server running on port 3000");