import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * This class contains the sample code to post a Wasabi assignment, passing profile data to be used in evaluating a Segmentation rule.
 * This assumes an experiment that has a rule like "salary is greater than 10000".
 */
public class AssignmentWithSegmentation {

  public static void main(String[] args) throws IOException, ParseException {
    System.out.print(postAssignmentWithSegmentation("{{experiment.applicationName}}", "{{experiment.label}}", "UserName", "10001"));
  }


  /**
   * This method calls the Wasabi Api to retrieve an assignment for the specified experiment and user passing profile data
   * for use in evaluating the segmentation rule.
   *
   * @param application the ApplicationName the experiment is running in
   * @param experiment  the name of the Experiment
   * @param user        the user for whom the Assignment should be posted
   * @param salary      the salary of the user, to be used in evaluating a segmentation rule
   * @return true if the assignment was recorded false otherwise
   * @throws IOException
   */
  private static Object postAssignmentWithSegmentation(String application, String experiment, String user, String salary) throws IOException, ParseException {

    String urlAssignmentWithSegmentation = String.format("{{baseUrl}}/assignments/applications/%s/experiments/%s/users/%s", application, experiment, user);

    URL url = new URL(urlAssignmentWithSegmentation);
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setRequestProperty("content-type", "application/json");
    connection.setRequestMethod("POST");
    connection.setDoOutput(true);

    OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
    // This example assumes only one parameter in the rule.  If you had more, you'd pass them as other attributes of the "profile".
    writer.write("{\"profile\":{\"salary\":\"" + salary + "\"}}");
    writer.flush();

    // give it 500 milliseconds to respond
    connection.setReadTimeout(500);
    connection.connect();
    
    // read the output from the server
    BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
    
    /*
    The response from the server is a json with the following exemplary entries:
     {"cache":true,
     "payload":null,
     "assignment":"b",
     "context":"PROD",
     "status":"EXISTING_ASSIGNMENT"}
     */
    String line = reader.readLine();

    JSONParser parser = new JSONParser();
    JSONObject assignment = (JSONObject) parser.parse(line);

    return assignment.get("assignment"); // if you are just interested in the assignment take the 'assignment' field
  }

}