// Copyright (c) 2016, Miquel Martí <miquelmarti111@gmail.com>
// See LICENSE for licensing information
package cat.mvmike.minimalcalendarwidget.activity;

import java.text.DateFormatSymbols;
import java.util.Locale;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;

import cat.mvmike.minimalcalendarwidget.MonthWidget;
import cat.mvmike.minimalcalendarwidget.R;
import cat.mvmike.minimalcalendarwidget.util.ConfigurationUtil;

public class ConfigurationActivity extends Activity {

    @Override
    public void onCreate(final Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.configuration);

        setAvailableValues();
        loadPreviousConfig();

        applyListener();
        closeListener();
    }

    private void applyListener() {

        Button dismissButton = (Button) findViewById(R.id.applyButton);
        dismissButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                saveConfig();
                ConfigurationActivity.this.finish();
            }
        });
    }

    private void closeListener() {

        Button dismissButton = (Button) findViewById(R.id.closeButton);
        dismissButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ConfigurationActivity.this.finish();
            }
        });
    }

    private void setAvailableValues() {

        ArrayAdapter<String> adapterWeekDays = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item,
            DateFormatSymbols.getInstance(Locale.getDefault()).getWeekdays());

        adapterWeekDays.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        ((Spinner) findViewById(R.id.startWeekDaySpinner)).setAdapter(adapterWeekDays);
    }

    private void loadPreviousConfig() {
        ((Spinner) findViewById(R.id.startWeekDaySpinner)).setSelection(ConfigurationUtil.getStartWeekDay(getApplicationContext()));
    }

    private void saveConfig() {

        int selectedPosition = ((Spinner) findViewById(R.id.startWeekDaySpinner)).getSelectedItemPosition();

        if (selectedPosition != 0)
            ConfigurationUtil.setStartWeekDay(getApplicationContext(), selectedPosition);

        MonthWidget.forceRedraw(getApplicationContext());
    }
}

@@ -185,16 +185,41 @@
 ymbols;%0A
+import java.util.Arrays;%0A
 import j
@@ -235,16 +235,16 @@
 Locale;%0A
-
 %0Aimport 
@@ -655,24 +655,86 @@
 Activity %7B%0A%0A
+    private static final int BLANK_POSITION_DIFFERENCE = -1;%0A%0A
     @Overrid
@@ -1786,24 +1786,277 @@
 Values() %7B%0A%0A
+        // get locale weekDays and remove blank initial value%0A        String%5B%5D localeWeekDays = DateFormatSymbols.getInstance(Locale.getDefault()).getWeekdays();%0A        String%5B%5D weekDays = Arrays.copyOfRange(localeWeekDays, 1, localeWeekDays.length);%0A%0A
         Arra
@@ -2156,85 +2156,17 @@
 tem,
-%0A            DateFormatSymbols.getInstance(Locale.getDefault()).getWeekd
+ weekD
 ays
-()
 );%0A%0A
@@ -2450,24 +2450,37 @@
 DaySpinner))
+%0A            
 .setSelectio
@@ -2539,16 +2539,44 @@
 ntext())
+ + BLANK_POSITION_DIFFERENCE
 );%0A    %7D
@@ -2723,48 +2723,8 @@
 ();%0A
-%0A        if (selectedPosition != 0)%0A    
     
@@ -2798,24 +2798,52 @@
 ctedPosition
+ - BLANK_POSITION_DIFFERENCE
 );%0A%0A        

remove blank space on start week day config