/*
 * Copyright (c) Mattia Barbon <mattia@barbon.org>
 * distributed under the terms of the MIT license
 */

package org.barbon.mangaget.fragments;

import android.content.DialogInterface;

import android.database.Cursor;

import android.os.Bundle;

import android.view.View;

import android.widget.SimpleCursorAdapter;
import android.widget.ImageView;
import android.widget.ListView;

import android.support.v4.app.Fragment;
import android.support.v4.app.ListFragment;

import org.barbon.mangaget.Download;
import org.barbon.mangaget.R;

import org.barbon.mangaget.data.DB;

public class ChapterList extends ListFragment {
    private static final StatusBinder VIEW_BINDER = new StatusBinder();
    private SimpleCursorAdapter adapter;
    private DownloadListener listener = new DownloadListener();
    private Download.ListenerManager manager =
        new Download.ListenerManager(listener);

    public static class DownloadConfirmationDialog extends ConfirmationDialog {
        private static final String TAG = "downloadConfirmationDialog";

        public static DownloadConfirmationDialog newInstance(long chapterId) {
            DownloadConfirmationDialog frag = new DownloadConfirmationDialog();
            Bundle args = getDialogArguments(R.string.download_title,
                                             R.string.start_download,
                                             R.string.cancel);

            args.putLong("chapterId", chapterId);

            frag.setArguments(args);

            return frag;
        }

        public static DownloadConfirmationDialog find(Fragment f) {
            return (DownloadConfirmationDialog)
                f.getFragmentManager().findFragmentByTag(TAG);
        }

        public void show(Fragment f) {
            show(f.getFragmentManager(), TAG);
        }

        public long getChapterId() {
            return getArguments().getLong("chapterId");
        }
    }

    private class DownloadListener extends Download.ListenerAdapter {
        public long currentManga = -1;

        @Override
        public void onMangaUpdateComplete(long mangaId, boolean success) {
            if (!success || mangaId != currentManga)
                return;

            loadChapterList(mangaId);
        }
    }

    private static class StatusBinder
            implements SimpleCursorAdapter.ViewBinder {
        @Override
        public boolean setViewValue(View view, Cursor cursor, int column) {
            if (view.getId() != R.id.chapter_downloaded)
                return false;

            ImageView image = (ImageView) view;
            int status = cursor.getInt(column);

            // TODO add another icon for the partially downloaded status
            if (status == DB.DOWNLOAD_COMPLETE)
                image.setImageResource(R.drawable.btn_check_buttonless_on);
            else
                image.setImageResource(R.drawable.btn_check_buttonless_off);

            return true;
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        adapter = new SimpleCursorAdapter(
            getActivity(), R.layout.chapter_item, null,
            new String[] { DB.CHAPTER_NUMBER, DB.CHAPTER_TITLE,
                           DB.DOWNLOAD_STATUS },
            new int[] { R.id.chapter_number, R.id.chapter_title,
                        R.id.chapter_downloaded });
        adapter.setViewBinder(VIEW_BINDER);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        setEmptyText(getString(R.string.no_chapter));
        setListAdapter(adapter);
        bindConfirmationDialog(DownloadConfirmationDialog.find(this));
    }

    @Override
    public void onResume() {
        super.onResume();

        if (adapter.getCursor() != null)
            adapter.getCursor().requery();

        manager.connect(getActivity());
    }

    @Override
    public void onPause() {
        super.onPause();

        manager.disconnect(getActivity());
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();

        adapter.changeCursor(null);
    }

    // public interface

    public void loadChapterList(long mangaId) {
        DB db = DB.getInstance(getActivity());

        listener.currentManga = mangaId;
        adapter.changeCursor(db.getChapterList(mangaId));
    }

    // implementation

    private void bindConfirmationDialog(DownloadConfirmationDialog dialog) {
        if (dialog == null)
            return;

        final long chapterId = dialog.getChapterId();

        DialogInterface.OnClickListener download =
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Download.startChapterDownload(getActivity(), chapterId);
                }
            };

        dialog.setPositiveClick(download);
    }

    // event handlers

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        DownloadConfirmationDialog frag =
            DownloadConfirmationDialog.newInstance(id);

        bindConfirmationDialog(frag);

        frag.show(this);
    }
}

@@ -617,16 +617,73 @@
 gment %7B%0A
+    private static final String SELECTED_ID = %22mangaId%22;%0A
     priv
@@ -742,24 +742,25 @@
 usBinder();%0A
+%0A
     private 
@@ -788,16 +788,52 @@
 dapter;%0A
+    private long currentManga = -1;%0A
     priv
@@ -2096,48 +2096,8 @@
 r %7B%0A
-        public long currentManga = -1;%0A%0A
     
@@ -3641,32 +3641,143 @@
 nstanceState);%0A%0A
+        if (savedInstanceState != null)%0A            loadChapterList(savedInstanceState.getLong(SELECTED_ID));%0A%0A
         setEmpty
@@ -4119,32 +4119,207 @@
 vity());%0A    %7D%0A%0A
+    @Override%0A    public void onSaveInstanceState(Bundle outState) %7B%0A        super.onSaveInstanceState(outState);%0A%0A        outState.putLong(SELECTED_ID, currentManga);%0A    %7D%0A%0A
     @Override%0A  
@@ -4664,24 +4664,24 @@
 ty());%0A%0A
+
         
 listener
@@ -4676,17 +4676,8 @@
     
-listener.
 curr

Save currently selected manga in ChapterList state.