package us.semanter.app.ui;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;

import java.util.HashSet;
import java.util.Set;

import us.semanter.app.R;
import us.semanter.app.model.Tag;

public class TagView extends LinearLayout implements TagEditor.TagListener {
    private Context context;
    private LinearLayout tagLayout;
    private TagEditor editorView;

    private Set<Tag> tags;

    public TagView(Context context, AttributeSet attrs) {
        super(context, attrs);

        this.context = context;

        // UI

        setOrientation(LinearLayout.HORIZONTAL);

        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.view_tag, this, true);

        tagLayout = (LinearLayout)findViewById(R.id.tag_selector_scroll_layout);
        editorView = (TagEditor)findViewById(R.id.tag_editor_text);
        editorView.registerListener(this);

        // Data

        tags = new HashSet<Tag>();
    }

    public TagView(Context context) {
        this(context, null);
    }

    private void addTag(Tag t) throws TagExistsException {
        // ui
        LayoutInflater inflater = LayoutInflater.from(context);
        Button button = (Button) inflater.inflate(R.layout.tag, null, false);

        button.setText(t.getValue());
        tagLayout.addView(button, 0);

        // data
        if(!tags.add(t))
            throw new TagExistsException();
    }

    public void onNewTag(Tag newTag) {
        Log.d("TagView", newTag.getValue());

        try {
            addTag(newTag);
        } catch(TagExistsException e) {
            Toast.makeText(context, "tag already added", Toast.LENGTH_SHORT);
        }
    }

    public Set getTags() {
        return tags;
    }

    static class TagExistsException extends Exception {
        public TagExistsException() {
            super();
        }
    }
}

@@ -149,16 +149,42 @@
 flater;%0A
+import android.view.View;%0A
 import a
@@ -267,24 +267,50 @@
 get.Toast;%0A%0A
+import java.util.HashMap;%0A
 import java.
@@ -319,24 +319,46 @@
 il.HashSet;%0A
+import java.util.Map;%0A
 import java.
@@ -632,16 +632,57 @@
 g%3E tags;
+%0A    private Map%3CTag, Button%3E tagButtons;
 %0A%0A    pu
@@ -1289,24 +1289,73 @@
 Set%3CTag%3E();%0A
+        tagButtons = new HashMap%3CTag, Button%3E();%0A
     %7D%0A%0A    p
@@ -1451,13 +1451,21 @@
 Tag(
+final 
 Tag t
+ag
 ) th
@@ -1671,16 +1671,18 @@
 etText(t
+ag
 .getValu
@@ -1687,16 +1687,197 @@
 lue());%0A
+        button.setOnClickListener(new OnClickListener() %7B%0A            @Override%0A            public void onClick(View v) %7B%0A                removeTag(tag);%0A            %7D%0A        %7D);%0A%0A
         
@@ -1923,16 +1923,53 @@
 // data%0A
+        tagButtons.put(tag, button);%0A
         
@@ -1982,16 +1982,18 @@
 gs.add(t
+ag
 ))%0A     
@@ -2030,32 +2030,167 @@
 ption();%0A    %7D%0A%0A
+    private synchronized void removeTag(Tag tag) %7B%0A        tags.remove(tag);%0A        tagLayout.removeView(tagButtons.get(tag));%0A    %7D%0A%0A
     public void 

Remove tags by clicking them.