package lemming.context.inbound;

import lemming.context.Context;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.model.IModel;

import javax.ws.rs.core.MultivaluedMap;
import java.util.ArrayList;
import java.util.List;

/**
 * A form for the verification of inbound contexts.
 */
class InboundContextVerificationForm extends Form<InboundContextPackage> {
    /**
     * Creates a context verify form.
     *
     * @param model model of a group of inbound contexts
     */
    public InboundContextVerificationForm(IModel<InboundContextPackage> model) {
        super("InboundContextVerificationForm", model);
        add(new SubmitButton(this));
    }

    private class SubmitButton extends AjaxButton {

        public SubmitButton(Form<InboundContextPackage> form) {
            super("submitButton", form);
        }

        /**
         * Match contexts by hash.
         */
        private void matchContextsByHash() {
            InboundContextPackage contextPackage = (InboundContextPackage) getForm().getModelObject();
            new InboundContextPackageDao().matchContextsByHash(contextPackage);
        }

        /**
         * Finds and sets locations with unmatched contexts.
         *
         * @param contextPackage package of inbound contexts
         */
        private List<String> findUnmatchedLocations(InboundContextPackage contextPackage) {
            return new InboundContextPackageDao().findUnmatchedContextLocations(contextPackage);
        }

        /**
         * Computes matching triples for a location.
         *
         * @param contextPackage package of inbound contexts
         * @param location location of contexts
         * @return A list of matching triples.
         */
        private List<Triple> computeMatchingTriplets(InboundContextPackage contextPackage, String location) {
            InboundContextPackageDao inboundContextPackageDao = new InboundContextPackageDao();
            MultivaluedMap<Integer, InboundContext> groupedContexts = inboundContextPackageDao
                    .groupUnmatchedContexts(contextPackage, location);
            List<Triple> allMatchingTriples = new ArrayList<>();

            for (Integer key : groupedContexts.keySet()) {
                List<InboundContext> contexts = groupedContexts.get(key);
                List<Context> complements = new InboundContextDao().findComplements(contexts);

                if (complements != null) {
                    MultivaluedMap<Integer, Triple> tripleMap = MatchHelper
                            .getTriples(contexts, complements);
                    List<Triple> matchingTriples = MatchHelper.computeMatchingTriples(tripleMap);
                    allMatchingTriples.addAll(MatchHelper.sortTriples(matchingTriples));
                }
            }

            return allMatchingTriples;
        }

        /**
         * Called on form submit.
         *
         * @param target target that produces an Ajax response
         * @param form the parent form
         */
        @Override
        protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
            super.onSubmit(target, form);
            InboundContextPackage contextPackage = (InboundContextPackage) getForm().getModelObject();

            matchContextsByHash();
            List<String> unmatchedLocations = findUnmatchedLocations(contextPackage);

            for (String location : unmatchedLocations) {
                List<Triple> matchingTriples = computeMatchingTriplets(contextPackage, location);
            }
        }
    }
}

@@ -1661,23 +1661,31 @@
  for a l
-ocation
+ist of contexts
 .%0A      
@@ -1718,79 +1718,16 @@
 text
-Package package of inbound contexts%0A         * @param location location
+s a list
  of 
@@ -1851,530 +1851,41 @@
 ets(
-InboundContextPackage contextPackage, String location) %7B%0A            InboundContextPackageDao inboundContextPackageDao = new InboundContextPackageDao();%0A            MultivaluedMap%3CInteger, InboundContext%3E groupedContexts = inboundContextPackageDao%0A                    .groupUnmatchedContexts(contextPackage, location);%0A            List%3CTriple%3E allMatchingTriples = new ArrayList%3C%3E();%0A%0A            for (Integer key : groupedContexts.keySet()) %7B%0A                List%3CInboundContext%3E contexts = groupedContexts.get(key);%0A    
+List%3CInboundContext%3E contexts) %7B%0A
     
@@ -1968,28 +1968,24 @@
 contexts);%0A%0A
-    
             
@@ -2027,20 +2027,16 @@
         
-    
 Multival
@@ -2086,37 +2086,8 @@
 lper
-%0A                            
 .get
@@ -2138,42 +2138,14 @@
     
-    List%3CTriple%3E matchingTriples =
+return
  Mat
@@ -2192,115 +2192,8 @@
 p);%0A
-                    allMatchingTriples.addAll(MatchHelper.sortTriples(matchingTriples));%0A                %7D%0A
     
@@ -2226,26 +2226,25 @@
 urn 
-allMatchingTriples
+new ArrayList%3C%3E()
 ;%0A  
@@ -2836,24 +2836,350 @@
 ocations) %7B%0A
+                MultivaluedMap%3CInteger, InboundContext%3E groupedContexts = new InboundContextPackageDao()%0A                        .groupUnmatchedContexts(contextPackage, location);%0A%0A                for (Integer key : groupedContexts.keySet()) %7B%0A                    List%3CInboundContext%3E contexts = groupedContexts.get(key);%0A    
             
@@ -3244,35 +3244,37 @@
 (context
-Package, location);
+s);%0A                %7D
 %0A       

Simplify code in InboundContextVerificationForm.