package net.fastfourier.something.widget;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.NumberPicker;

import com.salvadordalvik.fastlibrary.FastDialogFragment;

import net.fastfourier.something.R;

/**
 * Created by matthewshepard on 2/1/14.
 */
public class PageSelectDialogFragment extends FastDialogFragment implements View.OnClickListener, NumberPicker.OnValueChangeListener {
    private int maxPage, current, selected;

    private NumberPicker picker;

    public static interface PageSelectable{
        public void goToPage(int page);
    }

    public PageSelectDialogFragment() {
        super(R.layout.page_select_dialog, R.string.page_select_title);
    }

    public static PageSelectDialogFragment newInstance(int currentPage, int maxPage, PageSelectable targetFragment){
        PageSelectDialogFragment frag = new PageSelectDialogFragment();
        Bundle args = new Bundle();
        args.putInt("current", currentPage);
        args.putInt("max", maxPage);
        frag.setArguments(args);
        frag.setTargetFragment((Fragment) targetFragment, 0);
        return frag;
    };

    @Override
    public void viewCreated(View frag, Bundle savedInstanceState) {
        Bundle args = getArguments();
        current = args.getInt("current");
        maxPage = args.getInt("max");
        selected = current;

        picker = (NumberPicker) frag.findViewById(R.id.page_number_picker);
        picker.setMinValue(1);
        picker.setMaxValue(maxPage);
        picker.setValue(current);
        picker.setWrapSelectorWheel(false);
        picker.setOnValueChangedListener(this);
        frag.findViewById(R.id.page_select_button).setOnClickListener(this);
        frag.findViewById(R.id.page_select_first).setOnClickListener(this);
        frag.findViewById(R.id.page_select_last).setOnClickListener(this);
    }

    private void selectPage(int page) {
        Fragment fragment = getTargetFragment();
        if(fragment instanceof PageSelectable){
            ((PageSelectable) fragment).goToPage(page);
        }else{
            throw new RuntimeException("PageSelectDialogFragment has no Target PageSelectable Fragment!");
        }
        dismiss();
    }

    @Override
    public void refreshData(boolean pullToRefresh) {}

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.page_select_button:
                selectPage(selected);
                break;
            case R.id.page_select_first:
                picker.setValue(1);
                break;
            case R.id.page_select_last:
                picker.setValue(maxPage);
                break;
        }
    }

    @Override
    public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
        selected = newVal;
    }
}

@@ -409,44 +409,8 @@
 ener
-, NumberPicker.OnValueChangeListener
  %7B%0A 
@@ -435,27 +435,8 @@
 Page
-, current, selected
 ;%0A%0A 
@@ -1237,16 +1237,20 @@
         
+int 
 current 
@@ -1316,36 +1316,8 @@
 x%22);
-%0A        selected = current;
 %0A%0A  
@@ -1540,56 +1540,8 @@
 e);%0A
-        picker.setOnValueChangedListener(this);%0A
     
@@ -2329,27 +2329,270 @@
     
-selectPage(selected
+//NumberPicker won't update its internal value until the user finishes typing,%0A                //but it will update when it loses focus.%0A                //this is so fucking stupid%0A                picker.clearFocus();%0A                selectPage(picker.getValue()
 );%0A 
@@ -2604,33 +2604,32 @@
           break;
-%0A
             case
@@ -2809,32 +2809,32 @@
          break;%0A
+
         %7D%0A    %7D%0A
@@ -2836,132 +2836,7 @@
    %7D
-%0A%0A    @Override%0A    public void onValueChange(NumberPicker picker, int oldVal, int newVal) %7B%0A        selected = newVal;%0A    %7D
 %0A%7D%0A

Fix wrong page number getting selected in page numberpicker.