package com.ifavine.pay.ui.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.WindowManager;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.ImageView;


import com.ifavine.pay.R;
import com.ifavine.pay.ui.base.BaseActivity;
import com.ifavine.pay.utils.SPUtil;

import butterknife.BindView;

/**
 * Powered by www.ifavine.com
 */
public class WelcomeActivity extends BaseActivity {

    @BindView(R.id.iv_image)
    ImageView ivImage;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_welcome;
    }


    @Override
    protected void onCreate(Bundle paramBundle) {
        super.onCreate(paramBundle);
        setToolbar(TOOL_BAR_TYPE_NONE, -1);
        getWebAgent();
    }

    @Override
    public void initView() {
        super.initView();
    }

    @Override
    public void initData() {
        super.initData();

        handler.sendEmptyMessageDelayed(0, 800);
        // 全屏
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);

    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            autoLogin();
            finish();
        }
    };

    @Override
    protected void onPause() {
        super.onPause();
        handler.removeMessages(0);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        handler.removeMessages(0);
    }

    private void autoLogin() {
        boolean isShow = SPUtil.getInstance().readBoolean("ShowGuide");
        if (isShow) {
            boolean login = SPUtil.getInstance().readBoolean("user.autoLogin");
            if (login) {
                gotoActivity(MainActivity.class);
            } else {
                //   gotoActivity(LoginActivity.class);
            }
        } else {
            gotoActivity(MainActivity.class);
        }
    }

    private String getWebAgent() {
        WebView webview = new WebView(WelcomeActivity.this);
        WebSettings settings = webview.getSettings();
        // 获取到UserAgentString
        String userAgent = settings.getUserAgentString();
        Log.i("aaaaa", "userAgent:" + userAgent);
        SPUtil.getInstance().writeString("UserAgent", userAgent);
        return userAgent;
    }
}
