
package com.battlelancer.seriesguide.ui.dialogs;

import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

import com.battlelancer.seriesguide.beta.R;
import com.battlelancer.seriesguide.provider.SeriesContract.ListItems;
import com.battlelancer.seriesguide.provider.SeriesContract.Lists;
import com.battlelancer.seriesguide.ui.OnListsChangedListener;

public class ListManageDialogFragment extends DialogFragment {

    public static ListManageDialogFragment newInstance(String listId) {
        ListManageDialogFragment f = new ListManageDialogFragment();

        Bundle args = new Bundle();
        args.putString("listid", listId);
        f.setArguments(args);

        return f;
    }

    private EditText mTitle;
    private OnListsChangedListener mListener;

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

        // hide title, use custom theme
        setStyle(STYLE_NO_TITLE, R.style.SeriesGuideTheme_Dialog);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        final View layout = inflater.inflate(R.layout.list_manage_dialog, null);

        // title
        mTitle = (EditText) layout.findViewById(R.id.title);

        // buttons
        Button buttonNegative = (Button) layout.findViewById(R.id.buttonNegative);
        buttonNegative.setText(R.string.list_remove);
        buttonNegative.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Cursor lists = getActivity().getContentResolver().query(Lists.CONTENT_URI,
                        new String[] {
                            Lists._ID
                        }, null, null, null);
                int listCount = lists.getCount();
                lists.close();

                // do not remove last list
                if (listCount > 1) {
                    // remove list and items
                    String listId = getArguments().getString("listid");
                    getActivity().getContentResolver().delete(Lists.buildListUri(listId), null,
                            null);
                    getActivity().getContentResolver().delete(ListItems.CONTENT_URI,
                            Lists.LIST_ID + "=?", new String[] {
                                listId
                            });

                    // remove tab from view pager
                    mListener.onListsChanged();
                }

                dismiss();
            }
        });
        Button buttonPositive = (Button) layout.findViewById(R.id.buttonPositive);
        buttonPositive.setText(android.R.string.ok);
        buttonPositive.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // update title
                String listId = getArguments().getString("listid");
                ContentValues values = new ContentValues();
                values.put(Lists.NAME, mTitle.getText().toString());
                getActivity().getContentResolver().update(Lists.buildListUri(listId), values, null,
                        null);

                // refresh view pager
                mListener.onListsChanged();

                dismiss();
            }
        });

        return layout;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        try {
            mListener = (OnListsChangedListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement OnListsChangedListener");
        }
    }

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

        String listId = getArguments().getString("listid");
        final Cursor list = getActivity().getContentResolver()
                .query(Lists.buildListUri(listId), new String[] {
                        Lists.NAME
                }, null, null, null);
        list.moveToFirst();

        mTitle.setText(list.getString(0));

        list.close();
    }

    /**
     * Display a dialog which allows to edit the title of this list or remove
     * it.
     */
    public static void showListManageDialog(String listId, FragmentManager fm) {
        // DialogFragment.show() will take care of adding the fragment
        // in a transaction. We also want to remove any currently showing
        // dialog, so make our own transaction and take care of that here.
        FragmentTransaction ft = fm.beginTransaction();
        Fragment prev = fm.findFragmentByTag("listmanagedialog");
        if (prev != null) {
            ft.remove(prev);
        }
        ft.addToBackStack(null);

        // Create and show the dialog.
        DialogFragment newFragment = ListManageDialogFragment.newInstance(listId);
        newFragment.show(ft, "listmanagedialog");
    }
}

@@ -1211,16 +1211,52 @@
 istener;
+%0A    private Button mButtonNegative;
 %0A%0A    @O
@@ -1778,32 +1778,26 @@
 ons%0A        
+m
 B
-utton b
 uttonNegativ
@@ -1851,33 +1851,34 @@
 ative);%0A        
-b
+mB
 uttonNegative.se
@@ -1910,25 +1910,26 @@
 e);%0A        
-b
+mB
 uttonNegativ
@@ -2041,388 +2041,8 @@
 ) %7B%0A
-                Cursor lists = getActivity().getContentResolver().query(Lists.CONTENT_URI,%0A                        new String%5B%5D %7B%0A                            Lists._ID%0A                        %7D, null, null, null);%0A                int listCount = lists.getCount();%0A                lists.close();%0A%0A                // do not remove last list%0A                if (listCount %3E 1) %7B%0A    
     
@@ -2078,20 +2078,16 @@
 d items%0A
-    
         
@@ -2138,36 +2138,32 @@
 ring(%22listid%22);%0A
-    
                 
@@ -2230,36 +2230,32 @@
 (listId), null,%0A
-    
                 
@@ -2277,36 +2277,32 @@
                 
-    
 getActivity().ge
@@ -2350,20 +2350,16 @@
 NT_URI,%0A
-    
         
@@ -2435,28 +2435,24 @@
             
-    
 listId%0A     
@@ -2466,37 +2466,29 @@
             
-  
-  
 %7D);%0A%0A
-    
             
@@ -2529,36 +2529,32 @@
                 
-    
 mListener.onList
@@ -2564,34 +2564,16 @@
 anged();
-%0A                %7D
 %0A%0A      
@@ -3864,24 +3864,59 @@
 ted(arg0);%0A%0A
+        // pre-populate list title%0A
         Stri
@@ -4189,25 +4189,24 @@
 eToFirst();%0A
-%0A
         mTit
@@ -4236,17 +4236,16 @@
 ng(0));%0A
-%0A
         
@@ -4253,24 +4253,390 @@
 ist.close();
+%0A%0A        // do not allow removing last list, disable remove button%0A        Cursor lists = getActivity().getContentResolver().query(Lists.CONTENT_URI,%0A                new String%5B%5D %7B%0A                    Lists._ID%0A                %7D, null, null, null);%0A        if (lists.getCount() == 1) %7B%0A            mButtonNegative.setEnabled(false);%0A        %7D%0A        lists.close();
 %0A    %7D%0A%0A    

Disable remove button for last list.