/**
 *  Copyright (C) 2015  Jon Griffiths (jon_p_griffiths@yahoo.com)
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.sufficientlysecure.ical.ui;

import org.sufficientlysecure.ical.R;
import org.sufficientlysecure.ical.Settings;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import android.content.Context;
import android.preference.DialogPreference;
import android.support.annotation.NonNull;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Spinner;

public class RemindersDialog extends DialogPreference {

    // Must match the list items in strings.xml
    private static final int[] MINUTES = {
        5, 10, 15, 30, 45, 60, 120, 240, 480, 720, 1440, 2880, 4320, 5760,
        7200, 8640, 10080, 20160, 30240, 40320
    };

    private LinearLayout mReminderItemsHolder;
    private LayoutInflater mInflater;
    private Settings mSettings;
    private int mNewId;

    public RemindersDialog(Context context, AttributeSet attrs) {
        super(context, attrs);
        MainActivity activity = (MainActivity) context;
        mInflater = activity.getLayoutInflater();
        mSettings = activity.getSettings();
        mNewId = 1;
    }

    public static List<Integer> getSavedRemindersInMinutes(Settings settings) {
        List<Integer> result = new ArrayList<Integer>();
        for (String item: settings.getString(Settings.PREF_DEFAULT_REMINDERS).split(",")) {
            if (item.length() > 0) {
                result.add(MINUTES[Integer.parseInt(item)]);
            }
        }
        return result;
    }

    @Override
    protected void onBindDialogView(@NonNull View view) {
        super.onBindDialogView(view);

        mReminderItemsHolder = (LinearLayout) view.findViewById(R.id.reminder_holder);

        for (String item: mSettings.getString(Settings.PREF_DEFAULT_REMINDERS).split(",")) {
            if (item.length() > 0) {
                addReminder(Integer.parseInt(item));
            }
        }

        View.OnClickListener addClick = new View.OnClickListener() {
            public void onClick(View v) {
                addReminder(0);
            }
        };
        Button addButton = (Button) view.findViewById(R.id.button_reminder_add);
        addButton.setOnClickListener(addClick);
    }

    private void addReminder(int index) {

        View newItem = mInflater.inflate(R.layout.reminder, mReminderItemsHolder, false);
        final int id = mNewId++;
        newItem.setId(id);
        Spinner dropDown = (Spinner) newItem.findViewById(R.id.reminder_item);
        dropDown.setSelection(index);

        View.OnClickListener delClick = new View.OnClickListener() {
            public void onClick(View v) {
                mReminderItemsHolder.removeView(mReminderItemsHolder.findViewById(id));
            }
        };
        Button delButton = (Button) newItem.findViewById(R.id.button_reminder_delete);
        delButton.setOnClickListener(delClick);

        mReminderItemsHolder.addView(newItem);
    }

    @Override
    protected void onDialogClosed(boolean positiveResult) {
        super.onDialogClosed(positiveResult);
        if (!positiveResult) {
            return;
        }

        // Save the (reordered and unique) chosen reminders to settings
        Set<Integer> values = new HashSet<Integer>();
        final int count = mReminderItemsHolder.getChildCount();
        for (int i = 0; i < count; i++) {
            View item = mReminderItemsHolder.getChildAt(i);
            Spinner dropDown = (Spinner) item.findViewById(R.id.reminder_item);
            values.add(dropDown.getSelectedItemPosition());
        }

        List<Integer> sorted = new ArrayList<Integer>(values);
        Collections.sort(sorted);

        StringBuilder b = new StringBuilder();
        for (Integer v: sorted) {
            if (b.length() != 0) {
                b.append(",");
            }
            b.append(v);
        }

        mSettings.putString(Settings.PREF_DEFAULT_REMINDERS, b.toString());
    }
}

@@ -1599,24 +1599,16 @@
 Layout m
-Reminder
 ItemsHol
@@ -1628,64 +1628,34 @@
 ate 
-LayoutInflater mInflater;%0A    private Settings mSettings
+SettingsActivity mActivity
 ;%0A  
@@ -1782,20 +1782,17 @@
         
-Main
+m
 Activity
@@ -1796,24 +1796,19 @@
 ity 
-activity = (Main
+= (Settings
 Acti
@@ -1826,102 +1826,8 @@
 xt;%0A
-        mInflater = activity.getLayoutInflater();%0A        mSettings = activity.getSettings();%0A
     
@@ -2346,32 +2346,24 @@
 ;%0A%0A        m
-Reminder
 ItemsHolder 
@@ -2420,25 +2420,24 @@
 older);%0A
-%0A
         
 for (Str
@@ -2432,35 +2432,61 @@
     
-for (
 String 
-item: mSettings
+def = new Settings(mActivity.getPreferences())
 .get
@@ -2516,32 +2516,64 @@
 FAULT_REMINDERS)
+;%0A%0A        for (String item: def
 .split(%22,%22)) %7B%0A 
@@ -3057,24 +3057,42 @@
 Item = m
+Activity.getLayout
 Inflater
 .inflate
@@ -3083,16 +3083,18 @@
 Inflater
+()
 .inflate
@@ -3114,24 +3114,16 @@
 inder, m
-Reminder
 ItemsHol
@@ -3437,32 +3437,24 @@
            m
-Reminder
 ItemsHolder.
@@ -3465,24 +3465,16 @@
 veView(m
-Reminder
 ItemsHol
@@ -3666,24 +3666,16 @@
        m
-Reminder
 ItemsHol
@@ -4034,32 +4034,24 @@
 nt count = m
-Reminder
 ItemsHolder.
@@ -4134,24 +4134,16 @@
 item = m
-Reminder
 ItemsHol
@@ -4619,17 +4619,20 @@
     
-m
+new 
 Settings
 .put
@@ -4627,16 +4627,44 @@
 Settings
+(mActivity.getPreferences())
 .putStri

Fix reminders settings dialog for new prefs