package com.example.sergey.frag;

/**
 *   Example of two different Fragments used for Portrait and Landscape
 *     orientations. Adopted from DiMarzio's textbook.
 *
 *   The app layout is empty at the start. The Fragment to load is
 *     chosen dynamically in onCreate(..), based on screen orientation
 *     at the moment it runs.
 *
 *   This code also includes a callback for changes in configuration;
 *     this callback needs a special declaration in Manifest to work.
 *
 *   See also: https://developer.android.com/guide/components/fragments.html
 */

import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.content.res.Configuration;
import android.widget.Toast;
import static android.widget.Toast.*;

public class MainActivity extends Activity {

    private Fragment1 fragment1 = null;
    private Fragment2 fragment2 = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Log.d("Lifecycle", "On Create");

        attachFragments();
    }

    private void attachFragments()
    {
        FragmentManager fragmentManager = getFragmentManager();

        FragmentTransaction fragmentTransaction =
                fragmentManager.beginTransaction();

        // This example's original code used DisplayMetrics to determine orientation.
        //   There is an easier way: just ask the Configuration object.
        //
        //---get the current display info---
        //DisplayMetrics display = this.getResources().getDisplayMetrics();
        // int width = display.widthPixels;
        //int height = display.heightPixels;
        // Log.d("SIZE", "Width: " + width + " Height: " + height);
        // if (width > height) {  // There's a better way:

        Configuration config = getResources().getConfiguration();
        if( config.orientation ==  Configuration.ORIENTATION_LANDSCAPE )
        {
            //---landscape mode---
            if( fragment1 == null ) {
                fragment1 = new Fragment1();
            }

            // android.R.id.content refers to the content view of the activity
            fragmentTransaction.replace( android.R.id.content, fragment1);
        }
        else
        {
            //---portrait mode---
            if( fragment2 == null ) {
                fragment2 = new Fragment2();
            }
            fragmentTransaction.replace( android.R.id.content, fragment2);
        }
        fragmentTransaction.commit();
    }

    @Override public void onRestart() {
        super.onRestart();
        Log.d("Lifecycle", "On Restart");
    }

    @Override public void onPause() {
        super.onPause();
        Log.d("Lifecycle", "On Pause");
    }

    @Override public void onStop() {
        super.onStop();
        Log.d("Lifecycle", "On Stop");
    }

    @Override public void onDestroy() {
        super.onDestroy();
        Log.d("Lifecycle", "On Destroy");
    }

    // This function will _not_ run unless the Activity
    //   declaration in the Manifest includes
    //     android:configChanges="orientation|screenSize"
    // When this declaration is present, Activity is _not_
    //   destroyed and re-created on flips, but rather this
    //   function if called. Test this and observer the
    //   log messages from the lifecycle functions of both
    //   this activity and the fragment classes.
    //
    @Override
    public void onConfigurationChanged (Configuration newConfig)
    {
        Log.d("CONFIG:","onConfigurationChanged called");
        super.onConfigurationChanged(newConfig);

        // Checks the orientation of the screen
        if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
            Toast.makeText(this, "landscape", LENGTH_SHORT).show();
        } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
            Toast.makeText(this, "portrait", LENGTH_SHORT).show();
        }

        attachFragments();
    }

}

