package Matrix2P;

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

/**
 * Author: Max Buegler - max@sarstech.com
 * Date: Oct 5, 2009
 * Time: 7:50:27 PM
 */
public class DominatedActions implements GameAlgorithm{
    private boolean stop=false;
    public void settings(ActionListener ul) {
        final JFrame settingsFrame=new JFrame("Dominated Actions settings");
                settingsFrame.setLayout(new GridBagLayout());
                GridBagConstraints cons=new GridBagConstraints();
                cons.weightx=0.5;
                cons.weighty=0.5;
                cons.gridx=0;
                cons.gridy=0;
                cons.gridheight=1;
                cons.gridwidth=1;
                cons.fill=GridBagConstraints.HORIZONTAL;
                JPanel aboutPanel=new JPanel();
                aboutPanel.setBorder(BorderFactory.createCompoundBorder(
                                BorderFactory.createTitledBorder("About"),
                                BorderFactory.createEmptyBorder(5,5,5,5)));
                    aboutPanel.setLayout(new GridLayout(1,1));
                    JTextArea about=new JTextArea();
                    about.setEditable(false);
                    about.setCursor(null);
                    about.setOpaque(false);
                    about.setFocusable(false);
                    about.setLineWrap(true);
                    about.setText("Finds dominated actions for both players");
                    aboutPanel.add(about);
                settingsFrame.add(aboutPanel,cons);

                settingsFrame.setSize(300,200);
                settingsFrame.setVisible(true);

    }

    public void run(GameMatrix m, ActionListener finish) {
        stop=false;
        boolean improvement=true;
        int step=0;
        boolean[] p1=new boolean[m.getActionsP1()];
        boolean[] p2=new boolean[m.getActionsP2()];
        int found=0;
        while(improvement){
            if (stop){
                finish.actionPerformed(null);
                return;
            }
            improvement=false;
            for (int x=0;x<m.getActionsP1();x++){
                if (!p1[x]){
                    double dominated=-1;
                    int dominating=-1;
                    for (int x2=0;x2<m.getActionsP1();x2++){

                        if (!p1[x2]&&x!=x2){
                            double d=Double.MAX_VALUE;
                            for (int y=0;y<m.getActionsP2();y++){
                                if (!p2[y]){
                                    double v=m.getP1()[x2][y]-m.getP1()[x][y];
                                    d=Math.min(d,v);
                                    //System.out.println(d);
                                }
                            }
                            if (d>dominated){
                                dominating=x2;
                                dominated=d;
                            }
                        }
                    }
                    if (dominated>=0){
                        step++;
                        p1[x]=true;
                        System.out.println("["+step+"] Pl1 action "+(x+1)+" dominated by "+(dominating+1));
                        found++;
                        improvement=true;
                    }
                }
            }
            for (int y=0;y<m.getActionsP2();y++){
                if (!p2[y]){
                    double dominated=-1;
                    int dominating=-1;
                    for (int y2=0;y2<m.getActionsP2();y2++){
                        if (!p2[y2]&&y!=y2){
                            double d=Double.MAX_VALUE;
                            for (int x=0;x<m.getActionsP1();x++){
                                if (!p1[x]){
                                    double v=m.getP2()[x][y2]-m.getP2()[x][y];
                                    d=Math.min(d,v);
                                }
                            }
                            if (d>dominated){
                                dominating=y2;
                                dominated=d;
                            }
                        }
                    }
                    if (dominated>=0){
                        step++;
                        p2[y]=true;
                        System.out.println("["+step+"] Pl2 action "+(y+1)+" dominated by "+(dominating+1));
                        found++;
                        improvement=true;
                    }
                }
            }
        }
        if (found==0)System.out.println("No dominated actions");
        finish.actionPerformed(null);
    }

    public void stop() {
        stop=true;
    }

    public String getDescriptor() {
        return "Dominated Actions";  
    }
}
