package dynamic.people;

import dynamic.common.ContentGrabber;
import dynamic.common.OutputFileNamer;
import dynamic.people.dblist.StaffList;
import dynamic.people.dblist.StaffListEntry;
import dynamic.people.supplementary.StaffInfoOther;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

public class PeopleHtmlFormatterList extends PeopleHtmlFormatter {

    static int max_list_size = 125;
    static int output_file_counter = 0;
    static int column_width_name = 30;
    static int column_width_phone = 25;
    static int column_width_email = 10;

    protected static void th(BufferedWriter f) throws IOException {
        f.write("<th style=\"text-align: left;\">");
    }

    protected static void thw(BufferedWriter f, int width) throws IOException {
        f.write("<th style=\"text-align: left; width: " + width + "%;\">");
    }

    protected static void td(BufferedWriter f) throws IOException {
        f.write("<td>");
    }

//    protected static void nbsp(BufferedWriter f) throws IOException {
//            f.write("&nbsp;");
//    }

    public static void FormatOutput(StaffList sl, String title, String fname) {

        try {

            output_file_counter = 0;
            f = new BufferedWriter(new FileWriter(OutputFileNamer.GetOutputFilename(fname, output_file_counter)));

            f.write(ContentGrabber.content_div_tag_open);

            f.write("<h1>" + title + "</h1>");

            f.write("<table>");
            f.write("<tbody>");

            // write headers
            tr(f);
            thw(f, column_width_name);f.write("Name");thclose(f);
            thw(f, column_width_phone);f.write("Phone");thclose(f);
            thw(f, column_width_email);f.write("Email");thclose(f);
            th(f);f.write("Room");thclose(f);
            trclose(f);


            StaffInfoOther sio = new StaffInfoOther();

            for (int i = 0; i < sl.size(); i++) {

                StaffListEntry entry = sl.get(i);

                // open a new file for larger input
                if (i % max_list_size == max_list_size - 1)
                    open_next_output_file(fname);

                String login = entry.Login;
                String salutation = entry.Salutation;
                String firstname = entry.Firstname;
                String lastname = entry.Lastname;
                String fullname = salutation + " " + firstname + " " + lastname;

                AddressBookEntry abe = AddressBookRetriever.GetAddressBookDetails(login);

                String telephone;
                String room;
                String email;

                if (abe != null) {
                    telephone = abe.telephone;
                    email = abe.mail;
                    room = abe.room;
                } else {
                    telephone = entry.Telephone;
                    if (telephone.indexOf("48") == 0)
                        telephone = "+44 (0)20 759-" + telephone;
                    room = entry.Room;
                    email = login + "@doc.ic.ac.uk";
                }

                tr(f);

                // name
                td(f);
                f.write("<a href=\"http://www.doc.ic.ac.uk/~" + login.trim() + "\">");
                f.write("<strong>" + fullname.trim() + "</strong>");
                f.write("</a>");
                tdclose(f);

                // phone number
                td(f);
                f.write(telephone.trim());
                tdclose(f);

                // email
                td(f);
                f.write("<a href=\"mailto:" + email.trim() + "\" target=\"_blank\">email</a>");
                tdclose(f);

                // room
                td(f);
                f.write(room.trim());
                tdclose(f);

                trclose(f);

            }


            f.write("</tbody>");
            f.write("</table>");
            f.write(ContentGrabber.content_div_tag_close);

            f.close();

        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    private static void open_next_output_file(String fname) throws IOException {

        f.write("</tbody>");
        f.write("</table>");
        f.write(ContentGrabber.content_div_tag_close);

        f.close();

        f = new BufferedWriter(new FileWriter(OutputFileNamer.GetOutputFilename(fname, ++output_file_counter)));
        f.write(ContentGrabber.content_div_tag_open);
        f.write("<table>");
        f.write("<tbody>");
            // write headers
        tr(f);
        thw(f,column_width_name);thclose(f);
        thw(f,column_width_phone);thclose(f);
        thw(f,column_width_email);thclose(f);
        th(f);thclose(f);
        trclose(f);


    }

}
