package Visitors;

import Types.*;
import Elements.*;
import GUI.*;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

import java.util.ArrayList;

/**
 * A visitor to display a detailed view of a particular study.  Note: this 
 * approach differs slightly from that taken in the Summary and Source writers
 * due to the need to get the appropriate study to work with from the event in
 * the ActionListener in the controller.
 */
public class CStudyDetailsWriter implements CStudyVisitor
{
	JPanel detailsContainer;
	CStudyView view;

	public CStudyDetailsWriter(CStudyView v)
	{
		view = v;
	}

	// do some setup and visit the kids
	public void visitStudy(CStudyElement e)
	{
		// to store details
		detailsContainer = new JPanel();
		detailsContainer.setLayout(new BoxLayout(detailsContainer, BoxLayout.Y_AXIS));

		// a visitor to get details
		CStudyContainerElement curEl = (CStudyContainerElement) e;
		ArrayList<CStudyElement> kids = curEl.children;

		// visit the children to get details
		for (CStudyElement kid:kids)
		{
			kid.accept(this);
		}

		// set up the overall details pane with the content from the current study
		view.setupDetails(detailsContainer);
	}

	// this visitor doesn't operate on study sets at this stage
	public void visitStudySet(CStudyElement e){}

	// write a heading and then add any titles info that exists
	public void visitTitles(CStudyElement e)
	{
		Titles t = (Titles) e.getType();
		
		// write a heading
		JTextArea titleHeading = new JTextArea();
		titleHeading.setEditable(false);
		titleHeading.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 14));
		titleHeading.append("Titles");
		detailsContainer.add(titleHeading);

		// write the details
		JTextArea titleDetail = new JTextArea();
		titleDetail.setEditable(false);
		titleDetail.setLineWrap(true);
		titleDetail.setWrapStyleWord(true);

		// brief title is guaranteed to exist by dtd
		titleDetail.append("Brief Title:\n  " + t.getBriefTitle() + "\n");

		// add extra text if it is available (illustrates a generalisable 
		// approach to the display of optional elements)
		if (!t.getOfficialTitle().equals(""))
		{
			titleDetail.append("Official Title:\n  " + t.getOfficialTitle() + "\n");
		}

		if (!t.getAcronym().equals(""))
		{
			titleDetail.append("Acronym:\n  " + t.getAcronym() + "\n");
		}

		detailsContainer.add(titleDetail);
	}

	// write a heading and then add any sponsors info that exists
	public void visitSponsors(CStudyElement e)
	{
		Sponsors s = (Sponsors) e.getType();

		// write heading
		JTextArea sponsorsHeading = new JTextArea();
		sponsorsHeading.setEditable(false);
		sponsorsHeading.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 14));
		sponsorsHeading.append("Sponsors");
		detailsContainer.add(sponsorsHeading);

		// write details
		JTextArea sponsorsDetail = new JTextArea();
		sponsorsDetail.setEditable(false);

		sponsorsDetail.append("Lead sponsor:\n  " + s.getLead().getAgency());
		sponsorsDetail.append(" (" + s.getLead().getSClass() + ")\n");

		detailsContainer.add(sponsorsDetail);
	}

	// write a heading and then add any status info that exists
	public void visitStatus(CStudyElement e)
	{
		Status s = (Status) e.getType();

		// write heading
		JTextArea statusHeading = new JTextArea();
		statusHeading.setEditable(false);
		statusHeading.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 14));
		statusHeading.append("Status");
		detailsContainer.add(statusHeading);

		// write details
		JTextArea statusDetails = new JTextArea();
		statusDetails.setEditable(false);

		statusDetails.append("Overall status:\n  " + s.getStatus() + "\n");

		detailsContainer.add(statusDetails);
	}
	
	// write a heading and then add any misc info that exists
	public void visitMisc(CStudyElement e)
	{
		Misc m = (Misc) e.getType();

		// write heading
		JTextArea miscHeading = new JTextArea();
		miscHeading.setEditable(false);
		miscHeading.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 14));
		miscHeading.append("Miscellaneous");
		detailsContainer.add(miscHeading);

		// write details
		JTextArea miscDetails = new JTextArea();
		miscDetails.setEditable(false);

		miscDetails.append("Study type:\n  " + m.getSType() + "\n");

		detailsContainer.add(miscDetails);
	}

	// write a heading and then add any dates info that exists
	public void visitDates(CStudyElement e)
	{
		Dates d = (Dates) e.getType();

		// write heading
		JTextArea datesHeading = new JTextArea();
		datesHeading.setEditable(false);
		datesHeading.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 14));
		datesHeading.append("Dates");
		detailsContainer.add(datesHeading);

		// write details
		JTextArea datesDetails = new JTextArea();
		datesDetails.setEditable(false);

		datesDetails.append("First received:\n  " + d.getFirstRec() + "\n");

		detailsContainer.add(datesDetails);
	}
	
	// write a heading and then add any responsible party info that exists
	public void visitResponsibleParty(CStudyElement e)
	{
		ResponsibleParty r = (ResponsibleParty) e.getType();

		// write info
		JTextArea resPartyHeading = new JTextArea();
		resPartyHeading.setEditable(false);
		resPartyHeading.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 14));
		resPartyHeading.append("Responsible Party");
		detailsContainer.add(resPartyHeading);

		// write details
		JTextArea resPartyDetails = new JTextArea();
		resPartyDetails.setEditable(false);

		resPartyDetails.append("Name:\n  " + r.getNameT() + "\n");
		resPartyDetails.append("Organisation:\n  " + r.getOrg() + "\n");

		detailsContainer.add(resPartyDetails);
	}


	// for potential future implementation
	public void visitOversight(CStudyElement e){}
	public void visitDescription(CStudyElement e){}
	public void visitOutcomes(CStudyElement e){}
	public void visitConditions(CStudyElement e){}
	public void visitInterventions(CStudyElement e){}
	public void visitEligibility(CStudyElement e){}
	public void visitContacts(CStudyElement e){}
	public void visitLocations(CStudyElement e){}
}