package com.ucasoft.controls.dialogs;

import android.annotation.TargetApi;
import android.app.*;
import android.content.DialogInterface;
import android.os.Build;
import android.os.Bundle;
import com.ucasoft.controls.R;

import java.text.DateFormatSymbols;
import java.util.Calendar;
import java.util.HashSet;

/**
 * Created by UCASoft.
 * User: Antonov Sergey
 * Date: 15.03.14
 * Time: 16:07
 */
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class WeekdaysPicker extends DialogFragment {

    public interface OnWeekdaysListener {
        void OnWeekdaysSelected(int[] weekdays);
    }

    private final String TAG = "WeekdaysPicker";
    private OnWeekdaysListener listener;
    private HashSet<Integer> selectedDays = new HashSet<Integer>();

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity())
                .setMultiChoiceItems(getWeekDays(), null, new DialogInterface.OnMultiChoiceClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i, boolean b) {
                        int firstDay = Calendar.getInstance().getFirstDayOfWeek();
                        int dayOfWeek = i + firstDay;
                        if (dayOfWeek > 7)
                            dayOfWeek = dayOfWeek - 7;
                        if (selectedDays.contains(dayOfWeek))
                            selectedDays.remove(dayOfWeek);
                        else
                            selectedDays.add(dayOfWeek);
                    }
                })
                .setTitle(R.string.weekdays_dialog_title)
                .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        if (listener != null) {
                            listener.OnWeekdaysSelected(getIntResult());
                        }
                    }
                })
                .setNegativeButton(android.R.string.cancel, null);
        return builder.create();
    }

    private int[] getIntResult() {
        int[] result = new int[selectedDays.size()];
        int i = 0;
        for(Integer day : selectedDays) {
            result[i++] = day;
        }
        return result;
    }

    public void show(FragmentManager fragmentManager) {
        show(fragmentManager, TAG);
    }

    public void setOnWeekdaysListener(OnWeekdaysListener listener) {
        this.listener = listener;
    }

    private String[] getWeekDays() {
        String[] result = new String[7];
        String[] weekdays = DateFormatSymbols.getInstance().getWeekdays();
        int firstDay = Calendar.getInstance().getFirstDayOfWeek();
        for (int i = 0; i < 7; i++) {
            result[i] = weekdays[firstDay];
            if (firstDay == 7)
                firstDay = 1;
            else
                firstDay++;
        }
        return result;
    }
}

@@ -271,42 +271,9 @@
 til.
-Calendar;%0Aimport java.util.HashSet
+*
 ;%0A%0A/
@@ -655,20 +655,20 @@
 private 
-Hash
+Tree
 Set%3CInte
@@ -695,12 +695,12 @@
 new 
-Hash
+Tree
 Set%3C
@@ -917,20 +917,33 @@
 Days(), 
-null
+getSelectedDays()
 , new Di
@@ -2164,181 +2164,739 @@
    p
-rivate int%5B%5D getIntResult() %7B%0A        int%5B%5D result = new int%5BselectedDays.size()%5D;%0A        int i = 0;%0A        for(Integer day : selectedDays) %7B%0A            result%5Bi++%5D = day
+ublic void setSelectedDays(int%5B%5D weekdays) %7B%0A        if (weekdays != null) %7B%0A            for (int weekday : weekdays) %7B%0A                selectedDays.add(weekday);%0A            %7D%0A        %7D%0A    %7D%0A%0A    private int%5B%5D getIntResult() %7B%0A        int%5B%5D result = new int%5BselectedDays.size()%5D;%0A        int i = 0;%0A        for(Integer day : selectedDays) %7B%0A            result%5Bi++%5D = day;%0A        %7D%0A        return result;%0A    %7D%0A%0A    private boolean%5B%5D getSelectedDays()%7B%0A        boolean%5B%5D result = new boolean%5B7%5D;%0A        int firstDay = Calendar.getInstance().getFirstDayOfWeek();%0A        for (int day : selectedDays) %7B%0A            int i = day - firstDay;%0A            if (i %3C 0) %7B%0A                i += 7;%0A            %7D%0A            result%5Bi%5D = true
 ;%0A  

Work with WeekdaysPicker.