package com.ifavine.pay.ui.activity;

import android.app.KeyguardManager;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.PowerManager;
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.common.AppContext;
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);
    }

    /**
     * 唤醒手机屏幕并解锁
     */
    private void wakeUpAndUnlock() {
        // 获取电源管理器对象
        PowerManager pm = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
        boolean screenOn = pm.isScreenOn();
        if (!screenOn) {
            // 获取PowerManager.WakeLock对象,后面的参数|表示同时传入两个值,最后的是LogCat里用的Tag
            PowerManager.WakeLock wl = pm.newWakeLock(
                    PowerManager.ACQUIRE_CAUSES_WAKEUP |
                            PowerManager.SCREEN_BRIGHT_WAKE_LOCK, "bright");
            wl.acquire(1000); // 点亮屏幕
            wl.release(); // 释放
        }
        // 屏幕解锁
        KeyguardManager keyguardManager = (KeyguardManager) mContext.getSystemService(KEYGUARD_SERVICE);
        KeyguardManager.KeyguardLock keyguardLock = keyguardManager.newKeyguardLock("unLock");
        // 屏幕锁定 //keyguardLock.reenableKeyguard();
        keyguardLock.disableKeyguard(); // 解锁
    }


    @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:" + 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;
    }
}
