package com.example.ben.zhihudaily.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.ben.zhihudaily.R;
import com.example.ben.zhihudaily.data.entity.StartImage;
import com.example.ben.zhihudaily.network.BenFactory;
import com.example.ben.zhihudaily.ui.base.BaseActivity;
import com.example.ben.zhihudaily.utils.GlideUtils;

import java.util.concurrent.TimeUnit;

import butterknife.Bind;
import butterknife.ButterKnife;
import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

/**
 * Created by Zhou bangquan on 16/10/8.
 */


public class StartActivity extends BaseActivity {

    @Bind(R.id.start_image)
    ImageView mStartImageView;
    @Bind(R.id.image_author)
    TextView mAuthorTextView;
    @Bind(R.id.start_image_layout)
    RelativeLayout mStartLayout;
    @Bind(R.id.bottom_animation_layout)
    RelativeLayout mBottomLayout;
    private int imageLayoutHeight = 0;

    @Override
    protected int provideContentViewId() {
        return R.layout.start_layout;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
        getHeights();
        setImageLayout();
        translateBottonLayout();
    }

    private void loadStartImage() {
        String size = App.screenWidth + "*" + imageLayoutHeight;
        unsubscribe();
        subscription = BenFactory.getStoryApi()
                .getStartImage(size)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<StartImage>() {
                    @Override
                    public void call(StartImage startImage) {
                        GlideUtils.loadingImage(mContext, mStartImageView, startImage.img);
                        mAuthorTextView.setText(startImage.text);
                        Observable.timer(2, TimeUnit.SECONDS).subscribe(new Action1<Long>() {
                            @Override
                            public void call(Long aLong) {
                                startActivity(new Intent(mContext, MainActivity.class));
                                finish();
                            }
                        });
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {

                    }
                });
    }

    private void setImageLayout() {
        mStartLayout.getLayoutParams().height = imageLayoutHeight;
    }

    private void translateBottonLayout() {
        float fromY = App.screenHeight;
        float toY = App.screenHeight - (mBottomLayout.getLayoutParams().height) - App.statusBarHeight;
        TranslateAnimation animation = new TranslateAnimation(0, 0, fromY, toY);
        animation.setDuration(1000);
        animation.setFillAfter(true);
        mBottomLayout.startAnimation(animation);

        Observable.timer(1, TimeUnit.SECONDS).subscribe(new Action1<Long>() {
            @Override
            public void call(Long aLong) {
                loadStartImage();
            }
        });

    }

    private void getHeights() {
        imageLayoutHeight = (int) (App.screenHeight - (mBottomLayout.getLayoutParams().height) - App.statusBarHeight - 5 * App.screenDensity);
    }

    @Override
    public void onBackPressed() {
    }
}

@@ -660,24 +660,48 @@
 Observable;%0A
+import rx.Subscription;%0A
 import rx.an
@@ -736,16 +736,16 @@
 dulers;%0A
-
 import r
@@ -1536,24 +1536,50 @@
 onLayout();%0A
+        loadStartImage();%0A
     %7D%0A%0A    p
@@ -1824,24 +1824,68 @@
 ulers.io())%0A
+                .delay(1, TimeUnit.SECONDS)%0A
             
@@ -1948,25 +1948,24 @@
        .
-subscribe
+doOnNext
 (new Act
@@ -2259,49 +2259,148 @@
     
-    Observable.timer(2, TimeUnit.SECONDS)
+%7D%0A                %7D)%0A                .delay(3, TimeUnit.SECONDS)%0A                .observeOn(AndroidSchedulers.mainThread())%0A                
 .sub
@@ -2410,36 +2410,42 @@
 ibe(new Action1%3C
-Long
+StartImage
 %3E() %7B%0A          
@@ -2450,32 +2450,24 @@
             
-   
-     
 @Override%0A  
@@ -2460,32 +2460,24 @@
   @Override%0A
-        
             
@@ -2501,38 +2501,41 @@
 id call(
-Long aLong) %7B%0A        
+StartImage startImage) %7B%0A
         
@@ -2568,148 +2568,8 @@
 ity(
-new Intent(mContext, MainActivity.class));%0A                                finish();%0A                            %7D%0A                        %7D
 );%0A 
@@ -2725,17 +2725,57 @@
 able) %7B%0A
+                        startActivity();
 %0A
-
         
@@ -2807,32 +2807,157 @@
      %7D);%0A    %7D%0A%0A
+    private void startActivity() %7B%0A        startActivity(new Intent(mContext, MainActivity.class));%0A        finish();%0A    %7D%0A%0A
     private void
@@ -3388,24 +3388,24 @@
 fter(true);%0A
+
         mBot
@@ -3445,213 +3445,8 @@
 n);%0A
-%0A        Observable.timer(1, TimeUnit.SECONDS).subscribe(new Action1%3CLong%3E() %7B%0A            @Override%0A            public void call(Long aLong) %7B%0A                loadStartImage();%0A            %7D%0A        %7D);%0A%0A
     

improve StartActivity