/**
 * Copyright 2016 Lloyd Torres
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.lloydtorres.stately.census;

import android.view.View;

import com.lloydtorres.stately.R;
import com.lloydtorres.stately.core.RecyclerDialogFragment;
import com.lloydtorres.stately.dto.CensusScale;
import com.lloydtorres.stately.dto.Dataset;

import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
 * Created by Lloyd on 2016-04-10.
 * A dialog showing the available trend datasets.
 */
public class DatasetDialog extends RecyclerDialogFragment {
    public static final String DIALOG_TAG = "fragment_dataset_dialog";
    public static final String DATASETS_KEY = "datasets";

    private LinkedHashMap<Integer, Dataset> datasets;

    public void setDatasets(LinkedHashMap<Integer, CensusScale> rawDataset, int selected) {
        datasets = new LinkedHashMap<Integer, Dataset> ();
        ArrayList<CensusScale> rawScales = new ArrayList<CensusScale>(rawDataset.values());

        for (int i = 0; i < rawScales.size()-1; i++) {
            CensusScale scale = rawScales.get(i);

            Dataset d = new Dataset();
            d.name = scale.name;
            d.id = scale.id;
            d.selected = false;
            datasets.put(d.id, d);
        }

        if (datasets.containsKey(selected)) {
            datasets.get(selected).selected = true;
        }
    }

    @Override
    protected void initRecycler(View view) {
        super.initRecycler(view);
        setDialogTitle(getString(R.string.trends_datasets));

        mRecyclerAdapter = new DatasetRecyclerAdapter(getActivity(), this, datasets);
        mRecyclerView.setAdapter(mRecyclerAdapter);

        int position = ((DatasetRecyclerAdapter) mRecyclerAdapter).getSelectedPosition();
        if (position != DatasetRecyclerAdapter.INVALID_POSITION) {
            mLayoutManager.scrollToPosition(position);
        }
    }
}

@@ -652,17 +652,110 @@
 oid.
-view.View
+os.Bundle;%0Aimport android.view.LayoutInflater;%0Aimport android.view.View;%0Aimport android.view.ViewGroup
 ;%0A%0Ai
@@ -1280,16 +1280,90 @@
 datasets
+_key%22;%0A    public static final String SELECTED_KEY = %22selected_dataset_key
 %22;%0A%0A    
@@ -1411,16 +1411,42 @@
 atasets;
+%0A    private int selected;
 %0A%0A    pu
@@ -1531,32 +1531,37 @@
 cted) %7B%0A        
+this.
 datasets = new L
@@ -1683,25 +1683,24 @@
 .values());%0A
-%0A
         for 
@@ -1930,32 +1930,37 @@
 se;%0A            
+this.
 datasets.put(d.i
@@ -1985,20 +1985,162 @@
         
-if (
+setSelectedDataset(selected);%0A    %7D%0A%0A    private void setSelectedDataset(final int selected) %7B%0A        this.selected = selected;%0A        if (this.
 datasets
@@ -2177,16 +2177,21 @@
         
+this.
 datasets
@@ -2760,11 +2760,1001 @@
  %7D%0A    %7D
+%0A%0A    @Override%0A    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) %7B%0A        if (savedInstanceState != null) %7B%0A            ArrayList%3CDataset%3E rawDataset =%0A                    savedInstanceState.getParcelableArrayList(DATASETS_KEY);%0A            this.datasets = new LinkedHashMap%3CInteger, Dataset%3E ();%0A            for (Dataset dataset : rawDataset) %7B%0A                this.datasets.put(dataset.id, dataset);%0A            %7D%0A            %0A            setSelectedDataset(savedInstanceState.getInt(SELECTED_KEY));%0A        %7D%0A        return super.onCreateView(inflater, container, savedInstanceState);%0A    %7D%0A%0A    @Override%0A    public void onSaveInstanceState(Bundle outState) %7B%0A        // Save state%0A        super.onSaveInstanceState(outState);%0A        final ArrayList%3CDataset%3E rawDatasets = new ArrayList%3C%3E(datasets.values());%0A        outState.putParcelableArrayList(DATASETS_KEY, rawDatasets);%0A        outState.putInt(SELECTED_KEY, selected);%0A    %7D
 %0A%7D%0A

Fix null pointer error in dataset dialog