package io.github.lewismcgeary.androidgameoflife;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.ActivityOptionsCompat;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.util.Pair;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.CardView;
import android.view.Surface;
import android.view.View;
import android.widget.ImageView;

public class IntroActivity extends AppCompatActivity implements IntroFragment.OnFragmentInteractionListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_intro);
        IntroFragment fragment = IntroFragment.newInstance(null, null);
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.container, fragment);
        fragmentTransaction.commit();
        /** Button letsPlayButton = (Button)findViewById(R.id.lets_play_button);
        final AppBarLayout appBarLayout = (AppBarLayout)findViewById(R.id.app_bar_layout);
        letsPlayButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setFixedScreenOrientation();
                startTransition();
                //appBarLayout.setExpanded(false, true);
            }
        });*/
    }

    @Override
    protected void onResume() {
        //allows screen to rotate again. Screen orientation is fixed while transitioning between
        //activities
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
        super.onResume();
    }

    private void startTransition(){
        CardView introCardView = (CardView)findViewById(R.id.intro_card_view);
        ImageView logo = (ImageView)findViewById(R.id.app_logo);
        Intent intent = new Intent(this, LifeGameActivity.class);
        Pair<View, String> pair1 = Pair.create((View)introCardView, getString(R.string.card_view_transition_name));
        Pair<View, String> pair3 = Pair.create((View)logo, getString(R.string.logo_transition_name));
        ActivityOptionsCompat options =
                ActivityOptionsCompat.makeSceneTransitionAnimation(this, pair1, pair3);
        ActivityCompat.startActivity(this, intent, options.toBundle());
    }

    private void setFixedScreenOrientation(){
        //setting fixed orientation before switching to the next activity avoids an error if device
        //is in a different orientation when back button is pressed to return to this activity
        int orientation = getWindowManager().getDefaultDisplay().getRotation();

        switch(orientation) {
            case Surface.ROTATION_180:
                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);
                break;
            case Surface.ROTATION_270:
                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
                break;
            case  Surface.ROTATION_0:
                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
                break;
            case Surface.ROTATION_90:
                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
                break;
        }
    }

    @Override
    public void onFragmentInteraction(Uri uri) {

    }

    @Override
    public void letsPlay() {
        setFixedScreenOrientation();
        startTransition();
    }
}

@@ -48,39 +48,8 @@
 e;%0A%0A
-import android.content.Intent;%0A
 impo
@@ -84,16 +84,16 @@
 tyInfo;%0A
+
 import a
@@ -161,81 +161,33 @@
 ort.
-v4.app.ActivityCompat;%0Aimport android.support.v4.app.ActivityOptionsCompa
+design.widget.AppBarLayou
 t;%0Ai
@@ -287,45 +287,8 @@
 on;%0A
-import android.support.v4.util.Pair;%0A
 impo
@@ -408,34 +408,8 @@
 ce;%0A
-import android.view.View;%0A
 impo
@@ -545,18 +545,98 @@
 Listener
- %7B
+, LifeGridFragment.OnFragmentInteractionListener %7B%0A%0A    AppBarLayout appBarLayout;
 %0A%0A    @O
@@ -1119,24 +1119,96 @@
 n.commit();%0A
+        appBarLayout = (AppBarLayout)findViewById(R.id.app_bar_layout);%0A
         /** 
@@ -2132,24 +2132,28 @@
 go);%0A       
+ /**
  Intent inte
@@ -2616,16 +2616,417 @@
 ndle());
+*/%0A        appBarLayout.setExpanded(false, true);%0A        LifeGridFragment lifeGridFragment = LifeGridFragment.newInstance(null, null);%0A        FragmentManager fragmentManager = getSupportFragmentManager();%0A        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();%0A        fragmentTransaction.replace(R.id.container, lifeGridFragment);%0A        fragmentTransaction.commit();
 %0A    %7D%0A%0A
@@ -3996,32 +3996,32 @@
 %0A%0A    @Override%0A
-
     public void 
@@ -4053,32 +4053,271 @@
  uri) %7B%0A%0A    %7D%0A%0A
+    @Override%0A    public void gameOver() %7B%0A%0A    %7D%0A%0A    @Override%0A    public void noCellsWereSelected() %7B%0A%0A    %7D%0A%0A    @Override%0A    public void cellDrawingInProgress() %7B%0A%0A    %7D%0A%0A    @Override%0A    public void cellDrawingFinished() %7B%0A%0A    %7D%0A%0A
     @Override%0A  

load intro and game fragments using same Activity