package uk.co.patrickhaston.mars;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextPane;

public class MarsReport
  extends MPanel
{
  private MarsFrame theFrame;

  private BorderLayout borderLayout1 = new BorderLayout();

  // add a way of closing this view
  private JButton closeButton = null;
  private JTextPane titlePanel = new JTextPane();
  private JTextPane descriptionPanel = new JTextPane();
  private JTextPane parentPanel = new JTextPane();
  private JScrollPane listScrollPanel = new JScrollPane();
  private MList linkList = new MList();
  
  private int theReportType;
  private int theId;
  
  private Vector links = null;
  
  // public constants
  public static int ACTIVITY_REPORT = 1;
  public static int SETTLEMENT_REPORT = 2;
  public static int PERSONAL_REPORT = 3;
  public static int SYSTEM_REPORT = 4;
  public static int MARSPEDIA_REPORT = 5;
  
  public MarsReport(MarsFrame frame)
  {
    super();
    try  
    {
      theFrame = frame;
      jbInit();
    }
    catch (Exception e) 
    {
      e.printStackTrace();
    }
  }

  public MarsReport(MarsFrame frame, int reportType, int id)
  {
    super();
    try  
    {
      theFrame = frame;
      jbInit();
    }
    catch (Exception e) 
    {
      e.printStackTrace();
    }
    
    theId = id;
    theReportType = reportType;
    
    showReport();
  }

  private void jbInit() throws Exception 
  {
    // add a button to close the view
    closeButton = new JButton("close");
    this.setLayout(null);
    closeButton.setBounds(new Rectangle(20, 10, 250, 20));
    closeButton.addActionListener(new ActionListener()
    {
      public void actionPerformed(ActionEvent e)
      {
       closeButton_ActionPerformed(e);
      }
    });

    titlePanel.setText("Title");
    titlePanel.setToolTipText("null");
    titlePanel.setBounds(new Rectangle(20, 30, 250, 20));
    titlePanel.setForeground(Color.red);
    titlePanel.setOpaque(false);
    titlePanel.setFont(new Font("Arial", 1, 14));

    descriptionPanel.setText("Description");
    descriptionPanel.setToolTipText("null");
    descriptionPanel.setBounds(new Rectangle(20, 50, 250, 60));
    descriptionPanel.setOpaque(false);
    descriptionPanel.setForeground(Color.orange);

    parentPanel.setText("Parent.");
    parentPanel.setToolTipText("null");
    parentPanel.setBounds(new Rectangle(20, 110, 250, 20));
    parentPanel.setForeground(Color.white);
    parentPanel.setOpaque(false);

    links = new Vector();
    
    listScrollPanel.setBounds(new Rectangle(20, 130, 250, 100));

    this.add(closeButton, null);
    this.add(titlePanel, null);
    this.add(descriptionPanel, null);
    this.add(parentPanel, null);
    listScrollPanel.getViewport().add(linkList, null);
    this.add(listScrollPanel, null);

    MouseListener mouseListener = new MouseAdapter() 
    {
      public void mouseClicked(MouseEvent e) 
      {
        int index = linkList.locationToIndex(e.getPoint());
        if (index >= 0)
        {
          showLink(index);
        }
      }
    };
    linkList.setForeground(Color.white);
    linkList.addMouseListener(mouseListener);
    
    mouseListener = new MouseAdapter()
    {
      public void mouseClicked(MouseEvent e) 
      {
        showParent();
      }
    };
  }

  void closeButton_ActionPerformed(ActionEvent e)
  {
    theFrame.hideReport();
  }
  
  private void showReport()
  {
    if(theReportType == MARSPEDIA_REPORT)
    {
      titlePanel.setText(theFrame.mars.marsPedia.getTitle(theId));
      descriptionPanel.setText(theFrame.mars.marsPedia.getDescription(theId));
      int parent_id = theFrame.mars.marsPedia.getParent(theId);
      parentPanel.setText(theFrame.mars.marsPedia.getTitle(parent_id));
      links.clear();
      int[] linkIds = theFrame.mars.marsPedia.getLinks(theId);
    }
  }
  
  private void showLink(int id)
  {
    if(theReportType == MARSPEDIA_REPORT)
    {
      int[] linkIds = theFrame.mars.marsPedia.getLinks(theId);
      theId = linkIds[id];
      showReport();
    }
    repaint();
  }

  private void showParent()
  {
    if(theReportType == MARSPEDIA_REPORT)
    {
      int parentId = theFrame.mars.marsPedia.getParent(theId);
      if(parentId == 0) return;
      theId = theFrame.mars.marsPedia.getParent(theId);
      showReport();
    }
    repaint();
  }
  
}

