package lemming.resource;

import lemming.context.Context;
import lemming.context.ContextType;
import lemming.lemma.Lemma;
import lemming.pos.Pos;

import javax.xml.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;

/**
 * A helper class with Java objects to build a KWIC index with JAXB.
 */
public abstract class KwicIndex {
    /**
     * Sublist of element kwiclist.
     */
    @XmlRootElement(name = "sublist")
    public static class SubList {
        /**
         * Key attribute of a sublist element.
         */
        @XmlAttribute
        private String key;

        /**
         * Items of a sublist.
         */
        @XmlElement(name = "item")
        private List<Item> items;

        /**
         * Creates a sublist. Needed for JAXB.
         */
        public SubList() {}

        /**
         * Creates a sublist.
         *
         * @param key key attribute
         */
        public SubList(String key) {
            this.key = key;
            items = new ArrayList<Item>();
        }

        /**
         * Add a context as item to the sublist.
         *
         * @param context a context
         */
        public void addContext(Context context) {
            getItems().add(new Item(context));
        }

        /**
         * Returns the key attribute of a sublist element.
         *
         * @return A key attribute as string.
         */
        public String getKey() {
            return key;
        }

        /**
         * Returns items of a sublist.
         *
         * @return A list of items.
         */
        public List<Item> getItems() {
            return items;
        }
    }

    /**
     * Item of a sublist element.
     */
    @XmlAccessorType(XmlAccessType.FIELD)
    public static class Item {
        /**
         * Creates an item.
         *
         * @param context a context
         */
        public Item(Context context) {
            following = context.getFollowing();
            keyword = context.getKeyword();
            location = context.getLocation();
            preceding = context.getPreceding();

            if (context.getLemma() instanceof Lemma) {
                lemma = context.getLemma().getName();
            }

            if (context.getPos() instanceof Pos) {
                pos = context.getPos().getName();
            }

            if (context.getType().equals(ContextType.Type.RUBRIC)) {
                type = "rubric_item";
            } else if (context.getType().equals(ContextType.Type.SEGMENT)) {
                type = "seg_item";
            }
        }

        /**
         * Following attribute of an index.
         */
        @XmlAttribute(required = true)
        private String following;

        /**
         * Keyword value of an index.
         */
        @XmlValue
        private String keyword;

        /**
         * Lemma attribute of an index.
         */
        @XmlAttribute
        private String lemma;

        /**
         * Location attribute of an index.
         */
        @XmlAttribute(required = true)
        private String location;

        /**
         * Pos attribute of an index.
         */
        @XmlAttribute
        private String pos;

        /**
         * Preceding attribute of an index.
         */
        @XmlAttribute(required = true)
        private String preceding;

        /**
         * Type attribute of an index.
         */
        @XmlAttribute(required = true)
        private String type;

        /**
         * Returns the following attribute of an index.
         *
         * @return An attribute as string.
         */
        public String getFollowing() {
            return following;
        }

        /**
         * Returns the keyword attribute of an index.
         *
         * @return An attribute as string.
         */
        public String getKeyword() {
            return keyword;
        }

        /**
         * Returns the lemma attribute of an index.
         *
         * @return An attribute as string.
         */
        public String getLemma() {
            return lemma;
        }

        /**
         * Returns the location attribute of an index.
         *
         * @return An attribute as string.
         */
        public String getLocation() {
            return location;
        }

        /**
         * Returns the pos attribute of an index.
         *
         * @return An attribute as string.
         */
        public String getPos() {
            return pos;
        }

        /**
         * Returns the preceding attribute of an index.
         *
         * @return An attribute as string.
         */
        public String getPreceding() {
            return preceding;
        }

        /**
         * Returns the type attribute of an index.
         *
         * @return An attribute as string.
         */
        public String getType() {
            return type;
        }
    }
}

@@ -286,20 +286,19 @@
 a KWIC i
-ndex
+tem
  with JA
@@ -2632,36 +2632,35 @@
 ttribute of an i
-ndex
+tem
 .%0A         */%0A  
@@ -2767,36 +2767,35 @@
 rd value of an i
-ndex
+tem
 .%0A         */%0A  
@@ -2881,36 +2881,35 @@
 ttribute of an i
-ndex
+tem
 .%0A         */%0A  
@@ -3000,36 +3000,35 @@
 ttribute of an i
-ndex
+tem
 .%0A         */%0A  
@@ -3134,36 +3134,35 @@
 ttribute of an i
-ndex
+tem
 .%0A         */%0A  
@@ -3252,36 +3252,35 @@
 ttribute of an i
-ndex
+tem
 .%0A         */%0A  
@@ -3388,36 +3388,35 @@
 ttribute of an i
-ndex
+tem
 .%0A         */%0A  
@@ -3536,36 +3536,35 @@
 ttribute of an i
-ndex
+tem
 .%0A         *%0A   
@@ -3747,36 +3747,35 @@
 ttribute of an i
-ndex
+tem
 .%0A         *%0A   
@@ -3952,36 +3952,35 @@
 ttribute of an i
-ndex
+tem
 .%0A         *%0A   
@@ -4156,36 +4156,35 @@
 ttribute of an i
-ndex
+tem
 .%0A         *%0A   
@@ -4361,36 +4361,35 @@
 ttribute of an i
-ndex
+tem
 .%0A         *%0A   
@@ -4562,36 +4562,35 @@
 ttribute of an i
-ndex
+tem
 .%0A         *%0A   
@@ -4778,20 +4778,19 @@
  of an i
-ndex
+tem
 .%0A      

Fix typos in class KwicIndex.