package com.ul.deliveryclient;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.ObjectMessage;
import javax.jms.Session;
import javax.naming.Context;
import javax.naming.InitialContext;

/**
 * Enterprise Application Client main class.
 *
 */
public class Main {
    
    private static List<Long> unprocessedRequests = new ArrayList<>();
    
    public static void main( String[] args ) {
        
//        // check that four numbers are given as parameter
//        if(args.length<4) {
//            System.out.println("Usage : "+args[0]+" reportId, weight, height, width");
//            System.exit(1);
//        }
//        
//        long[] values=new long[4];
//        
//        for(int i=1;i<args.length;i++)
//            values[i-1]=Long.parseLong(args[i]);

        Scanner scanner = new Scanner(System.in);
        
        try {
            // get the required information from the context

            Context context=new InitialContext();
            ConnectionFactory connectionFactory=(ConnectionFactory) context.lookup("jms/QueueConnectionFactory");
            Destination replyQueue = (Destination)context.lookup("DeliveryReplyQueue");
            Destination requestQueue = (Destination)context.lookup("DeliveryRequestQueue");

            // create the connection

            Connection connection = connectionFactory.createConnection();
            Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            MessageProducer messageProducer = session.createProducer(replyQueue);
            MessageConsumer messageConsumer = session.createConsumer(requestQueue);
            messageConsumer.setMessageListener(new DeliveryRequestMessageListener());
            connection.start();
            System.out.println("");
            System.out.println("");
            System.out.println("===============================================================================");
            System.out.println("     INSTRUCTIONS:");
            System.out.println("     The client will now start listening for delivery requests");
            System.out.println("     At any time, enter -1           for a list of unprocessed requests");
            System.out.println("                  enter 0            to exit");
            System.out.println("                  enter the order id to confirm the delivery");
            System.out.println("===============================================================================");
            System.out.println("");
            System.out.println("");
            
            while(true){
                System.out.print("Enter command: ");
                long input = scanner.nextLong();
                if(input < 0) {
                    printUnprocessedRequests();
                    System.out.println("===============================================================================");
                } else if (input == 0) {
                    break;
                } else {
                    if(!hasRequest(input)) {
                        System.out.println("");
                        System.out.println("The ID does not seem to correspond to an active request. Enter -1 to get list of requests.");
                        System.out.println("===============================================================================");
                    } else {        
                        // create and send the message
                        ObjectMessage message = session.createObjectMessage();
                        message.setObject(input);
                        messageProducer.send(message);
                        removeRequest(input);
                        System.out.println("");
                        System.out.println("Confirmation message sent for the bill of order with id: " + input);
                        printUnprocessedRequests();
                        System.out.println("===============================================================================");
                    }
                }
            }
            session.close();
            connection.close();
            connection = null;
        } catch (Exception e) {
            e.printStackTrace();
        }
            
        
    }
    
    public static void printUnprocessedRequests() {
        if (unprocessedRequests.isEmpty()) {
            System.out.println("");
            System.out.println("No unprocessed requests");
        } else {
            System.out.println("");
            System.out.println("Unprocessed request IDs: ");
            for(Long element : unprocessedRequests) {
                System.out.println(element.toString());
            }
        }
    }
    
    public static void addRequest(Long id) {
        unprocessedRequests.add(id);
    }
    
    public static void removeRequest(Long id) {
        unprocessedRequests.remove(id);
    }
    
    public static boolean hasRequest(Long id) {
        return unprocessedRequests.contains(id);
    }
    
}
