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;
import java.util.HashMap;

public class PeopleHtmlFormatterPictures extends PeopleHtmlFormatter {

    protected static void td(BufferedWriter f) throws IOException {
        f.write("<td style=\"vertical-align: center; text-align: center;\" width=\"33%\">");
    }

    public static void FormatOutput(StaffList sl, HashMap<String, String> image_lookup, String title, String fname) {

        try {

            StaffInfoOther sio = new StaffInfoOther();

            // only one output file required for pictures in general
            String output_file_name = OutputFileNamer.GetOutputFilename(fname,0);

            BufferedWriter f = new BufferedWriter(new FileWriter(output_file_name));

            f.write(ContentGrabber.content_div_tag_open);

            f.write("<h1>" + title + "</h1>");

            f.write("<table width=\"100%\">");

            f.write("<tbody>");

            int count = 0;
            int todo = sl.size();

            while (todo > 0) {
                int lstart = sl.size() - todo;
                int lend = lstart + 2;
                if (lend >= sl.size())
                    lend = sl.size() - 1;

                tr(f);
                for (int i = lstart; i <= lend; i++) {

                    StaffListEntry entry = sl.get(i);
                    td(f);

                    String salutation = entry.Salutation;
                    String firstname = entry.Firstname;
                    String lastname = entry.Lastname;
                    String fullname = salutation + " " + firstname + " " + lastname;

                    String login = entry.Login;

                    String imgsrc = "/pls/portallive/docs/1/19351700.JPG";
                    if (image_lookup.containsKey(login))
                        imgsrc = image_lookup.get(login);

                    f.write("<a href=\"http://www.doc.ic.ac.uk/~" + login + "\">");
                    f.write("<img src=\"" + webstub + imgsrc + "\" alt=\"Photo of " + fullname + "\" title=\"Photo of " + fullname + "\" width=\"100\" /></a>");
                    tdclose(f);
                }
                trclose(f);
                tr(f);
                for (int i = lstart; i <= lend; i++) {
                    StaffListEntry entry = sl.get(i);
                    td(f);
                    String salutation = entry.Salutation;
                    String firstname = entry.Firstname;
                    String lastname = entry.Lastname;
                    String fullname = salutation + " " + firstname + " " + lastname;
                    f.write("<strong>" + fullname + "</strong>");
                    tdclose(f);
                }
                trclose(f);
                tr(f);
                for (int i = lstart; i <= lend; i++) {
                    StaffListEntry entry = sl.get(i);
                    td(f);
                    String login = entry.Login;

                    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";

                    }

                    String email_link = "<a href=\"mailto:" + email + "\" target=\"_blank\">" + email + "</a>";
                    f.write(email_link);
                    f.write("<br />");
                    f.write(room);
                    f.write("<br />");
                    f.write(telephone);
                    tdclose(f);
                }
                trclose(f);
                tr(f);
                for (int i = lstart; i <= lend; i++) {
                    StaffListEntry entry = sl.get(i);
                    td(f);
                    String deptrole = entry.DeptRole;
                    f.write(deptrole.trim());
                    tdclose(f);
                }
                trclose(f);
                todo -= 3;
                if (todo > 0) {
                    tr(f);
                    for (int i = lstart; i <= lend; i++) {
                        td(f);
                        f.write("<br /><br />");
                        tdclose(f);
                    }
                    trclose(f);
                }
            }
            f.write("</tbody>");
            f.write("</table>");
            f.write(ContentGrabber.content_div_tag_close);

            f.close();

        } catch (IOException e) {
            e.printStackTrace();
        }

    }



}
