import java.util.Scanner;
import java.util.ArrayList;
import java.io.File;

public class CustomProgram {
    public static void main(String[] args) throws Exception {
	// reads the provided customPage.html into list
	Scanner in = new Scanner(new File("customPage.html"));
	ArrayList<String> list = new ArrayList<>();
	while(in.hasNextLine()) list.add(in.nextLine());

	String name = "Stranger";
	// update list to reflect changes requested through command line args
	// TODO: Complete this section
	if(args.length > 0)
	    for(String arg : args[0].split("&")) {
		String[] keyValuePair = arg.split("=");
		switch(keyValuePair[0]) {
		case "name":
		    // TODO: when a greeting is selected (below), this
		    // arguments's value should be displayed in that greeting
		    name = keyValuePair[1];
		    break;
		case "background":
		    // TODO: when background="Dark", the body's color should be
		    // set to white and it's background-color should be set to
		    // black (the opposite of how they are set for "Light" by
		    // default.

		    if (keyValuePair[1].equals("Dark")) {
			int i;
			for (i = 0; i < list.size(); i++){
			    if (list.get(i).contains("background-color:")) {
				break;
			    }
			}
			list.set(i,"      background-color: black;");
			list.set(i + 1,"      color: white;");
		    }
		    
		    break;
		case "Greeting":
		    // TODO: when this argument is present and =true, an <h1>
		    // element containing the text "Welcome Stranger" should
		    // be inserted as the first element within the body.  If
		    // a non-empty-string name is provided (see above), that
		    // name should be used in place of the word Stranger in
		    // this greeting.
		    if (keyValuePair[1].equals("true")) {
			int i;
			for (i = 0; i < list.size(); ++i) {
			    if (list.get(i).contains("</body>")) {
				break;
			    }
			}
			list.add(i + 1, "      <h1>Welcome: " + name + "</h1>");
		    }
		    break;
		case "Time":
		    // TODO: when this argument is present and =true, a <p>
		    // element containing the text: "Page Updated: date-time"
		    // should be inserted as the last element within the body.
		    // Note that the date-time part of this paragraph should
		    // be dynamically generated by calling
		    // java.time.LocalDateTime.now()
		    if (keyValuePair[1].equals("true")) {
                        int i;
                        for (i = 0; i < list.size(); ++i) {
                            if (list.get(i).contains("</body>")) {
                                break;
                            }
                        }
                        list.add(i - 1, "      <h1>Page Updated: " + java.time.LocalDateTime.now() + "</h1>");
                    }
		    break;
		case "SuppressOptions":
		    // TODO: when this argument is present and =true, the
		    // customization controls should be removed from the page.
		    // Everything from and including the <h1> label through the
		    // final </ul> should be omitted to accomplish this.
		    if (keyValuePair[1].equals("true")) {
                        int a = 0;
			int b = 0;
			int i;
                        for (i = 0; i < list.size(); ++i) {
                            if (list.get(i).contains("<body>")) {
                                a = i + 1;
                            } else if (list.get(i).contains("</body>")) {
				b = i - 1;
			    }
                        }
                        for (int j = b; j >= a; --j) {
			    list.remove(j);
			}
                    }
		    break;
		}
	    }
	
	// print the resulting html out to system.out (standard out)
	for(String line : list)
	    System.out.println(line);
    }
}
