package Division;

import Cooperative.Coalition;

import java.util.List;
import java.util.ArrayList;
import java.awt.event.ActionListener;
import java.awt.*;

import GUIs.CooperativeGUI;

import javax.swing.*;

/**
 * Author: Max Buegler - max@sarstech.com
 * Date: Oct 17, 2009
 * Time: 11:55:11 AM
 */
public class ConvertToCooperative implements DivisionCalculator{
    public void calculateShare(List<Claimer> claimers, double money, ActionListener finish) {
        ArrayList<Coalition> coalitions=getCoaltions(claimers.size());
        for (Coalition c:coalitions){
            double m=money;
            for (int x=0;x<claimers.size();x++){
                boolean inCoalition=false;
                for (int y:c.getPlayers())if (y==x)inCoalition=true;
                if (!inCoalition)
                    m-=claimers.get(x).getClaimed();
            }
            if (m<0)m=0;
            c.setValue(m);
        }
        new CooperativeGUI(coalitions,claimers.size());
        finish.actionPerformed(null);
    }


    private ArrayList<Coalition> getCoaltions(int p){
        ArrayList<Coalition> c=new ArrayList<Coalition>();
        for (int x=0;x<p;x++){
            getCoaltions(p,x+1,0,c,new int[x+1]);
        }
        return c;
    }
    private void getCoaltions(int p,int m, int n,ArrayList<Coalition> c, int[] cc){
        if (m==n)c.add(new Coalition(cc.clone(),0));
        else{
            int s=0;
            if (n>0)s=cc[n-1]+1;
            for (int x=s;x<p;x++){
                cc[n]=x;
                getCoaltions(p,m,n+1,c,cc);
            }
        }
    }


    public String getDescriptor() {
        return "Convert to cooperative game";  //To change body of implemented methods use File | Settings | File Templates.
    }

    public void stop() {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    public void settings(ActionListener updater) {
        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("Translates a bankruptcy problem to a cooperative game in Aumann and Maschler's way.");
            aboutPanel.add(about);
        settingsFrame.add(aboutPanel,cons);

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