import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.InetAddress;
import java.net.Socket;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class RankMe extends JFrame
  implements ActionListener
{
  public JPanel p = new JPanel(new FlowLayout());
  public JButton b1;
  public JButton b2;
  public Socket sock;
  public DataInputStream dis;
  public OutputStream os;

  public RankMe()
  {
    super("RankMe");
    setSize(600, 350);
    setContentPane(this.p);
    setLocation(200, 200);
    setDefaultCloseOperation(3);
  }

  public void update(byte[] paramArrayOfByte) {
    int i = paramArrayOfByte.length;
    for (int j = 0; j < i; ++j)
    {
      int tmp12_11 = j;
      byte[] tmp12_10 = paramArrayOfByte; tmp12_10[tmp12_11] = (byte)(tmp12_10[tmp12_11] ^ (byte)i);
    }
  }

  public int f(int paramInt) {
    return (paramInt ^ 0xC932F768);
  }

  public int getData() throws Exception {
    this.p.removeAll();
    int i = f(this.dis.readInt());
    if (i < 0) return i;
    byte[] arrayOfByte = new byte[i];
    this.dis.readFully(arrayOfByte);
    update(arrayOfByte);
    ImageIcon localImageIcon = new ImageIcon(arrayOfByte);
    this.b1 = new JButton(localImageIcon);
    this.b1.addActionListener(this);
    i = f(this.dis.readInt());
    arrayOfByte = new byte[i];
    this.dis.readFully(arrayOfByte);
    update(arrayOfByte);
    localImageIcon = new ImageIcon(arrayOfByte);
    this.b2 = new JButton(localImageIcon);
    this.b2.addActionListener(this);
    this.p.add(this.b1);
    this.p.add(this.b2);
    this.p.validate();
    this.p.repaint();
    return 0;
  }

  public void play(String paramString) throws Exception {
    this.sock = new Socket(paramString, 8129);
    InputStream localInputStream = this.sock.getInputStream();
    this.dis = new DataInputStream(localInputStream);
    this.os = this.sock.getOutputStream();
    this.os.write("illogical\n".getBytes());
    if (getData() == 0)
      setVisible(true);
  }

  public void actionPerformed(ActionEvent paramActionEvent)
  {
    JButton localJButton = (JButton)paramActionEvent.getSource();
    int i = -1;
    if (localJButton == this.b1)
      try {
        this.os.write(0);
        i = getData();
      }
      catch (Exception localException1) {
        setVisible(false);
        try {
          this.sock.close();
        }
        catch (Exception localException4) {
        }
      }
    else if (localJButton == this.b2)
      try {
        this.os.write(1);
        i = getData();
      }
      catch (Exception localException2) {
        setVisible(false);
        try {
          this.sock.close();
        }
        catch (Exception localException5) {
        }
      }
    if (i < 0) {
      switch (i)
      {
      case -2:
      case -1:
        JOptionPane.showMessageDialog(this, "Better luck next time");
        break;
      case -3:
        System.err.println();
        String str = JOptionPane.showInputDialog(this, "What sequence did you follow to get here? Enter as a string of L/R.");
        if (str != null)
          try {
            this.os.write(str.getBytes());
          } catch (Exception localException6) {
            return;
          }
        try
        {
          i = getData();
        } catch (Exception localException7) {
          return;
        }
        if (i == -5) {
          JOptionPane.showMessageDialog(this, "Better luck next time");
        }
        else if (i == -6) {
          JOptionPane.showMessageDialog(this, "Too slow. Better luck next time");
        }
        break;
      case -4:
        try
        {
          int j = this.dis.readInt();
          byte[] arrayOfByte = new byte[j];
          this.dis.readFully(arrayOfByte);
          JOptionPane.showMessageDialog(this, "Answer: " + new String(arrayOfByte), "Congratulations", -1);
          System.err.println(new String(arrayOfByte));
        } catch (Exception localException3) {
          setVisible(false);
          try {
            this.sock.close();
          }
          catch (Exception localException8)
          {
          }
        }
      }

      setVisible(false);
      System.exit(0);
    }
  }

  public static void usage() {
    System.err.println("java -jar t400_49d12929d2eeb6df.jar <host>");
    System.exit(1);
  }

  public static void main(String[] paramArrayOfString) throws Exception {
    if ((paramArrayOfString.length != 1) || (paramArrayOfString[0].startsWith("-"))) {
      usage();
    }
    else {
      try {
        InetAddress localInetAddress = InetAddress.getByName(paramArrayOfString[0]);
      } catch (Exception localException) {
        System.err.println("Invalid server address");
        usage();
      }
      RankMe localRankMe = new RankMe();
      localRankMe.play(paramArrayOfString[0]);
    }
  }
}
