package com.reyun.wandun;

import android.app.Service;
import android.content.Context;
import android.content.SharedPreferences;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.media.AudioManager;
import android.telephony.PhoneStateListener;
import android.telephony.ServiceState;
import android.telephony.SignalStrength;
import android.telephony.TelephonyManager;
import android.util.Log;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import me.weishu.reflection.Reflection;

import static android.hardware.Sensor.TYPE_ROTATION_VECTOR;

public class WdMain {
    private static WdMain mInstance = new WdMain();

    private static String TAG = "WdMain";

    // 用户申请sdk后获得的认证码，调用sdk接口时需传入
    private String mApiKey;

    //生成的唯一id
    private String mWdId;

    //app context
    private Context mContext;

    public static WdMain getInstance(){
        return mInstance;
    }

    public void wdRegisterSensor(Context context, SensorManager sm, Sensor sensor) {
        sm.registerListener(new WdSensorEventListener(context, sm), sensor, Sensor.REPORTING_MODE_ON_CHANGE);
    }

    public void wdRegisterSensors(Context context) {
        SensorManager sensorManager = (SensorManager) context.getSystemService(Service.SENSOR_SERVICE);
        Sensor defaultSensor = sensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY);
        if (defaultSensor != null) {
            wdRegisterSensor(context, sensorManager, defaultSensor);
        }
        Sensor defaultSensor2 = sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
        if (defaultSensor2 != null) {
            wdRegisterSensor(context, sensorManager, defaultSensor2);
        }
        Sensor defaultSensor3 = sensorManager.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR);
        if (defaultSensor3 != null) {
            wdRegisterSensor(context, sensorManager, defaultSensor3);
        }
        Sensor defaultSensor4 = sensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER);
        if (defaultSensor4 != null) {
            wdRegisterSensor(context, sensorManager, defaultSensor4);
        }
        Sensor defaultSensor5 = sensorManager.getDefaultSensor(Sensor.TYPE_PRESSURE);
        if (defaultSensor5 != null) {
            wdRegisterSensor(context, sensorManager, defaultSensor5);
        }
        Sensor defaultSensor6 = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        if (defaultSensor6 != null) {
            wdRegisterSensor(context, sensorManager, defaultSensor6);
        }
        Sensor defaultSensor7 = sensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE);
        if (defaultSensor7 != null) {
            wdRegisterSensor(context, sensorManager, defaultSensor7);
        }
        Sensor defaultSensor8 = sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
        if (defaultSensor8 != null) {
            wdRegisterSensor(context, sensorManager, defaultSensor8);
        }
        Sensor defaultSensor9 = sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
        if (defaultSensor9 != null) {
            wdRegisterSensor(context, sensorManager, defaultSensor9);
        }
    }


    //初始化方法，
    public void init(Context context, String apiKey) {
        mContext = context;
        mApiKey = apiKey;

        //access hidden api above android 9.0+
        Reflection.unseal(context);

        //load native lib
        System.loadLibrary("wdun");
        initNative(context);

        TelephonyManager tm = (TelephonyManager) context.getSystemService(Service.TELEPHONY_SERVICE);
        tm.listen(new PhoneStateListener() {
            @Override
            public void onSignalStrengthsChanged(SignalStrength signalStrength) {
             //   onSignalStrengthChangedNative(signalStrength);
            }
        }, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);

        wdRegisterSensors(context);
    }

    //获取wdId
    public void getWdId(final WdCallback cb) {
        ExecutorService es = Executors.newSingleThreadExecutor();
        es.execute(new Runnable() {
            @Override
            public void run() {
                String wdId;
                //文件中读取
                SharedPreferences sp = mContext.getSharedPreferences("wd_dna", Context.MODE_PRIVATE);
                wdId =  sp.getString(MdUtils.md5("device_id"), null);
                if(wdId == null) {
                    //重新请求服务器获取
                    wdId = getWdIdNative();
                }
                cb.onWdId(wdId);
            }
        });
        es.shutdown();
    }

    public native void initNative(Context context);

    //native层获取wdId
    public native String getWdIdNative();

    //上报传感器变化
    public native void onSensorChangedNative(SensorEvent sensorEvent);

    //上报手机信号强度变化
    public native void onSignalStrengthChangedNative(SignalStrength signalStrength);

    //隔段时间上报风控信息
    public native void reportnative();
}
