package com.example.calculator;

//import Fragment classes from the support library
import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentManager; 
import android.support.v4.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;

public class SelectionFragment extends Fragment {
    private boolean bothFragmentsDisplayed = false;
    private Calculator currentlySelectedFragment;

    @Override
    public View onCreateView(LayoutInflater inflater,
            ViewGroup container, Bundle savedInstanceState) {

        View root = inflater.inflate(R.layout.fragment_selection,
                container, false);

        Button tipsButton = (Button) root
                .findViewById(R.id.tipCalculatorButton);
        tipsButton.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                showCalculator(Calculator.TIPS);
            }
        });

        Button bmiButton = (Button) root
                .findViewById(R.id.bmiCalculatorButton);
        bmiButton.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                showCalculator(Calculator.BMI);
            }
        });

        return root;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Determine if we are displaying both fragments simultaneously
        View otherFragment = getActivity().findViewById(
                R.id.placeholder);
        if (otherFragment != null
                && otherFragment.getVisibility() == View.VISIBLE) {
            bothFragmentsDisplayed = true;
        }

        if (bothFragmentsDisplayed) {
            if (savedInstanceState != null) {
                showCalculator((Calculator) savedInstanceState
                        .getSerializable("currentFragment"));
            }
            else {
                showCalculator(currentlySelectedFragment);
            }
        }
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putSerializable("currentFragment",
                currentlySelectedFragment);
    }

    public void showCalculator(Calculator fragmentToDisplay) {
        if (fragmentToDisplay == null) {
            fragmentToDisplay = Calculator.TIPS;
        }
        if (bothFragmentsDisplayed) {
            if (!fragmentToDisplay.equals(currentlySelectedFragment)) {
                Fragment fragment = fragmentToDisplay.getFragment();
                FragmentManager fm = getFragmentManager();
                FragmentTransaction ft = fm.beginTransaction();
                ft.replace(R.id.placeholder, fragment);
                ft.addToBackStack(null);
                ft.commit();
            }
        }
        else {
            // only displaying one fragment
            Intent i = new Intent();
            i.setClass(getActivity(), CalculatorActivity.class);
            i.putExtra("calculatorType", fragmentToDisplay);
            startActivity(i);
        }
        currentlySelectedFragment = fragmentToDisplay;
    }

}
