import java.awt.Dimension;
import java.awt.Toolkit;
import java.io.*;

import javax.swing.JFrame;

// 21jul2010 New, in order to write selected "row" to the RULEx_ID PV
// 22oct2010 Now used to connect to actual global rule DESC strings to display current values
// in dev, path is /afs/slac/g/lcls/physics/release/xal4lcls-R0-8-2/gov/sns/ca/Channel.java
// 25oct2010 Main now calls JayTable constructor instead of createAndShowGUI
import gov.sns.ca.Channel;
import gov.sns.ca.ChannelException;

import gov.sns.ca.ChannelFactory;
import gov.sns.ca.ConnectionException;
import gov.sns.ca.GetException;
import gov.sns.ca.PutException;


// File: globalRuleChooser.java
// Author: Dayle Kotturi <dayle@slac.stanford.edu> September 2010
 
public class globalRuleChooser {
  private static Channel[] ruleIDChan = null;
  private static Channel[] globalRuleNameChan = null;
  private static ChannelFactory cf = null;
  private static boolean connStatus = true; 
  public static String[] globalRuleNames; 
  public static int[] validRuleIndices;

  public static void main(String[] args) {

    String inputFile;
    String destTriggerRule;
    String globalTop;
    String thisLine;
    int numValidRules = 0;
    int PDU_NUM_GLOBAL_RULES = 11; //No preproc in java so no defines
                                   //Must match PDU_NUM_GLOBAL_RULES in epics app
    if(args.length!=2) {
      System.out.println("usage: java globalRuleChooser <GLOBAL_TOP> <hard IOC trig rule PV to set>");
      return;
    }

    globalTop = args[0];
    System.out.println("working with SIOC global top: "+globalTop); 

    // Actually, this is the MAX size. If some are UNUSED, it will be less
    globalRuleNames = new String[PDU_NUM_GLOBAL_RULES]; //Allocate memory to hold the array of strings
    validRuleIndices = new int[PDU_NUM_GLOBAL_RULES]; //Allocate memory to indices of valid rules

    destTriggerRule = args[1];
    System.out.println("working with SIOC trig rule PV: "+destTriggerRule); 
 
    try {
      // Set row to value of destTriggerRule PV via jca
      cf = ChannelFactory.defaultFactory();
      ruleIDChan = new Channel[1]; 
      ruleIDChan[0] = cf.getChannel(destTriggerRule);
      connStatus = connStatus & ruleIDChan[0].connectAndWait();
      if(!connStatus) {
        String err = "globalRuleChooser constructor: not connected to: "+destTriggerRule;
        System.out.println(err);		
        throw new ConnectionException();
      }			
    }
    catch (ConnectionException e) {
    }

    try {
      // Connect to all the global rules via jca in order to get their names
      // Ignore the global rules that are "UNUSED". Users should not select this
      // Yes, it makes it messy. Now we need a look up table to know what rule it
      // is as the table row won't necessarily be it
      globalRuleNameChan = new Channel[11];
      globalRuleNameChan[0] = cf.getChannel(globalTop+":SYS0:0:RULE.DESC");
      connStatus = connStatus & globalRuleNameChan[0].connectAndWait();
      if(!connStatus) {
        String err = "globalRuleChooser constructor: not connected to: "+globalTop+":SYS0:0:RULE.DESC";
        System.out.println(err);		
        throw new ConnectionException();
      }			
      try {
        globalRuleNames[numValidRules] = globalRuleNameChan[0].getValString();
      } 
      catch (GetException e) {
      }  
      if (globalRuleNames[numValidRules].equals("UNUSED")) {
        // We don't want it to appear in the list of choices
        // Don't increment numValidRules
        System.out.println("Rejecting "+globalTop+":SYS0:0:RULE.DESC from the list because it is not set up"); 
        globalRuleNameChan[0].disconnect(); 
      } else {
        // Ok. Increment numValidRules
        validRuleIndices[numValidRules]=0;
        numValidRules++;
      }

      globalRuleNameChan[1] = cf.getChannel(globalTop+":SYS0:1:RULE.DESC");
      connStatus = connStatus & globalRuleNameChan[1].connectAndWait();
      if(!connStatus) {
        String err = "globalRuleChooser constructor: not connected to: "+globalTop+":SYS0:1:RULE.DESC";
        System.out.println(err);		
        throw new ConnectionException();
      }			
      try {
        globalRuleNames[numValidRules] = globalRuleNameChan[1].getValString();
      } 
      catch (GetException e) {
      }  
      if (globalRuleNames[numValidRules].equals("UNUSED")) {
        // We don't want it to appear in the list of choices
        // Don't increment numValidRules
        System.out.println("Rejecting "+globalTop+":SYS0:1:RULE.DESC from the list because it is not set up"); 
      } else {
        // Ok. Increment numValidRules
        validRuleIndices[numValidRules]=1;
        numValidRules++;
      }

      globalRuleNameChan[2] = cf.getChannel(globalTop+":SYS0:2:RULE.DESC");
      connStatus = connStatus & globalRuleNameChan[2].connectAndWait();
      if(!connStatus) {
        String err = "globalRuleChooser constructor: not connected to: "+globalTop+":SYS0:2:RULE.DESC";
        System.out.println(err);		
        throw new ConnectionException();
      }			
      try {
      globalRuleNames[numValidRules] = globalRuleNameChan[2].getValString();
      } 
      catch (GetException e) {
      }  
      if (globalRuleNames[numValidRules].equals("UNUSED")) {
        // We don't want it to appear in the list of choices
        // Don't increment numValidRules
        System.out.println("Rejecting "+globalTop+":SYS0:2:RULE.DESC from the list because it is not set up"); 
      } else {
        // Ok. Increment numValidRules
        validRuleIndices[numValidRules]=2;
        numValidRules++;
      }

      globalRuleNameChan[3] = cf.getChannel(globalTop+":SYS0:3:RULE.DESC");
      connStatus = connStatus & globalRuleNameChan[3].connectAndWait();
      if(!connStatus) {
        String err = "globalRuleChooser constructor: not connected to: "+globalTop+":SYS0:3:RULE.DESC";
        System.out.println(err);		
        throw new ConnectionException();
      }			
      try {
        globalRuleNames[numValidRules] = globalRuleNameChan[3].getValString();
      } 
      catch (GetException e) {
      }  
      if (globalRuleNames[numValidRules].equals("UNUSED")) {
        // We don't want it to appear in the list of choices
        // Don't increment numValidRules
        System.out.println("Rejecting "+globalTop+":SYS0:3:RULE.DESC from the list because it is not set up"); 
      } else {
        // Ok. Increment numValidRules
        validRuleIndices[numValidRules]=3;
        numValidRules++;
      }

      globalRuleNameChan[4] = cf.getChannel(globalTop+":SYS0:4:RULE.DESC");
      connStatus = connStatus & globalRuleNameChan[4].connectAndWait();
      if(!connStatus) {
        String err = "globalRuleChooser constructor: not connected to: "+globalTop+":SYS0:4:RULE.DESC";
        System.out.println(err);		
        throw new ConnectionException();
      }			
      try {
        globalRuleNames[numValidRules] = globalRuleNameChan[4].getValString();
      } 
      catch (GetException e) {
      }  
      if (globalRuleNames[numValidRules].equals("UNUSED")) {
        // We don't want it to appear in the list of choices
        // Don't increment numValidRules
        System.out.println("Rejecting "+globalTop+":SYS0:4:RULE.DESC from the list because it is not set up"); 
      } else {
        // Ok. Increment numValidRules
        validRuleIndices[numValidRules]=4;
        numValidRules++;
      }

      globalRuleNameChan[5] = cf.getChannel(globalTop+":SYS0:5:RULE.DESC");
      connStatus = connStatus & globalRuleNameChan[5].connectAndWait();
      if(!connStatus) {
        String err = "globalRuleChooser constructor: not connected to: "+globalTop+":SYS0:5:RULE.DESC";
        System.out.println(err);		
        throw new ConnectionException();
      }			
      try {
        globalRuleNames[numValidRules] = globalRuleNameChan[5].getValString();
      } 
      catch (GetException e) {
      }  
      if (globalRuleNames[numValidRules].equals("UNUSED")) {
        // We don't want it to appear in the list of choices
        // Don't increment numValidRules
        System.out.println("Rejecting "+globalTop+":SYS0:5:RULE.DESC from the list because it is not set up"); 
      } else {
        // Ok. Increment numValidRules
        validRuleIndices[numValidRules]=5;
        numValidRules++;
      }

      globalRuleNameChan[6] = cf.getChannel(globalTop+":SYS0:6:RULE.DESC");
      connStatus = connStatus & globalRuleNameChan[6].connectAndWait();
      if(!connStatus) {
        String err = "globalRuleChooser constructor: not connected to: "+globalTop+":SYS0:6:RULE.DESC";
        System.out.println(err);		
        throw new ConnectionException();
      }			
      try {
        globalRuleNames[numValidRules] = globalRuleNameChan[6].getValString();
      } 
      catch (GetException e) {
      }  
      if (globalRuleNames[numValidRules].equals("UNUSED")) {
        // We don't want it to appear in the list of choices
        // Don't increment numValidRules
        System.out.println("Rejecting "+globalTop+":SYS0:6:RULE.DESC from the list because it is not set up"); 
      } else {
        // Ok. Increment numValidRules
        validRuleIndices[numValidRules]=6;
        numValidRules++;
      }

      globalRuleNameChan[7] = cf.getChannel(globalTop+":SYS0:7:RULE.DESC");
      connStatus = connStatus & globalRuleNameChan[7].connectAndWait();
      if(!connStatus) {
        String err = "globalRuleChooser constructor: not connected to: "+globalTop+":SYS0:7:RULE.DESC";
        System.out.println(err);		
        throw new ConnectionException();
      }			
      try {
        globalRuleNames[numValidRules] = globalRuleNameChan[7].getValString();
      } 
      catch (GetException e) {
      }  
      if (globalRuleNames[numValidRules].equals("UNUSED")) {
        // We don't want it to appear in the list of choices
        // Don't increment numValidRules
      } else {
        // Ok. Increment numValidRules
        validRuleIndices[numValidRules]=7;
        numValidRules++;
      }

      globalRuleNameChan[8] = cf.getChannel(globalTop+":SYS0:8:RULE.DESC");
      connStatus = connStatus & globalRuleNameChan[8].connectAndWait();
      if(!connStatus) {
        String err = "globalRuleChooser constructor: not connected to: "+globalTop+":SYS0:8:RULE.DESC";
        System.out.println(err);		
        throw new ConnectionException();
      }			
      try {
        globalRuleNames[numValidRules] = globalRuleNameChan[8].getValString();
      } 
      catch (GetException e) {
      }  
      if (globalRuleNames[numValidRules].equals("UNUSED")) {
        // We don't want it to appear in the list of choices
        // Don't increment numValidRules
        System.out.println("Rejecting "+globalTop+":SYS0:8:RULE.DESC from the list because it is not set up"); 
      } else {
        // Ok. Increment numValidRules
        validRuleIndices[numValidRules]=8;
        numValidRules++;
      }

      globalRuleNameChan[9] = cf.getChannel(globalTop+":SYS0:9:RULE.DESC");
      connStatus = connStatus & globalRuleNameChan[9].connectAndWait();
      if(!connStatus) {
        String err = "globalRuleChooser constructor: not connected to: "+globalTop+":SYS0:9:RULE.DESC";
        System.out.println(err);		
        throw new ConnectionException();
      }			
      try {
        globalRuleNames[numValidRules] = globalRuleNameChan[9].getValString();
      } 
      catch (GetException e) {
      }  
      if (globalRuleNames[numValidRules].equals("UNUSED")) {
        // We don't want it to appear in the list of choices
        // Don't increment numValidRules
        System.out.println("Rejecting "+globalTop+":SYS0:9:RULE.DESC from the list because it is not set up"); 
      } else {
        // Ok. Increment numValidRules
        validRuleIndices[numValidRules]=9;
        numValidRules++;
      }

      globalRuleNameChan[10] = cf.getChannel(globalTop+":SYS0:10:RULE.DESC");
      connStatus = connStatus & globalRuleNameChan[10].connectAndWait();
      if(!connStatus) {
        String err = "globalRuleChooser constructor: not connected to: "+globalTop+":SYS0:10:RULE.DESC";
        System.out.println(err);		
        throw new ConnectionException();
      }			
      try {
        globalRuleNames[numValidRules] = globalRuleNameChan[10].getValString();
      } 
      catch (GetException e) {
      }  
      if (globalRuleNames[numValidRules].equals("UNUSED")) {
        // We don't want it to appear in the list of choices
        // Don't increment numValidRules
        System.out.println("Rejecting "+globalTop+":SYS0:10:RULE.DESC from the list because it is not set up"); 
      } else {
        // Ok. Increment numValidRules
        validRuleIndices[numValidRules]=10;
        numValidRules++;
      }
    }
    catch (ConnectionException e) {
    }
  
    // Fix this now that we're at the end
    numValidRules--;

    System.out.println("Creating content pane for Global Rule Names");
    // The validRuleIndices is a look up table for get out the actual rule number. Since we are no longer
    // displaying any rule that is called "UNUSED", we can no longer directly use the row number of the table as the rule number
    JayTable newContentPane = new JayTable(globalRuleNames, "Global Rule Names", validRuleIndices, numValidRules, ruleIDChan, destTriggerRule);

    JFrame frame = new JFrame(destTriggerRule);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setContentPane(newContentPane); 
    frame.pack();

    // Position the window. To accommodate both single and double headed workstations, position is one quarter of
    // the way across and vertically in the middle
    int w = frame.getSize().width;
    int h = frame.getSize().height;
    Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
    int x=(dim.width-w)/4;
    int y=(dim.height-h)/2;
    frame.setLocation(x, y);

    //Display the window.
    frame.setVisible(true);
  }
}

