package com.ifavine.pay.ui.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
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.MD5Util;
import com.ifavine.pay.utils.SPUtil;
import com.ifavine.pay.utils.WifiAdmin;

import butterknife.BindView;

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

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

    @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();
        // 全屏
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        wifiAdmin = new WifiAdmin(mContext);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                autoLogin();
            }
        }, 1000);
    }


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

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

    private void autoLogin() {
        boolean autoLogin = SPUtil.getInstance().readBoolean("autoLogin");
        String Mac = wifiAdmin.GetMacAddress();
        if (!autoLogin) {
            SPUtil.getInstance().writeBoolean("autoLogin", true);
            String pwd = MD5Util.MD5("12345");
            SPUtil.getInstance().writeString("pwd", pwd);
        }
        if (!TextUtils.isEmpty(Mac)) {
            SPUtil.getInstance().writeString("Mac", Mac);
            SPUtil.getInstance().writeString("DeviceNo", MD5Util.MD5(Mac));
        }
        Log.i("ggg", Mac);
        gotoActivity(MainActivity.class);
        finish();
    }

    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;
    }
}
