package derpibooru.derpy.ui.fragments;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

import butterknife.Bind;
import butterknife.ButterKnife;
import derpibooru.derpy.R;
import derpibooru.derpy.data.server.DerpibooruSearchOptions;
import derpibooru.derpy.ui.views.FloatingSearchView;

public class BrowseOptionsFragment extends Fragment {
    @Bind(R.id.floatingSearchView) FloatingSearchView searchText;

    @Bind(R.id.spinnerSortBy) Spinner sortBy;
    @Bind(R.id.spinnerSortDirection) Spinner sortDirection;
    @Bind(R.id.spinnerFavesFilter) Spinner favesFilter;
    @Bind(R.id.spinnerUpvotesFilter) Spinner upvotesFilter;
    @Bind(R.id.spinnerUploadsFilter) Spinner uploadsFilter;
    @Bind(R.id.spinnerWatchedTagsFilter) Spinner watchedTagsFilter;

    @Bind(R.id.textMinScore) EditText minScore;
    @Bind(R.id.textMaxScore) EditText maxScore;

    private DerpibooruSearchOptions mSelectedOptions = new DerpibooruSearchOptions();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_browse_options, container, false);
        ButterKnife.bind(this, v);
        if ((savedInstanceState != null)
                && (savedInstanceState.containsKey(BrowseFragment.EXTRAS_SEARCH_OPTIONS))) {
            mSelectedOptions = savedInstanceState.getParcelable(BrowseFragment.EXTRAS_SEARCH_OPTIONS);
        } else {
            mSelectedOptions = getArguments().getParcelable(BrowseFragment.EXTRAS_SEARCH_OPTIONS);
        }
        setSearchActionListenerForSearchQueryView();
        setSpinnerState(mSelectedOptions);
        return v;
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putParcelable(BrowseFragment.EXTRAS_SEARCH_OPTIONS, getSelectedOptions());
    }

    public DerpibooruSearchOptions getSelectedOptions() {
        if (searchText != null) {
            hideSoftKeyboard();
            mSelectedOptions.setSearchQuery(
                    searchText.getText().toString().isEmpty() ? "*" : searchText.getText().toString());
            mSelectedOptions.setSortBy(
                    DerpibooruSearchOptions.SortBy.fromValue(sortBy.getSelectedItemPosition()));
            mSelectedOptions.setSortDirection(
                    DerpibooruSearchOptions.SortDirection.fromValue(sortDirection.getSelectedItemPosition()));
            mSelectedOptions.setFavesFilter(
                    DerpibooruSearchOptions.UserPicksFilter.fromValue(favesFilter.getSelectedItemPosition()));
            mSelectedOptions.setUpvotesFilter(
                    DerpibooruSearchOptions.UserPicksFilter.fromValue(upvotesFilter.getSelectedItemPosition()));
            mSelectedOptions.setUploadsFilter(
                    DerpibooruSearchOptions.UserPicksFilter.fromValue(uploadsFilter.getSelectedItemPosition()));
            mSelectedOptions.setWatchedTagsFilter(
                    DerpibooruSearchOptions.UserPicksFilter.fromValue(watchedTagsFilter.getSelectedItemPosition()));
            mSelectedOptions.setMinScore(
                    getInteger(minScore.getText().toString()));
            mSelectedOptions.setMaxScore(
                    getInteger(maxScore.getText().toString()));
        }
        return mSelectedOptions;
    }

    private void setSpinnerState(DerpibooruSearchOptions from) {
        if (!from.getSearchQuery().equals("*")) searchText.setText(from.getSearchQuery());
        sortBy.setSelection(from.getSortBy().toValue());
        sortDirection.setSelection(from.getSortDirection().toValue());
        favesFilter.setSelection(from.getFavesFilter().toValue());
        upvotesFilter.setSelection(from.getUpvotesFilter().toValue());
        uploadsFilter.setSelection(from.getUploadsFilter().toValue());
        watchedTagsFilter.setSelection(from.getWatchedTagsFilter().toValue());
        if (from.getMinScore() != null) minScore.setText(from.getMinScore().toString());
        if (from.getMaxScore() != null) maxScore.setText(from.getMaxScore().toString());
    }

    private Integer getInteger(String text) {
        return (text.equals("")) ? null : Integer.parseInt(text);
    }

    private void setSearchActionListenerForSearchQueryView() {
        /* TODO: start search on keyboard search button press */
        searchText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    hideSoftKeyboard();
                    return true;
                }
                return false;
            }
        });
    }

    private void hideSoftKeyboard() {
        InputMethodManager imm =
                (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(searchText.getWindowToken(), 0);
    }
}

@@ -346,24 +346,59 @@
 hodManager;%0A
+import android.widget.AdapterView;%0A
 import andro
@@ -413,24 +413,24 @@
 t.EditText;%0A
-
 import andro
@@ -506,16 +506,46 @@
 e.Bind;%0A
+import butterknife.BindColor;%0A
 import b
@@ -1279,16 +1279,122 @@
 Score;%0A%0A
+    @BindColor(R.color.colorTextDark) int textColor;%0A    @BindColor(R.color.colorAccent) int accentColor;%0A
     priv
@@ -2153,24 +2153,65 @@
 ueryView();%0A
+        setSpinnerTintToggleListeners();%0A
         setS
@@ -4606,32 +4606,32 @@
 ().toString());%0A
-
         if (from
@@ -4702,32 +4702,1052 @@
 ring());%0A    %7D%0A%0A
+    private void setSpinnerTintToggleListeners() %7B%0A        AdapterView.OnItemSelectedListener toggleListener =%0A                new AdapterView.OnItemSelectedListener() %7B%0A                    @Override%0A                    public void onItemSelected(AdapterView%3C?%3E parent, View view, int position, long id) %7B%0A                        if (view != null) %7B%0A                            ((TextView) view).setTextColor((position == 0) ? textColor : accentColor);%0A                        %7D%0A                    %7D%0A%0A                    @Override%0A                    public void onNothingSelected(AdapterView%3C?%3E parent) %7B %7D%0A                %7D;%0A        sortBy.setOnItemSelectedListener(toggleListener);%0A        sortDirection.setOnItemSelectedListener(toggleListener);%0A        favesFilter.setOnItemSelectedListener(toggleListener);%0A        upvotesFilter.setOnItemSelectedListener(toggleListener);%0A        uploadsFilter.setOnItemSelectedListener(toggleListener);%0A        watchedTagsFilter.setOnItemSelectedListener(toggleListener);%0A    %7D%0A%0A
     private Inte

Change spinner color when the value is changed