// Copyright 2008 Google Inc.
//
// 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.google.android.stardroid.activities;

import android.app.FragmentManager;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;

import com.google.android.stardroid.ApplicationConstants;
import com.google.android.stardroid.R;
import com.google.android.stardroid.StardroidApplication;
import com.google.android.stardroid.activities.dialogs.EulaDialogFragment;
import com.google.android.stardroid.inject.HasComponent;
import com.google.android.stardroid.util.Analytics;
import com.google.android.stardroid.util.MiscUtil;

import javax.inject.Inject;

/**
 * Shows a splash screen, then launch the next activity.
 */
public class SplashScreenActivity extends InjectableActivity
    implements EulaDialogFragment.EulaAcceptanceListener, HasComponent<SplashScreenComponent> {
  private final static String TAG = MiscUtil.getTag(SplashScreenActivity.class);

  @Inject Analytics analytics;
  @Inject SharedPreferences sharedPreferences;
  @Inject Animation fadeAnimation;
  @Inject EulaDialogFragment eulaDialogFragmentWithButtons;
  @Inject FragmentManager fragmentManager;
  private View graphic;
  private SplashScreenComponent daggerComponent;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    Log.d(TAG, "onCreate");
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splash);
    daggerComponent = DaggerSplashScreenComponent.builder()
        .applicationComponent(getApplicationComponent())
        .splashScreenModule(new SplashScreenModule(this)).build();
    daggerComponent.inject(this);

    graphic = findViewById(R.id.splash);

    fadeAnimation.setAnimationListener(new AnimationListener() {
      public void onAnimationEnd(Animation arg0) {
        Log.d(TAG, "onAnimationEnd");
        graphic.setVisibility(View.INVISIBLE);
        Intent intent = new Intent(SplashScreenActivity.this, DynamicStarMapActivity.class);
        startActivity(intent);
        finish();
      }

      public void onAnimationRepeat(Animation arg0) {
      }

      public void onAnimationStart(Animation arg0) {
        Log.d(TAG, "SplashScreen.Animcation onAnimationStart");
      }
    });
  }

  @Override
  public void onResume() {
    super.onResume();
    boolean eulaShown = maybeShowEula();
    if (!eulaShown) {
      // User has previously accepted - let's get on with it!
      graphic.startAnimation(fadeAnimation);
    }
  }

  @Override
  public void onStart() {
    super.onStart();
    analytics.trackPageView(Analytics.SPLASH_SCREEN_ACTIVITY);
  }

  @Override
  public void onPause() {
    Log.d(TAG, "onPause");
    super.onPause();
  }

  @Override
  public void onDestroy() {
    Log.d(TAG, "onDestroy");
    super.onDestroy();
  }

  private boolean maybeShowEula() {
    int versionCode = ((StardroidApplication) getApplication()).getVersion();
    boolean eulaAlreadyConfirmed = (sharedPreferences.getInt(
        ApplicationConstants.READ_TOS_PREF_VERSION, -1) == versionCode);
    if (!eulaAlreadyConfirmed) {
      // If we don't make this check there can be crashes when the app is backgrounded and
      // brought back if the Activity has been killed.  This seems to be a framework bug.
      if (!eulaDialogFragmentWithButtons.isAdded()) {
        eulaDialogFragmentWithButtons.show(fragmentManager, "Eula Dialog");
      }
      return true;
    } else {
      return false;
    }
  }

  @Override
  public void eulaAccepted() {
    int versionCode = ((StardroidApplication) getApplication()).getVersion();
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putInt(ApplicationConstants.READ_TOS_PREF_VERSION, versionCode);
    editor.commit();
    // Let's go.
    View graphic = findViewById(R.id.splash);
    graphic.startAnimation(fadeAnimation);
  }

  @Override
  public void eulaRejected() {
    Log.d(TAG, "Sorry chum, no accept, no app.");
    finish();
  }

  @Override
  public SplashScreenComponent getComponent() {
    return daggerComponent;
  }
}

@@ -3759,245 +3759,8 @@
 ) %7B%0A
-      // If we don't make this check there can be crashes when the app is backgrounded and%0A      // brought back if the Activity has been killed.  This seems to be a framework bug.%0A      if (!eulaDialogFragmentWithButtons.isAdded()) %7B%0A  
     
@@ -3825,32 +3825,24 @@
 a Dialog%22);%0A
-      %7D%0A
       return

Clean up a now unneeded check.