package com.joker.fourfun.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.ScaleAnimation;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.animation.ViewPropertyAnimation;
import com.joker.fourfun.Constants;
import com.joker.fourfun.R;
import com.joker.fourfun.base.BaseMvpActivity;
import com.joker.fourfun.model.Picture;
import com.joker.fourfun.presenter.SplashPresenter;
import com.joker.fourfun.presenter.contract.SplashContract;

import butterknife.BindView;

public class SplashActivity extends BaseMvpActivity<SplashContract.View, SplashPresenter> implements
        SplashContract.View {
    @BindView(R.id.iv_content)
    ImageView mContentImageView;
    private String mZhihuImg;
    private Picture mPicture;
    public ScaleAnimation mAnimation;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN
                | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
        super.onCreate(savedInstanceState);
        mContentImageView = (ImageView) findViewById(R.id.iv_content);

        mPresenter.getZhihuPic();
    }

    @Override
    protected void setContentViewAndInject(Bundle savedInstanceState) {
        setContentView(R.layout.activity_splash);
        getComponent().inject(this);
    }

    private void start2mainActivity() {
        // MediaFragment 背景图
        Bundle mediaBundle = new Bundle();
        mediaBundle.putString(Constants.ZHIHU_IMG, mZhihuImg);
        // PictureFragment 第一个 fragment 的数据
        Bundle pictureBundle = new Bundle();
        pictureBundle.putParcelable(Constants.PICTURE_ONE_IMG, mPicture);

        Bundle bundle = new Bundle();
        bundle.putBundle(Constants.MEDIA_BUNDLE, mediaBundle);
        bundle.putBundle(Constants.PICTURE_BUNDLE, pictureBundle);

        Intent intent = new Intent(SplashActivity.this, MainActivity.class);
        intent.putExtra(Constants.MAIN_ACTIVITY_BUNDLE, bundle);
        startActivity(intent);
        finish();
    }

    @Override
    public void showZhihuPic(String url) {
        Glide.with(this)
                .load(url)
                .animate(new ViewPropertyAnimation.Animator() {
                    @Override
                    public void animate(View view) {
                        mAnimation = new ScaleAnimation(1.0f, 1.5f, 1.0f, 1.5f, Animation
                                .RELATIVE_TO_SELF, 0.5f, Animation
                                .RELATIVE_TO_SELF, 0.5f);
                        mAnimation.setFillAfter(true);
                        mAnimation.setDuration(3000);
                        mAnimation.setAnimationListener(new Animation.AnimationListener() {
                            @Override
                            public void onAnimationStart(Animation animation) {
                            }

                            @Override
                            public void onAnimationEnd(Animation animation) {
                                start2mainActivity();
                            }

                            @Override
                            public void onAnimationRepeat(Animation animation) {
                            }
                        });
                        view.startAnimation(mAnimation);
                    }
                })
                .diskCacheStrategy(DiskCacheStrategy.RESULT)
                .centerCrop()
                .into(mContentImageView);
    }

    @Override
    public void setMediaBackground(String url) {
        this.mZhihuImg = url;
    }

    @Override
    public void setPictureOne(Picture picture) {
        this.mPicture = picture;
    }

    @Override
    public void showError(String message) {
        mZhihuImg = "";
        mAnimation.cancel();
//        SystemUtil.showToast(this, message);
        start2mainActivity();
    }
}

@@ -28,39 +28,8 @@
 i;%0A%0A
-import android.content.Intent;%0A
 impo
@@ -50,16 +50,16 @@
 Bundle;%0A
+
 import a
@@ -465,16 +465,61 @@
 tivity;%0A
+import com.joker.fourfun.login.LoginContext;%0A
 import c
@@ -661,16 +661,63 @@
 ontract;
+%0Aimport com.joker.fourfun.utils.PreferenceUtil;
 %0A%0Aimport
@@ -871,16 +871,54 @@
 .View %7B%0A
+    public ScaleAnimation mAnimation;%0A
     @Bin
@@ -1036,46 +1036,8 @@
 ure;
-%0A    public ScaleAnimation mAnimation;
 %0A%0A  
@@ -1437,32 +1437,281 @@
 huPic();%0A    %7D%0A%0A
+    @Override%0A    protected void onStart() %7B%0A        super.onStart();%0A        // %E4%BB%8E sharedPreference %E8%8E%B7%E5%8F%96%E7%99%BB%E5%BD%95%E7%8A%B6%E6%80%81%0A        boolean isLogin = PreferenceUtil.getDefaultFalse(Constants.LOGIN_STATE);%0A        LoginContext.getInstance().setState(isLogin);%0A    %7D%0A%0A
     @Override%0A  
@@ -2394,49 +2394,21 @@
     
-Intent intent = new Intent(Splash
+start
 Activity
 .thi
@@ -2407,15 +2407,9 @@
 vity
-.this, 
+(
 Main
@@ -2421,109 +2421,33 @@
 ity.
-class);%0A        intent.putExtra(Constants.MAIN_ACTIVITY_BUNDLE, bundle);%0A        startActivity(intent
+newInstance(this, bundle)
 );%0A 

fix bug