package de.plastickarma.readlater.app;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;


/**
 * Main Activity to add a bookmark.
 */
public final class ReadLaterMain extends ActionBarActivity {

    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_read_later_main);

        final EditText titleTextInput = (EditText) findViewById(R.id.bookmarkTitleInput);
        final EditText urlTextInput = (EditText) findViewById(R.id.bookmarkURLInput);
        final EditText descriptionTextInput = (EditText) findViewById(R.id.bookmarkDescriptionInput);
        final EditText categoriesTextInput = (EditText) findViewById(R.id.bookmarkCategoriesInput);

        // Try to fill in the text fields if we come from a share intent.
        final Intent intent = this.getIntent();
        final String action = intent.getAction();
        if (action != null && action.equals(Intent.ACTION_SEND)) {
            final String text = intent.getStringExtra(Intent.EXTRA_TEXT);
            final Bookmark bookmark = BookmarkExtractor.extractBookmark(text);
            if (bookmark == null) {
                urlTextInput.setText(text);
            } else {
                titleTextInput.setText(bookmark.getDescription());
                urlTextInput.setText(bookmark.getUrl());
            }
        }
        categoriesTextInput.setText(Settings.getOwncloudDefaultCategory(this));

        final Button saveButton = (Button) findViewById(R.id.saveButton);
        saveButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View v) {
                Bookmark bm = new Bookmark(
                        ActivityHelper.getNullCheckedText(urlTextInput),
                        ActivityHelper.getNullCheckedText(titleTextInput),
                        ActivityHelper.getNullCheckedText(descriptionTextInput),
                        ActivityHelper.getNullCheckedText(categoriesTextInput));
                BookmarkCreator.createRemoteBookmark(
                        v.getContext(),
                        Settings.getOwncloudURL(v.getContext()),
                        Settings.getOwncloudUser(v.getContext()),
                        Settings.getOwncloudPassword(v.getContext()),
                        bm
                );
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(final Menu menu) {
        getMenuInflater().inflate(R.menu.read_later_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(final MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            Intent intent = new Intent(this, Settings.class);
            startActivity(intent);
            return true;
        } else if (id == R.id.action_categories) {
            Intent intent = new Intent(this, CategoryMappingActivity.class);
            startActivity(intent);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

@@ -33,16 +33,48 @@
 r.app;%0A%0A
+import android.content.Context;%0A
 import a
@@ -316,16 +316,68 @@
 tText;%0A%0A
+import java.util.ArrayList;%0Aimport java.util.List;%0A%0A
 %0A/**%0A * 
@@ -1666,33 +1666,80 @@
       %7D%0A        
-%7D
+    String category = getCategories(text, this);
 %0A        categor
@@ -1731,16 +1731,20 @@
         
+    
 categori
@@ -1767,51 +1767,29 @@
 ext(
-Settings.getOwncloudDefaultCategory(this));
+category);%0A        %7D%0A
 %0A%0A  
@@ -2717,32 +2717,918 @@
      %7D);%0A    %7D%0A%0A
+    public static String getCategories(String text, Context ctx) %7B%0A%0A        final List%3CCategoryMapping%3E categoryMappings = CategoryMapping.getCategoryMappings(ctx);%0A        final List%3CString%3E categories = new ArrayList%3CString%3E();%0A        for (CategoryMapping m : categoryMappings) %7B%0A            if (text.contains(m.getText())) %7B%0A                categories.add(m.getCategories());%0A            %7D%0A        %7D%0A        if (categories.isEmpty()) %7B%0A            categories.add(Settings.getOwncloudDefaultCategory(ctx));%0A        %7D%0A        return implode(categories);%0A    %7D%0A%0A    private static String implode(List%3CString%3E l) %7B%0A        StringBuilder result = new StringBuilder();%0A        for (int i = 0; i %3C l.size(); i++) %7B%0A            result.append(l.get(0));%0A            if (i %3C l.size() - 1) %7B%0A                result.append(',');%0A            %7D%0A        %7D%0A        return result.toString();%0A    %7D%0A%0A
     @Override%0A  

use category mapping when adding bookmark