/*
 * Copyright (C) 2009 Johan Nilsson <http://markupartist.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.markupartist.sthlmtraveling;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

import com.markupartist.sthlmtraveling.provider.FavoritesDbAdapter;

public class FavoritesActivity extends ListActivity {

    private FavoritesDbAdapter mFavoritesDbAdapter;

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

        mFavoritesDbAdapter = new FavoritesDbAdapter(this).open();

        fillData();
    }

    private void fillData() {
        Cursor favoritesCursor = mFavoritesDbAdapter.fetch();

        startManagingCursor(favoritesCursor);

        String[] from = new String[] {
                FavoritesDbAdapter.KEY_START_POINT, 
                FavoritesDbAdapter.KEY_END_POINT
            };

        int[] to = new int[]{R.id.favorite_start_point, R.id.favorite_end_point};

        SimpleCursorAdapter favorites = new SimpleCursorAdapter(
                this, R.layout.favorite_row, favoritesCursor, from, to);

        setListAdapter(favorites);
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);

        Cursor favoritesCursor = ((SimpleCursorAdapter) this.getListAdapter()).getCursor();

        // TODO: Cache getColumnIndex for the future.
        String startPoint = favoritesCursor.getString(
                favoritesCursor.getColumnIndex(FavoritesDbAdapter.KEY_START_POINT));
        String endPoint = favoritesCursor.getString(
                favoritesCursor.getColumnIndex(FavoritesDbAdapter.KEY_END_POINT));

        Uri routesUri = RoutesActivity.createRoutesUri(startPoint, endPoint);
        Intent i = new Intent(Intent.ACTION_VIEW, routesUri, this, RoutesActivity.class);
        startActivity(i);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mFavoritesDbAdapter.close();
    }
}

@@ -829,13 +829,71 @@
 oid.
-view.
+util.Log;%0Aimport android.view.View;%0Aimport android.widget.Image
 View
@@ -969,16 +969,222 @@
 dapter;%0A
+import android.widget.TextView;%0Aimport android.widget.SimpleCursorAdapter.CursorToStringConverter;%0Aimport android.widget.SimpleCursorAdapter.ViewBinder;%0A%0Aimport com.markupartist.sthlmtraveling.planner.Stop;
 %0Aimport 
@@ -2001,16 +2001,22 @@
         
+final 
 SimpleCu
@@ -2136,16 +2136,1208 @@
 om, to);
+%0A        favorites.setViewBinder(new ViewBinder() %7B%0A            @Override%0A            public boolean setViewValue(View view, Cursor cursor, int columnIndex) %7B%0A                String name = cursor.getString(columnIndex);%0A                if (name.equals(Stop.TYPE_MY_LOCATION)) %7B%0A                    /*%0A                    View layout = getLayoutInflater().inflate(R.layout.favorite_row, null);%0A%0A                    if (view.getId() == R.id.favorite_start_point) %7B%0A                        ImageView startImageView = (ImageView) %0A                                layout.findViewById(R.id.favorite_start_point_image);%0A                        startImageView.setImageResource(R.drawable.ic_current_position);%0A                    %7D else %7B%0A                        ImageView endImageView = (ImageView) %0A                                layout.findViewById(R.id.favorite_end_point_image);%0A                        endImageView.setImageResource(R.drawable.ic_current_position);%0A                    %7D%0A                    */%0A%0A                    name = getString(R.string.my_location);%0A                %7D%0A%0A                ((TextView) view).setText(name);%0A                return true;%0A            %7D%0A        %7D);
 %0A%0A      

support for my location in favorite list