/*
 * Copyright (c) 2015 Justin White <jw@justinwhite.net>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 *
 * 3. Neither the name of the copyright holder nor the names of its
 * contributors may be used to endorse or promote products derived from this
 * software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 */

package net.justinwhite.score_it;

import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.SeekBar;
import android.widget.TextView;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

import net.justinwhite.score_model.phase_10.Phase10GameModel;

public class CreateGameFragment
        extends Fragment
        implements SeekBar.OnSeekBarChangeListener {

    @Bind(R.id.seekNumPlayers)
    SeekBar seekNumPlayers;
    @Bind(R.id.textNumPlayers)
    TextView labelNumPlayers;
    @Bind(R.id.labelMinPlayers)
    TextView labelMinPlayers;
    @Bind(R.id.labelMaxPlayers)
    TextView labelMaxPlayers;
    // effectively constant, but not final because no constructors in Fragments
    private int SEEKBAR_OFFSET;
    private GameSetupListener gameSetupListener;
    private int numPlayers;

    public static CreateGameFragment newInstance() {
        CreateGameFragment fragment = new CreateGameFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    public CreateGameFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_create_game, container, false);
        ButterKnife.bind(this, rootView);

        // get max & min players from the GameModel
        int maxNumPlayers = Phase10GameModel.MAX_PLAYERS;
        labelMaxPlayers.setText(String.valueOf(maxNumPlayers));
        int minNumPLayers = Phase10GameModel.MIN_PLAYERS;
        labelMinPlayers.setText(String.valueOf(minNumPLayers));

        // use min players to set seekbar offset since seekbars always have to start at 0
        SEEKBAR_OFFSET = minNumPLayers;
        seekNumPlayers.setMax(maxNumPlayers - SEEKBAR_OFFSET);

        numPlayers = gameSetupListener.getNumPlayers();
        seekNumPlayers.setProgress(numPlayers - SEEKBAR_OFFSET);
        seekNumPlayers.setOnSeekBarChangeListener(this);

        labelNumPlayers.setText(Integer.toString(numPlayers));
        labelNumPlayers.clearFocus();

        return rootView;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        try {
            gameSetupListener = (GameSetupListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement GameSetupListener");
        }
    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromTouch) {
        if (fromTouch) {
            numPlayers = progress + SEEKBAR_OFFSET;
            labelNumPlayers.setText(Integer.toString(numPlayers));
            gameSetupListener.setNumPlayers(numPlayers);
        }
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {

    }

    @OnClick(R.id.buttonStartGame)
    protected void StartNewGame(View view) {
        gameSetupListener.setNumPlayers(numPlayers);
        Fragment newFragment = GameFragment.newInstance();
        gameSetupListener.setCurrentFragmentID(MainActivity.FRAG_ID_GAME);
        getFragmentManager().beginTransaction()
                .replace(R.id.container, newFragment)
                .commit()
        ;
    }

}

@@ -3425,30 +3425,32 @@
 setText(
-String.valueOf
+Integer.toString
 (maxNumP
@@ -3553,22 +3553,24 @@
 ext(
-String.valueOf
+Integer.toString
 (min

Use Integer.toString() instead of String.valueOf, since we'd get any NPE before it gets that far.