/***
 * Excerpted from "Domain-Driven Design Using Naked Objects",
 * published by The Pragmatic Bookshelf.
 * Copyrights apply to this code. It may not be used to create training material, 
 * courses, books, articles, and the like. Contact us if you are in doubt.
 * We make no guarantees that this code is fit for any purpose. 
 * Visit http://www.pragmaticprogrammer.com/titles/dhnako for more book information.
***/
package com.pragprog.dhnako.carserv.restful;

import static org.starobjects.restful.applib.XomUtils.getAttributeValue;
import static org.starobjects.restful.applib.XomUtils.prettyPrint;
import nu.xom.Document;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.Options;
import org.apache.log4j.BasicConfigurator;
import org.starobjects.restful.applib.AbstractRestfulClient;

import com.pragprog.dhnako.carserv.restful.CmdLineUtil.Optionality;

public class FindCustomer extends AbstractRestfulClient {

    private static final String DEFAULT_HOST_URI = "http://localhost:7070";
    private static final String DEFAULT_CUSTOMER_NAME = "Joe Bloggs";

    public static void main(String[] args) throws Exception {

        BasicConfigurator.configure();
        
        Options options = new Options();
        CmdLineUtil.addOption(options, 
                "h", "host-uri", true, "host URI, eg " + DEFAULT_HOST_URI, 
                Optionality.OPTIONAL);
        CmdLineUtil.addOption(options, 
                "n", "name", true, 
                "first and last name of customer to search for, eg '" + DEFAULT_CUSTOMER_NAME + "'", 
                Optionality.OPTIONAL);

        CommandLine parse = CmdLineUtil.parse(FindCustomer.class.getSimpleName(), options, args);
        String hostUri = parse.getOptionValue("h", DEFAULT_HOST_URI);
        String customerName = parse.getOptionValue("n", DEFAULT_CUSTOMER_NAME);
        String[] names=customerName.split("\\W+");
        if (names.length != 2) {
            System.err.println("Name must be in form: 'FirstName LastName'");
            return;
        }
        
        FindCustomer client = new FindCustomer(hostUri);
        client.findCustomer(names[1], names[0]);
    }


    public FindCustomer(String hostUri) {
        super(hostUri);
    }

    private void findCustomer(
            final String lastName, 
            final String firstName) throws Exception {


        Document customersDoc = get(
            combine(getHostUri(),"/services"));
        prettyPrint(customersDoc);

        

        String customersServicePath = getAttributeValueElseException(
                 customersDoc, 
                "//a[@class='nof-service'][text()='Customers']/@href", 
                "Unable to find 'Customers' service");

        

        Document customerFindByNameDoc = post(
            combine(
                getHostUri(), customersServicePath, 
                "/action/findByName(java.lang.String,java.lang.String)"), 
                "arg0", lastName, 
                "arg1", firstName);
        prettyPrint(customerFindByNameDoc);



        String customerPath = getAttributeValueElseException(
                customerFindByNameDoc, 
                "//a[@class='nof-action-result']/@href", 
                "No Customer found");

        

        Document customerDoc = get(
                combine(getHostUri(), customerPath));
        prettyPrint(customerDoc);

    }

}
