import java.awt.* ;
import java.awt.event.* ;
import java.applet.Applet ;
import java.io.InputStreamReader ;
import java.io.BufferedReader ;
import java.io.IOException ;
import java.net.URL ;
import java.net.URLConnection ;
import java.text.NumberFormat ;

class Player {
    static double weight ;
    String name ;
    Team team ;
    double gp,mn,pts,ast,to,fgmsd,ftmsd,off,reb,stl,blk,pf,pos,per,poss,sfo ;
    double fgm,fga,ftm,fta,treym,treya,disp ;
    double teamast, pace, lgdrb, lgftm, lgfta, vop, factor ;
    void compute() {
	teamast = team.teamast ;
	pace = Team.leaguePace / team.pace ;
	lgdrb = Team.lgdrb ;
	lgftm = Team.lgftm ;
	lgfta = Team.lgfta ;
	vop = Team.vop ;
	factor = Team.factor ;
	per = (treym + 0.667*ast + (2.-factor*teamast)*fgm +
	       (1.+(1.-teamast)+(0.667*teamast))*0.5*ftm - vop*to - 
	       vop*fgmsd*lgdrb - vop*ftmsd*0.44*(0.44+(0.56*lgdrb)) +
	       vop*(1.-lgdrb)*(reb-off) + vop*lgdrb*off + vop*stl + 
	       vop*lgdrb*blk - (lgftm-(0.44*vop*lgfta))*pf) / mn
	       * pace ;
	poss = (off + stl) * pace ;
	sfo =36. * (fga + 0.44*fta - off) / mn + (pos <= 3 ? -0.5 : 1.5) ;
    }
}

class Team {
    static double leaguePace, lgdrb, lgftm, lgfta, vop, factor ;
    String name ;
    double gp, mn, fgm, fga, ftm, fta, ast, off, reb, to, pf, pts ;
    double teamast, pace ;
    double fga_o, fta_o, off_o, to_o ;
    void compute() {
	teamast = ast / fgm ;
	pace = (0.44*(fta+fta_o) + fga + fga_o + to + to_o - off - off_o) / (2 *mn) ;
    }
}

public class bball extends Applet {
    String teamNames[] = { "atl", "bos", "cha", "chi", "cle", "dal", "den",
			   "det", "gsw", "hou", "ind", "lac", "lal", "mem",
			   "mia", "mil", "min", "njn", "nor", "nyk", "orl",
			   "phi", "pho", "por", "sac", "san", "sea", "tor",
			   "uta", "was" } ;

    Label label, label2, label3 ;
    TextField textField, textField2, textField3 ;
    Button b1 = new Button("PER"),
	b2 = new Button("Assists"),
	b3 = new Button("Rebounds"),
	b4 = new Button("SFO"),
	b5 = new Button("3pt%"),
	b6 = new Button("Steals"),
	b7 = new Button("Blocks"),
	b8 = new Button("PPR"),
	b9 = new Button("Poss"),
	bb = new Button("TS%"),
	bc = new Button("FT"),
	b0 = new Button("Close");
    CheckboxGroup cbg = new CheckboxGroup() ;
    Checkbox x1 = new Checkbox("Per/min", cbg, true),
	x2 = new Checkbox("Per/48 min", cbg, false),
	x3 = new Checkbox("Per/game", cbg, false),
	x4 = new Checkbox("Totals", cbg, false);
    int points = 0, listnum ;
    Team[] teams = new Team[30] ;
    Player[] players = new Player[600] ;
    List playerList = new List(24,false) ;
    double despos, mincut, desired ;
    String permin;
    static boolean standalone = false ;
    static int last = 1, divflag = 1 ;
    double[] sc = new double[600] ;
    NumberFormat form = NumberFormat.getInstance() ;
    Color backgrd = new Color(187, 102, 0) ;

    public void paint(Graphics g) {
	g.setColor(backgrd) ;
	g.fillRect(0, 0, getSize().width, getSize().height) ;
    }

    void startAction(int sorttype, int divflag) {
	getVals() ;
	sort(sorttype,divflag) ;
	if (listnum > (int)desired) listnum=(int)desired ;
	if (playerList.getItemCount() > 0) playerList.removeAll() ;
	for (int i=0;i<listnum;i++) showPlayer(i,players[i].name,players[i].disp) ;
    }

    void showPlayer(int i,String name,double f) {
	String fstr = form.format(f) ;
	String nstr = ((i < 9) ? " " : "")+String.valueOf(i+1) ;
	playerList.addItem(nstr+")    "+fstr+"    "+name) ;
    }

    public void init() {
	GridBagConstraints endConst = new GridBagConstraints() ;
	GridBagLayout gridbag = new GridBagLayout() ;
	setLayout(gridbag) ;
	endConst.weightx = 1.0 ;
	endConst.fill = GridBagConstraints.HORIZONTAL ;

	String[] fontList = Toolkit.getDefaultToolkit().getFontList() ;
	Font newfnt = new Font("Helvetica",Font.BOLD,14) ;
	this.setFont(newfnt) ;
	form.setMaximumFractionDigits(3) ;
	form.setMinimumFractionDigits(3) ;

	teamInfo("http://www.dougstats.com/09-10RD.Team.txt", 
		 "http://www.dougstats.com/09-10RD.Team.Opp.txt") ;
	double totalpace = 0., totaloff = 0., totalreb = 0., totalgp = 0. ;
	double totalftm = 0., totalfta = 0., totalpf = 0., totalpts = 0. ;
	double totalfgm = 0., totalast = 0. ;
	for (int i = 0; i < 30; i++) {
	    teams[i].compute() ;
	    totalpace += teams[i].pace ;
	    totaloff += teams[i].off ;
	    totalreb += teams[i].reb ;
	    totalfgm += teams[i].fgm ;
	    totalftm += teams[i].ftm ;
	    totalfta += teams[i].fta ;
	    totalast += teams[i].ast ;
	    totalpf += teams[i].pf ;
	    totalgp += teams[i].gp ;
	    totalpts += teams[i].pts ;
	}
	Team.leaguePace = totalpace / 30. ;
	Team.lgdrb = 1. - totaloff / totalreb ;
	Team.lgftm = totalftm / totalpf ;
	Team.lgfta = totalfta / totalpf ;
	Team.vop = totalpts / totalgp / (240.*Team.leaguePace) ;
	Team.factor = 0.667 - (0.5*(totalast/totalfgm)) / 
	    (2.*(totalfgm/totalftm)) ;
	fileInfo("http://www.dougstats.com/09-10RD.txt") ;
	double totalper = 0., totalmn = 0. ;
	for (int i = 0; i < points; i++) {
	    players[i].compute() ;
	    totalper += players[i].per * players[i].mn ;
	    totalmn += players[i].mn ;
	}
	Player.weight = 15. / (totalper / totalmn) ;

	label = new Label("Position (1-5): ") ;
	endConst.gridx = 0 ;
	endConst.gridy = 0 ;
	gridbag.setConstraints(label,endConst) ;
	label.setBackground(backgrd) ;
	add(label) ;
	textField = new TextField(6) ;
	endConst.gridx = 1 ;
	endConst.gridy = 0 ;
	gridbag.setConstraints(textField,endConst) ;
	textField.setBackground(Color.white) ;
	textField.addTextListener(new txt()) ;
	add(textField) ;

	label2 = new Label("Minutes cutoff: ") ;
	endConst.gridx = 0 ;
	endConst.gridy = 1 ;
	gridbag.setConstraints(label2,endConst) ;
	label2.setBackground(backgrd) ;
	add(label2) ;
	textField2 = new TextField(6) ;
	endConst.gridx = 1 ;
	endConst.gridy = 1 ;
	gridbag.setConstraints(textField2,endConst) ;
	textField2.setBackground(Color.white) ;
	textField2.addTextListener(new txt()) ;
	add(textField2) ;

	label3 = new Label("Number to list: ") ;
	endConst.gridx = 0 ;
	endConst.gridy = 2 ;
	gridbag.setConstraints(label3,endConst) ;
	label3.setBackground(backgrd) ;
	add(label3) ;
	textField3 = new TextField(6) ;
	endConst.gridx = 1 ;
	endConst.gridy = 2 ;
	gridbag.setConstraints(textField3,endConst) ;
	textField3.setBackground(Color.white) ;
	textField3.addTextListener(new txt()) ;
	add(textField3) ;

	Panel p = new Panel() ;
	p.setBackground(backgrd) ;
	x1.addItemListener(new cb()) ;
	x2.addItemListener(new cb()) ;
	x3.addItemListener(new cb()) ;
	x4.addItemListener(new cb()) ;
	p.add(x1) ;
	p.add(x2) ;
	p.add(x3) ;
	p.add(x4) ;
	endConst.gridx = 0 ;
	endConst.gridy = 3 ;
	endConst.gridwidth = 2 ;
	gridbag.setConstraints(p,endConst) ;
	add(p) ;

	Panel p2 = new Panel() ;
	p2.setBackground(backgrd) ;
	b1.addActionListener(new but()) ;
	b2.addActionListener(new but()) ;
	b3.addActionListener(new but()) ;
	b4.addActionListener(new but()) ;
	b9.addActionListener(new but()) ;
	bc.addActionListener(new but()) ;
	p2.add(b1) ;
	p2.add(b2) ;
	p2.add(b3) ;
	p2.add(b4) ;
	p2.add(b9) ;
	p2.add(bc) ;
	endConst.gridx = 0 ;
	endConst.gridy = 4 ;
	gridbag.setConstraints(p2,endConst) ;
	add(p2) ;

	Panel p3 = new Panel() ;
	p3.setBackground(backgrd) ;
	bb.addActionListener(new but()) ;
	b5.addActionListener(new but()) ;
	b6.addActionListener(new but()) ;
	b7.addActionListener(new but()) ;
	b8.addActionListener(new but()) ;
	b0.addActionListener(new but()) ;
	p3.add(bb) ;
	p3.add(b5) ;
	p3.add(b6) ;
	p3.add(b7) ;
	p3.add(b8) ;
	if (standalone) p3.add(b0) ;
	endConst.gridx = 0 ;
	endConst.gridy = 5 ;
	gridbag.setConstraints(p3,endConst) ;
	add(p3) ;

	endConst.gridx = 0 ;
	endConst.gridy = 6 ;
	gridbag.setConstraints(playerList,endConst) ;
	playerList.setBackground(Color.yellow) ;
	playerList.setForeground(Color.blue) ;
	add(playerList) ;
    }

    class txt implements TextListener {
	public void textValueChanged (TextEvent e) {
	    startAction(last,divflag) ;
	}
    }

    class but implements ActionListener {
	public void actionPerformed (ActionEvent e) {
	    last = conv_b(e) ;
	    startAction(last,divflag) ;
	}
    }

    class cb implements ItemListener {
	public void itemStateChanged (ItemEvent e) {
	    divflag = conv_c(e) ;
	    startAction(last,divflag) ;
	}
    }

    public void getVals() {
	String s1,s2,s3 ;
	s1 = textField.getText() ;
	s2 = textField2.getText() ;
	s3 = textField3.getText() ;
	despos = (s1.length() > 0) ? Double.valueOf(s1).doubleValue() : 0 ;
	mincut = (s2.length() > 0) ? Double.valueOf(s2).doubleValue() : 0 ;
	desired = (s3.length() > 0) ? Double.valueOf(s3).doubleValue() : 0 ;
    }

    public int conv_b(ActionEvent e) {
	if (e.getActionCommand().equals("Close")) System.exit(0) ;
	if (e.getActionCommand().equals("PER")) return 1 ;
	if (e.getActionCommand().equals("Assists")) return 2 ;
	else if (e.getActionCommand().equals("Rebounds")) return 3 ;
	else if (e.getActionCommand().equals("SFO")) return 4 ;
	else if (e.getActionCommand().equals("3pt%")) return 5 ;
	else if (e.getActionCommand().equals("Steals")) return 6 ;
	else if (e.getActionCommand().equals("Blocks")) return 7 ;
	else if (e.getActionCommand().equals("PPR")) return 8 ;
	else if (e.getActionCommand().equals("Poss")) return 9 ;
	else if (e.getActionCommand().equals("TS%")) return 11 ;
	else if (e.getActionCommand().equals("FT")) return 12 ;
	else return 1;
    }

    public int conv_c(ItemEvent e) {
	if (e.getItem().equals("Per/min")) return 1 ;
	else if (e.getItem().equals("Per/48 min")) return 2 ;
	else if (e.getItem().equals("Per/game")) return 3 ;
	else if (e.getItem().equals("Totals")) return 4 ;
	else return 1 ;
    }

    public String parseString(BufferedReader contents, int digits) throws IOException {
	StringBuffer temp = new StringBuffer(0) ;
	for (int i = 0; i < digits; i++)
	    temp.append((char)contents.read()) ;
	return temp.toString() ;
    }

    public double parseValue(BufferedReader contents, int digits) throws IOException {
	StringBuffer temp = new StringBuffer(0) ;
	for (int i = 0; i < digits; i++)
	    temp.append((char)contents.read()) ;
	return Double.valueOf(temp.toString()).doubleValue() ;
    }

    public void fileInfo(String file_name) {

	try {
	    URL u = new URL(file_name) ;
	    URLConnection urlc = u.openConnection() ;
	    urlc.setDoInput(true) ;
	    urlc.setAllowUserInteraction(false) ;

	    BufferedReader contents = new BufferedReader(new InputStreamReader(urlc.getInputStream())) ;

	    StringBuffer temp = new StringBuffer(0) ;
	    StringBuffer temp2 = new StringBuffer(0) ;

	    contents.readLine() ;

	    while (true) {
		Player newplay = new Player() ;
		newplay.name = parseString(contents, 18) ;
		String team = parseString(contents, 3) ;
		String pos = parseString(contents, 3) ;
		if (pos.equals(" PG")) newplay.pos = 1 ;
		if (pos.equals(" SG")) newplay.pos = 2 ;
		if (pos.equals(" SF")) newplay.pos = 3 ;
		if (pos.equals(" PF")) newplay.pos = 4 ;
		if (pos.equals("  C")) newplay.pos = 5 ;
		newplay.gp = parseValue(contents, 3) ;
		newplay.mn = parseValue(contents, 5) ;
		newplay.fgm = parseValue(contents, 5) ;
		newplay.fga = parseValue(contents, 5) ;
		newplay.fgmsd = newplay.fga - newplay.fgm ;
		newplay.treym = parseValue(contents, 4) ;
		newplay.treya = parseValue(contents, 4) ;
		newplay.ftm = parseValue(contents, 4) ;
		newplay.fta = parseValue(contents, 5) ;
		newplay.ftmsd = newplay.fta - newplay.fta ;
		newplay.off = parseValue(contents, 4) ;
		newplay.reb = parseValue(contents, 5) ;
		newplay.ast = parseValue(contents, 5) ;
		newplay.stl = parseValue(contents, 4) ;
		newplay.to = parseValue(contents, 4) ;
		newplay.blk = parseValue(contents, 4) ;
		newplay.pf = parseValue(contents, 4) ;
		parseValue(contents, 4) ;
		newplay.pts = parseValue(contents, 6) ;
		contents.readLine() ;
		for (int i = 0; i < 30; i++)
		    if (team.equals(teams[i].name)) {
			newplay.team = teams[i] ;
			players[points++] = newplay ;
		    }
		// if a player doesn't get added the team field was messed up
	    }
	} catch (IOException ioe) { System.out.println(ioe) ; }
	catch (NumberFormatException npe) { System.out.println("Done reading " + points + " players") ; }
    }
    
    public void teamInfo(String file_name, String other_name) {

	String endofline ;

	try {
	    URL u = new URL(file_name) ;
	    URLConnection urlc = u.openConnection() ;
	    urlc.setDoInput(true) ;
	    urlc.setAllowUserInteraction(false) ;
	    BufferedReader contents=new BufferedReader(new InputStreamReader(urlc.getInputStream())) ;
	    contents.readLine() ;

	    URL u2 = new URL(other_name) ;
	    URLConnection urlc2 = u2.openConnection() ;
	    urlc2.setDoInput(true) ;
	    urlc2.setAllowUserInteraction(false) ;
	    BufferedReader contents2=new BufferedReader(new InputStreamReader(urlc2.getInputStream())) ;
	    contents2.readLine() ;

	    for (int i = 0; i < 30; i++) {
		Team newteam=new Team() ;
		String name = parseString(contents, 21) ; // real name
		newteam.name = teamNames[i] ;
		newteam.gp = parseValue(contents, 2) + parseValue(contents, 5) ;
		newteam.mn = parseValue(contents, 6) ;
		newteam.fgm = parseValue(contents, 5) ;
		newteam.fga = parseValue(contents, 5) ;
		parseValue(contents, 5) ; // tpm
		parseValue(contents, 5) ; // tpa
		newteam.ftm = parseValue(contents, 5) ;
		newteam.fta = parseValue(contents, 5) ;
		newteam.off = parseValue(contents, 5) ;
		newteam.reb = parseValue(contents, 5) ;
		newteam.ast = parseValue(contents, 5) ;
		parseValue(contents, 5) ; // stl
		newteam.to = parseValue(contents, 5) ;
		parseValue(contents, 5) ; // blk
		newteam.pf = parseValue(contents, 5) ;
		newteam.pts = parseValue(contents, 6) ;
		contents.readLine() ;

		parseString(contents2, 21) ;
		parseValue(contents2, 2) ;
		parseValue(contents2, 5) ;
		parseValue(contents2, 6) ;
		parseValue(contents2, 5) ;
		newteam.fga_o = parseValue(contents2, 5) ;
		parseValue(contents2, 5) ;
		parseValue(contents2, 5) ;
		parseValue(contents2, 5) ;
		newteam.fta_o = parseValue(contents2, 5) ;
		newteam.off_o = parseValue(contents2, 5) ;
		parseValue(contents2, 5) ;
		parseValue(contents2, 5) ;
		parseValue(contents2, 5) ;
		newteam.to_o = parseValue(contents2, 5) ;
		contents2.readLine() ;

		teams[i] = newteam ;
	    }
	} catch (IOException ioe) { System.out.println(ioe) ; }
    }
    
    void sort (int sorttype, int divflag) {
	int i,j ;
	
	listnum=points ;
	for (i=0;i<listnum;i++) sc[i]=(double)1. ;
	if (despos!=0) {
	    for (i=0;i<points-1;i++) for (j=i+1;j<points;j++)
		if ((players[i].pos != despos) && (players[j].pos == despos))swap(i,j) ;
	    for (i=0;i<points-1;i++) if (players[i].pos != despos) {
		listnum=i ;
		break ;
	    }
	}
	
	for (i=0;i<listnum-1;i++) for (j=i+1;j<listnum;j++)
	    if ((players[i].mn < mincut) && (players[j].mn >= mincut)) swap(i,j) ;
	for (i=0;i<listnum-1;i++) if (players[i].mn < mincut) {
	    listnum=i ;
	    break ;
	}
	
	if (sorttype==5) {
	    for (i=0;i<listnum-1;i++) for (j=i+1;j<listnum;j++)
		if ((players[i].treym < 10) && (players[j].treym >= 10)) swap(i,j) ;
	    for (i=0;i<listnum-1;i++) if (players[i].treym < 10) {
		listnum=i ;
		break ;
	    }
	}

	if (sorttype==12) {
	    for (i=0;i<listnum-1;i++) for (j=i+1;j<listnum;j++)
		if ((players[i].sfo < 10.) && (players[j].sfo >= 10.)) swap(i,j) ;
	    for (i=0;i<listnum-1;i++) if (players[i].sfo < 10.) {
		listnum=i ;
		break ;
	    }
	}
	
	if (divflag==4) for (i=0;i<listnum;i++) sc[i]=players[i].mn ;
	if (divflag==3) for (i=0;i<listnum;i++) sc[i]=players[i].mn/players[i].gp ;
	if (divflag==2) for (i=0;i<listnum;i++) sc[i]=(double)48. ;
	for (i=0;i<listnum;i++) switch (sorttype) {
	case 1: 
	    players[i].disp=players[i].per*Player.weight ; break ;
	case 2: 
	    players[i].disp=players[i].ast/players[i].mn*sc[i] ; break ;
	case 3: 
	    players[i].disp=players[i].reb/players[i].mn*sc[i] ; break ;
	case 4: 
	    players[i].disp=players[i].sfo ; break ;
	case 5: 
	    players[i].disp=players[i].treym/players[i].treya ; break ;
	case 6: 
	    players[i].disp=players[i].stl/players[i].mn*sc[i] ; break ;
	case 7: 
	    players[i].disp=players[i].blk/players[i].mn*sc[i] ; break ;
	case 8: 
	    players[i].disp=(2./3.*players[i].ast - players[i].to)*100/players[i].mn ; break ;
	case 9: 
	    players[i].disp=players[i].poss/players[i].mn*sc[i] ; break ;
	case 11: 
	    players[i].disp=0.5*players[i].pts/(players[i].fga + 0.44*players[i].fta) ; break ;
	case 12: 
	    players[i].disp=(0.44*players[i].fta)/(players[i].fga + 0.44*players[i].fta) ; break ;
	}
	
	for (i=0;i<listnum-1;i++) for (j=i+1;j<listnum;j++) if (players[i].disp < players[j].disp) swap(i,j) ;
    }
    
    void swap(int i, int j) {
	Player t = new Player() ;
	t = players[i] ;
	players[i] = players[j] ;
	players[j] = t ;
	double t2 ;
	t2 = sc[i] ;
	sc[i] = sc[j] ;
	sc[j] = t2 ;
    }
    
    void processPlayer(String name) {
	startAction(1,1) ;
	for (int i=0;i<points;i++) {
	    if (players[i].name.startsWith(name)) {
		System.out.println(form.format(players[i].per*Player.weight) + " " + name) ;
		break ;
	    }
	}
    }

    public static void main(String args[]) {
	bball ps = new bball() ;
	standalone = true ;
	ps.init() ;

	if (args.length == 1) {
	    ps.processPlayer(args[0]) ;
	    System.exit(0) ;
	}
	
	Frame f = new Frame("Basketball Computations") ;
	f.setSize(400,750) ;
	f.add("Center",ps) ;
	f.show() ;
    }
}
