package Division;

import Division.gui.DisplayDivision;

import javax.swing.*;
import java.util.Collections;
import java.util.List;
import java.awt.event.ActionListener;
import java.awt.*;

/**
 * The Constrained Equal Award rule division calculator
 * This rule gives all claimants equal shares, except when this would give a claimant more than his claim.
 * @author robbert
 *
 */

public class CEADivision implements DivisionCalculator{

	public void calculateShare(List<Claimer> claimers, double money, ActionListener finish) {
		// first sort the players
		Collections.sort(claimers, new ClaimerComparator());
		
		int competingPlayers = claimers.size(); // the players still competing for the money
		double equalShare = money / competingPlayers; // the initial amount each claimant receives
		
		for(Claimer c : claimers){
			if(c.getClaimed() >= equalShare){
				c.setReceived(equalShare);
				money -= equalShare;
			}
			else{
				c.setReceived(c.getClaimed());
				money -= c.getClaimed();
				
				// set the new share for the remaining players
				equalShare = money / --competingPlayers;  
			}
			
			if(money <= 0){
                finish.actionPerformed(null);
                System.out.println("Payout: ");
                for (Claimer cl:claimers)
                    System.out.println("  Claim: "+cl.getClaimed()+"   Received: "+cl.getReceived());
                new DisplayDivision(claimers, false, getDescriptor());
				return;
			}
		}
        System.out.println("Payout: ");
        for (Claimer cl:claimers)
            System.out.println("  Claim: "+cl.getClaimed()+"   Received: "+cl.getReceived());
        new DisplayDivision(claimers, false, getDescriptor());
        finish.actionPerformed(null);
	}
    public String getDescriptor() {
        return "CEA Division";
    }

    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("Uses the Constrained Equal Award rule division calculator to determine payoffs");
            aboutPanel.add(about);
        settingsFrame.add(aboutPanel,cons);

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