package tw.moonjuice.light_sesnor_capture;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.os.Environment;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.util.HashMap;
import java.util.Map;

import io.flutter.embedding.android.FlutterActivity;
import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.plugin.common.EventChannel;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;

public class MainActivity extends FlutterActivity implements EventChannel.StreamHandler, SensorEventListener, MethodChannel.MethodCallHandler {
    private Sensor mLightSensor;
    private SensorManager mSensorManager;
    private EventChannel.EventSink mEventSink;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        mLightSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
    }

    @Override
    public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
        super.configureFlutterEngine(flutterEngine);
        EventChannel eventChannel = new EventChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), "tw.moonjuice.light_sensor.stream");
        eventChannel.setStreamHandler(this);
        MethodChannel methodChannel = new MethodChannel(flutterEngine.getDartExecutor(), "tw.moonjuice.light_sensor.method");
        methodChannel.setMethodCallHandler(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        mSensorManager.registerListener(this, mLightSensor, SensorManager.SENSOR_DELAY_FASTEST);
    }

    @Override
    protected void onStop() {
        mSensorManager.unregisterListener(this);
        super.onStop();
    }

    @Override
    public void onListen(Object arguments, EventChannel.EventSink events) {
        mEventSink = events;
    }

    @Override
    public void onCancel(Object arguments) {
        mEventSink = null;
    }

    @Override
    public void onSensorChanged(SensorEvent sensorEvent) {
        if (mEventSink != null) {
            final Map data = new HashMap();
            data.put("lux", sensorEvent.values[0]);
            data.put("timestamp", sensorEvent.timestamp);
            mEventSink.success(data);
        }
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int i) {

    }

    @Override
    public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result result) {
        switch (call.method) {
            case "getExternalDownloadsPath":
                result.success(Environment.getExternalStoragePublicDirectory(
                        Environment.DIRECTORY_DOWNLOADS).getAbsolutePath());
                break;
            case "start":
                Toast.makeText(getContext(), "start", Toast.LENGTH_SHORT).show();
                result.success("success");
                break;
            default:
                Toast.makeText(getContext(), "stop", Toast.LENGTH_SHORT).show();
                result.success("success");
                break;
        }
    }
}
