package com.danding.yang800;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;

import com.danding.yang800.Presenter.LogoActivityPresenter;
import com.danding.yang800.Util.LogUtil;
import com.danding.yang800.Util.ToastUtil;
import com.qiyukf.nimlib.sdk.NimIntent;
import com.qiyukf.unicorn.api.ConsultSource;
import com.qiyukf.unicorn.api.ProductDetail;
import com.qiyukf.unicorn.api.Unicorn;

import java.util.Timer;
import java.util.TimerTask;

public class LogoActivity extends Activity {
    private ImageView logo;
    SharedPreferences sp;
    LogoActivityPresenter presenter;
    public boolean isLogin = false;
    Animation anim;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_logo);

        if (!isTaskRoot()) {
            finish();
            return;
        }
//        Intent intent = getIntent();
//        if (intent.hasExtra(NimIntent.EXTRA_NOTIFY_CONTENT)) {
//            // 打开客服窗口
//            String title = "洋800客服";
//            /**
//             * 设置访客来源，标识访客是从哪个页面发起咨询的，用于客服了解用户是从什么页面进入。
//             * 三个参数分别为：来源页面的url，来源页面标题，来源页面额外信息（可自由定义）。
//             * 设置来源后，在客服会话界面的"用户资料"栏的页面项，可以看到这里设置的值。
//             */
//            ConsultSource source = new ConsultSource("", "111", "custom information string");
//            /**
//             * 请注意： 调用该接口前，应先检查Unicorn.isServiceAvailable()，
//             * 如果返回为false，该接口不会有任何动作
//             *
//             * @param context 上下文
//             * @param title   聊天窗口的标题
//             * @param source  咨询的发起来源，包括发起咨询的url，title，描述信息等
//             */
//            ProductDetail.Builder pb = new ProductDetail.Builder();
//            // pb.setUrl("http://www.163.com");
//            pb.setDesc("name");
//            pb.setNote("￥" + 100);
//            pb.setTitle("海彼购客服");
//            pb.setShow(1);
//            //pb.setShow(1);
//            pb.setPicture("https://gss0.bdstatic.com/-4o3dSag_xI4khGkpoWK1HF6hhy/baike/w%3D268%3Bg%3D0/sign=35ac0a83c6cec3fd8b3ea073eeb3b302/6159252dd42a2834b825c1445eb5c9ea14cebffa.jpg");
//            pb.setAlwaysSend(true);
//            source.productDetail = pb.create();
//            Unicorn.openServiceActivity(this, title, source);
//            // 最好将intent清掉，以免从堆栈恢复时又打开客服窗口
//            setIntent(new Intent());
//        }
        sp = getSharedPreferences("variable", Context.MODE_PRIVATE);
        logo = (ImageView) findViewById(R.id.logo_Iv);
        presenter = new LogoActivityPresenter(this);
        if (sp.getBoolean("login", false)) {
            presenter.keepLogin();
        }
        anim = AnimationUtils.loadAnimation(this, R.anim.logo_anim);
        anim.setAnimationListener(new Animation.AnimationListener() {
                                      @Override
                                      public void onAnimationStart(Animation animation) {

                                      }

                                      @Override
                                      public void onAnimationRepeat(Animation animation) {

                                      }

                                      @Override
                                      public void onAnimationEnd(Animation animation) {
                                          LogUtil.d(isLogin + "isLogin" + sp.getBoolean("isFirstRun", true));
                                          if (isLogin && !sp.getBoolean("isFirstRun", true)) {
                                              logo.setVisibility(View.INVISIBLE);
                                              Intent intent = new Intent(LogoActivity.this, MainActivity.class);
                                              startActivity(intent);
                                              finish();
                                          } else if (!isLogin && !sp.getBoolean("isFirstRun", true)) {
                                              sp.edit().putBoolean("login", false).commit();
                                              logo.setVisibility(View.INVISIBLE);
                                              Intent intent = new Intent(LogoActivity.this, LoginActivity.class);
                                              startActivity(intent);
                                              finish();
                                          } else {
                                              logo.setVisibility(View.INVISIBLE);
                                              Intent intent = new Intent(LogoActivity.this, LeadActivity.class);
                                              startActivity(intent);
                                              finish();
                                          }
                                      }
                                  }

        );
        Timer timer = new Timer();
        TimerTask tast = new TimerTask() {
            @Override
            public void run() {
                runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        logo.startAnimation(anim);
                    }
                });
            }
        };
        timer.schedule(tast, 2 * 1000);

    }
}
