/*
 * Authored By Julian Chu <walkingice@0xlab.org>
 *
 * Copyright (c) 2012 0xlab.org - http://0xlab.org/
 *
 * 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 org.zeroxlab.momome.impl;

import org.zeroxlab.momome.R;
import org.zeroxlab.momome.Momo;
import org.zeroxlab.momome.MomoApp;
import org.zeroxlab.momome.MomoModel;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceActivity;
import android.util.Log;
import android.widget.Toast;

public class PrefMain extends PreferenceActivity implements Momo {

    OnPreferenceClickListener mDataActionListener;
    MomoModel mModel;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mModel = MomoApp.getModel();

        addPreferencesFromResource(R.xml.pref_main);
        Preference exportPref = findPreference(KEY_EXPORT_DATA);
        Preference importPref = findPreference(KEY_IMPORT_DATA);
        Preference deletePref = findPreference(KEY_DELETE_DATA);
        Preference changePref = findPreference(KEY_CHANGE_PASSWORD);

        OnPreferenceClickListener listener = new DataActionListener();
        exportPref.setOnPreferenceClickListener(listener);
        importPref.setOnPreferenceClickListener(listener);
        deletePref.setOnPreferenceClickListener(listener);
        changePref.setOnPreferenceClickListener(listener);

        if (mModel.status() != DataStatus.OK
                || mModel.status() != DataStatus.FILE_IS_EMPTY) {

            exportPref.setEnabled(false);
            importPref.setEnabled(false);
            changePref.setEnabled(false);
        }
    }

    class DataActionListener implements OnPreferenceClickListener {
        public boolean onPreferenceClick(Preference preference) {
            if (preference.getKey().equals(KEY_EXPORT_DATA)) {
                Toast.makeText(PrefMain.this, "Export not implement yet",Toast.LENGTH_SHORT).show();
            } else if (preference.getKey().equals(KEY_IMPORT_DATA)) {
                Toast.makeText(PrefMain.this, "Import not implement yet",Toast.LENGTH_SHORT).show();
            } else if (preference.getKey().equals(KEY_DELETE_DATA)) {
                Toast.makeText(PrefMain.this, "Delete not implement yet",Toast.LENGTH_SHORT).show();
            } else if (preference.getKey().equals(KEY_CHANGE_PASSWORD)) {
                Toast.makeText(PrefMain.this, "Change password not implement yet",Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(PrefMain.this, "oooops",Toast.LENGTH_SHORT).show();
                return false;
            }

            return true;
        }
    }
}

@@ -729,16 +729,55 @@
 mome.R;%0A
+import org.zeroxlab.momome.data.Item;;%0A
 import o
@@ -801,16 +801,16 @@
 e.Momo;%0A
-
 import o
@@ -897,32 +897,104 @@
 d.app.Activity;%0A
+import android.app.AlertDialog;%0Aimport android.content.DialogInterface;%0A
 import android.c
@@ -1237,16 +1237,126 @@
 Toast;%0A%0A
+import java.io.File;%0Aimport java.io.FileInputStream;%0Aimport java.io.FileOutputStream;%0Aimport java.util.List;%0A%0A
 public c
@@ -2313,76 +2313,12 @@
 s.OK
-%0A                %7C%7C mModel.status() != DataStatus.FILE_IS_EMPTY
 ) %7B%0A
-%0A
     
@@ -2452,24 +2452,1483 @@
    %7D%0A    %7D%0A%0A
+    private void askExportData() %7B%0A        AlertDialog.Builder builder = new AlertDialog.Builder(this);%0A        File file = new File(EXTERNAL_DIR, FILENAME);%0A        builder.setTitle(%22Export data?%22);%0A        builder.setMessage(%22write to: %22 + file.getPath());%0A        builder.setPositiveButton(android.R.string.yes,%0A                new DialogInterface.OnClickListener() %7B%0A                    public void onClick(DialogInterface dialog, int which) %7B%0A                        if (which == DialogInterface.BUTTON_POSITIVE) %7B%0A                            onExportData();%0A                        %7D%0A                    %7D%0A                %7D);%0A        builder.show();%0A    %7D%0A%0A    private void onExportData() %7B%0A        List%3CItem%3E items = mModel.getItems();%0A        File output = new File(EXTERNAL_DIR, FILENAME);%0A        if (!output.getParentFile().canWrite()) %7B%0A            showToast(%22Cannot write to %22 + output.getPath());%0A            return;%0A        %7D%0A%0A        try %7B%0A            FileOutputStream fos = new FileOutputStream(output);%0A            boolean success = mModel.saveHelper(fos, items);%0A            if (success) %7B%0A                showToast(%22Done!%22);%0A            %7D else %7B%0A                showToast(%22Failed on writing%22);%0A            %7D%0A        %7D catch (Exception e) %7B%0A            showToast(%22Failed%22);%0A            e.printStackTrace();%0A        %7D%0A    %7D%0A%0A    private void showToast(CharSequence msg) %7B%0A        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();%0A    %7D%0A%0A
     class Da
@@ -4132,89 +4132,21 @@
     
-Toast.makeText(PrefMain.this, %22Export not implement yet%22,Toast.LENGTH_SHORT).show
+askExportData
 ();%0A

implement export data