package com.xypower.mpmaster;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.ConnectivityManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.StrictMode;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Switch;
import android.widget.TextView;


import com.dev.devapi.api.SysApi;
import com.xypower.common.HotspotManager;
import com.xypower.common.MicroPhotoContext;

import java.text.SimpleDateFormat;
import java.util.Date;


public class MainActivity extends AppCompatActivity {

    private static int MY_PERMISSIONS_REQUEST_FOREGROUND_SERVICE = 100;

    private Handler mHandler;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ActionBar actionBar = getSupportActionBar();

        mHandler = new Handler();
        mHandler.postDelayed(
                new Runnable() {
                    public void run() {
                        requestPermissions();
                    }
                }, 100);

        // String buildTime = BuildConfig.BUILD_
        Date date = new Date(BuildConfig.BUILD_TIMESTAMP);
        // SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat sdf = new SimpleDateFormat("MM-dd HH:mm");
        actionBar.setTitle(actionBar.getTitle().toString() + " v" + MicroPhotoContext.getVersionName(getApplicationContext()) + " " + sdf.format(date));

        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);

        MicroPhotoContext.AppConfig appConfig = MicroPhotoContext.getMpAppConfig(getApplicationContext());
        ((TextView)findViewById((R.id.cmdid))).setText(TextUtils.isEmpty(appConfig.cmdid) ? "" : appConfig.cmdid);

        findViewById(R.id.btnEnableCam3V3).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SysApi.setCam3V3Enable(true);
            }
        });

        findViewById(R.id.btnDisableCam3V3).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SysApi.setCam3V3Enable(false);
            }
        });

        findViewById(R.id.btnTurnOtgOn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SysApi.setOtgState(true);
            }
        });

        findViewById(R.id.btnTurnOtgOff).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SysApi.setOtgState(false);
            }
        });

        findViewById(R.id.btnTurnAppOn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // SysApi.enableApp(v.getContext(), "abcd1234");



            }
        });

        findViewById(R.id.btnTurn485On).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SysApi.setRS485Enable(true);
                SysApi.set12VEnable(true);
            }
        });

        findViewById(R.id.btnTurn485Off).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SysApi.set12VEnable(false);
                SysApi.setRS485Enable(false);
            }
        });

        Switch sw = (Switch)findViewById(R.id.btn485ReadMode);
        sw.setChecked(true);
        findViewById(R.id.btn485ReadMode).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Switch sw = (Switch)view;
                if (sw.isChecked()) {
                    SysApi.set485ReadMode();
                } else {
                    SysApi.set485WriteMode();
                }
            }
        });


        mHandler.postDelayed(
                new Runnable() {
                    public void run() {
                        HotspotManager.enableHotspot(getApplicationContext(), false);
                    }
                }, 5000);

        startMicroPhotoService(getApplicationContext());
    }

    private void requestPermissions() {
        String[] accessPermissions = new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.FOREGROUND_SERVICE, Manifest.permission.READ_PHONE_STATE,
                /*Manifest.permission.PACKAGE_USAGE_STATS,*/
                /*Manifest.permission.SET_TIME,*/};
        boolean needRequire = false;
        for (String access : accessPermissions) {
            int curPermission = ActivityCompat.checkSelfPermission(MainActivity.this, access);
            if (curPermission != PackageManager.PERMISSION_GRANTED) {
                needRequire = true;
                break;
            }
        }
        if (needRequire) {
            ActivityCompat.requestPermissions(MainActivity.this, accessPermissions, MY_PERMISSIONS_REQUEST_FOREGROUND_SERVICE);
            return;
        }
    }

    public static void startMicroPhotoService(Context context) {

        MicroPhotoContext.AppConfig appConfig = MicroPhotoContext.getMpAppConfig(context);

        Intent intent = new Intent(context, MpMasterService.class);
        intent.setAction(MpMasterService.ACTION_START);
        intent.putExtra("cmdid", appConfig.cmdid);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            context.startForegroundService(intent);
        } else {
            context.startService(intent);
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            // Back pressed
            return true; // disable it
        }

        return super.onKeyDown(keyCode, event);
    }


}