/*
 * MainApp.java
 *
 * Created on July 18, 2009, 12:15 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

/**
 *
 * @author Khalegh
 */
import java.io.*;
import java.util.Scanner;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.EtchedBorder;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.ListSelectionEvent;
import java.net.URL;

public class MainApp extends JFrame implements ActionListener {
    private int colorMode = 0;
    private int total = 23;
    private ImageIcon nextIcon = null;
    private ImageIcon prevIcon = null;
    
    private JRadioButton multiColor;
    private JRadioButton singleColor;
    private JButton generateBtn;
    private JButton nextBtn;
    private JButton prevBtn;
    private JComboBox allBox;
    
    private JPanel mainPan;
    
    private int n;
    private VennList vennList;
    private VennDiagram [] diagrams;
    private JPanel [] vennPanels;
    private int diagramIndex;
    
    /** Creates a new instance of MainApp */
    public MainApp(String title, int n) {
        super(title);
        this.n = n;
        int scrWidth = (int) Toolkit.getDefaultToolkit().getScreenSize().getWidth();
        int scrHeight = (int) Toolkit.getDefaultToolkit().getScreenSize().getHeight() - 30;
        setSize(scrWidth, scrHeight);
        this.setResizable(false);
        loadImages();
        createComponents();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }
    
    private void loadImages() {
        URL imgURL;
        Class vennClass = getClass();
        imgURL = vennClass.getResource("Images/snext.png");
        if (imgURL != null) {
            nextIcon = new ImageIcon(imgURL);
        }
        imgURL = vennClass.getResource("Images/sprev.png");
        if (imgURL != null) {
            prevIcon = new ImageIcon(imgURL);
        }
    }
    private void createComponents() {
        JPanel toolBar = new JPanel();
        toolBar.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.RAISED));
        toolBar.setBackground(Color.lightGray);
        toolBar.setLayout(new GridLayout(1, 0));
        
        JPanel leftPan = new JPanel();
        leftPan.setBackground(Color.lightGray);
        
        JPanel midPan = new JPanel();
        midPan.setBackground(Color.lightGray);
        
        JPanel rightPan = new JPanel();
        rightPan.setBackground(Color.lightGray);
        
        
        generateBtn = new JButton("Generate All");
        generateBtn.setActionCommand("1");
        generateBtn.addActionListener(this);
        
        JButton bestBtn = new JButton("Select Bests");
        bestBtn.setActionCommand("2");
        bestBtn.addActionListener(this);
        
        leftPan.add(generateBtn);
        leftPan.add(bestBtn);
        
        singleColor = new JRadioButton("SingleColor");
        singleColor.setActionCommand("3");
        singleColor.setSelected(true);
        singleColor.addActionListener(this);
        multiColor = new JRadioButton("MultiColor");
        multiColor.setActionCommand("4");
        multiColor.addActionListener(this);
        ButtonGroup colorBox = new ButtonGroup();
        colorBox.add(singleColor);
        colorBox.add(multiColor);
        
        JButton xfigBtn = new JButton("Create XFig files");
        xfigBtn.setActionCommand("5");
        xfigBtn.addActionListener(this);
        
        midPan.add(singleColor);
        midPan.add(multiColor);
        midPan.add(xfigBtn);
        
        prevBtn = new JButton(prevIcon);
        prevBtn.setActionCommand("6");
        prevBtn.addActionListener(this);
        prevBtn.setEnabled(false);
        
        allBox = new JComboBox();
        allBox.setEditable(false);
        allBox.setActionCommand("7");
        allBox.addActionListener(this);
        allBox.setEnabled(false);
        
        nextBtn = new JButton(nextIcon);
        nextBtn.setActionCommand("8");
        nextBtn.addActionListener(this);
        nextBtn.setEnabled(false);
        
        rightPan.add(prevBtn);
        rightPan.add(allBox);
        rightPan.add(nextBtn);
        
        toolBar.add(leftPan);
        toolBar.add(midPan);
        toolBar.add(rightPan);
        
        getContentPane().add(toolBar, BorderLayout.PAGE_START);
        mainPan = new JPanel();
        getContentPane().add(mainPan, BorderLayout.CENTER);
        
    }
    
    public void actionPerformed(ActionEvent e) {
        int buttonIndex = Integer.parseInt(e.getActionCommand());
        
        switch (buttonIndex) {
            case 1:
                AllVenn all = new AllVenn(n);
                vennList = all.generateAll();
                
//                int size = vennList.size();
//                diagrams = new VennDiagram[size];
//                vennPanels = new JPanel[size];
//                for (int i = 0; i < size; i++) {
//                    Venn venn = vennList.getVenn(i);
//                    int [][] mx = venn.getMatrix();
//                    System.out.println(venn.getNo());
//                    diagrams[i] = new VennDiagram(mx, colorMode);
//                    System.out.println("Balanced Points :" + diagrams[i].getTotalBalanced());
//                }
//                showDiagram(0);
//                allBox.removeAllItems();
//                for (int i = 1; i <= size; i++) {
//                    allBox.addItem(i);
//                }
//                prevBtn.setEnabled(true);
//                allBox.setEnabled(true);
//                nextBtn.setEnabled(true);
                break;
            case 2:
                findBests();
                break;
            case 3:
                colorMode = 0;
                break;
            case 4:
                colorMode = 1;
                break;
            case 5:
                createDiagrams();
                break;
            case 6:
                if (diagramIndex > 0) {
                    diagramIndex--;
                    allBox.setSelectedIndex(diagramIndex);
                    showDiagram(diagramIndex);
                }
                break;
            case 7:
                diagramIndex = allBox.getSelectedIndex();
                showDiagram(diagramIndex);
                break;
            case 8:
                if (diagramIndex < vennList.size() - 1) {
                    diagramIndex++;
                    allBox.setSelectedIndex(diagramIndex);
                    showDiagram(diagramIndex);
                }
        }
        
    }
    
    private void showDiagram(int index) {
        getContentPane().remove(mainPan);
        getContentPane().validate();
        if (vennPanels[index] == null) {
            vennPanels[index] = diagrams[index].createVennDiagram();
        }
        mainPan = vennPanels[index];
        getContentPane().add(mainPan, BorderLayout.CENTER);
        getContentPane().validate();
        repaint();
    }
    
    private int[][] readMatrix(Scanner file, int rows, int cols) {
        int [][] mx = new int[rows][cols];
        for (int i = 0; i < n - 1; i++) {
            for (int j = 0; j < cols; j++) {
                mx[i][j] = file.nextInt();
            }
        }
        return mx;
    }
    
    private void findBests() {
        PrintWriter bestsFile = null;
        try {
            bestsFile = new PrintWriter(new BufferedWriter(new FileWriter("Data/BestVenn.txt")));
        } catch (java.io.IOException e) { }
        
        for (int no = 1; no <= total; no++) {
            int [][] mx;
            int [][] bestMX = null;
            VennDiagram best = null;
            int cols;
            int rows;
            
            String fileName = String.format("Data/Venn_%02d.txt", no);
            Scanner vennFile = null;
            try {
                vennFile = new Scanner(new BufferedReader(new FileReader(fileName)));
            } catch (java.io.IOException e) { }
            rows = vennFile.nextInt() - 1;
            if (no == 1) {
                bestsFile.println(rows + 1);
            }
            
            while (vennFile.hasNext()) {
                cols = vennFile.nextInt();
                mx = readMatrix(vennFile, rows, cols);
                VennDiagram diagram = new VennDiagram(mx, 0);
                int t = diagram.getTotalBalanced();
                if (best == null || (diagram.getTotalBalanced() < best.getTotalBalanced())) {
                    best = diagram;
                    bestMX = mx;
               }
            }
            vennFile.close();
            
            Venn v = new Venn(bestMX);
            System.out.println("Digram no :" + no);
            v.printCodes();
            
            bestsFile.println(bestMX[0].length);
            for (int i = 0; i < bestMX.length; i++) {
                for (int j = 0; j < bestMX[i].length; j++) {
                    bestsFile.print(bestMX[i][j] + " ");
                }
                bestsFile.println();
            }
        }
        bestsFile.close();
    }
    
    private void createDiagrams() {
        int cols;
        int rows;
        
        VennList bestList = new VennList();
        Scanner bestsFile = null;
        try {
            bestsFile = new Scanner(new BufferedReader(new FileReader("Data/BestVenn.txt")));
        } catch (java.io.IOException e) { }
        rows = bestsFile.nextInt() - 1;
        while (bestsFile.hasNext()) {
            cols = bestsFile.nextInt();
            int [][] mx = readMatrix(bestsFile, rows, cols);
            Venn bestVenn = new Venn(mx);
            bestList.add(bestVenn);
        }
        bestsFile.close();
        
        for (int i = 0; i < bestList.size(); i++) {
            Venn bestVenn = bestList.getVenn(i);
            int no = i + 1;
            System.out.println("Diagram No : " + no);
            bestVenn.printCodes();
            if (bestVenn.isPolarSymmetric()) {
                System.out.println("Is polar symmetric.");
                System.out.println("-----------------------------------------------------------------------");
            }
            int [][] mx = bestVenn.getMatrix();
            VennDiagram diagram = new VennDiagram(mx, colorMode);
            diagram.createXFigDiagram();
            diagram.createCylindrical();
            diagram.createArcDiagram();
        }
    }
    
    public static void main(String [] args) {
        MainApp app = new MainApp("All Symmetric 7_Venn Diagramas", 7);
    }
    
}
