package com.example.watch.ui.activity

import android.bluetooth.BluetoothAdapter
import android.content.Intent
import android.os.Bundle
import android.widget.Button
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.example.watch.R


class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

//        首次加载
        FirstRun()

//        登录判断
        login()

//        连接心率带
        linkHeartBand()

        //判断是否有权限
//        if (ContextCompat.checkSelfPermission(this,
//                        Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
////请求权限
//            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
//                    MY_PERMISSIONS_REQUEST_ACCESS_COARSE_LOCATION);
////判断是否需要 向用户解释，为什么要申请该权限
//            if(ActivityCompat.shouldShowRequestPermissionRationale(this,
//                            Manifest.permission.READ_CONTACTS)) {
//                Toast.makeText(this, "shouldShowRequestPermissionRationale", Toast.LENGTH_SHORT).show();
//            }
//        }

//        点击按钮Toast
//        val button4 = findViewById<Button>(R.id.CONNECT)
//        button4.setOnClickListener {
//            Toast.makeText(this, "112233!", Toast.LENGTH_LONG).show();
////            获取蓝牙权限
//            val mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter()
//            if (mBluetoothAdapter == null) {
//                // Device does not support Bluetooth
//                Toast.makeText(this, "当前设备不支持蓝牙!", Toast.LENGTH_LONG).show();
//            } else {
//                Toast.makeText(this, "当前设备不支持蓝牙!", Toast.LENGTH_LONG).show();
//            }
//        }


//        跳转BaseActivity
//        val intent = Intent(this, BaseActivity().javaClass)
//        startActivity(intent)


    }


    private fun linkHeartBand() {
        val that = this;
        val linkHeartBandBtn = findViewById<Button>(R.id.linkBtn)
        linkHeartBandBtn.setOnClickListener {
            val mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter()
            if (mBluetoothAdapter == null) {
                Toast.makeText(this, "当前设备不支持蓝牙！", Toast.LENGTH_SHORT).show()
            }
            if (!mBluetoothAdapter.isEnabled) {
                val enableBtIntent = Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE)
                val REQUEST_ENABLE_BT = 1;
                startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT)
            }else{
                val mBluetoothLeScanner = mBluetoothAdapter.bluetoothLeScanner
                Toast.makeText(this, "蓝牙何在？", Toast.LENGTH_SHORT).show()

//                https://www.cnblogs.com/arci/p/8145145.html

//                val bleScanCallback = BleScanCallback(bleScanResults)
//                bleScanCallback.setContext(this.applicationContext)
//                bleScanner.startScan(bleScanCallback)
//                Toast.makeText(this.applicationContext, "蓝牙BLE扫描开始", Toast.LENGTH_SHORT).show()
//                bleScanHandler.postDelayed(bleStopScan, this.BLE_SCAN_PERIOD)
                }
        }
    }




    private fun initView() {

//        val navBatton = findViewById<Button>(R.id.navigation_second)
//        navBatton.setOnClickListener {
//            //        跳到其他页面
//            val historyIntent = Intent(this, SearchActivity::class.java)
//            this.startActivity(historyIntent)
//        }

    }

    private fun login() {
        //        val login = 1
//        if(login == 1){
//            val LoginActivityIntent = Intent(this, LoginActivity::class.java)
//            startActivity(LoginActivityIntent)
//        }
    }

    private fun FirstRun() {
        val sharedPreferences = getSharedPreferences("FirstRun", 0)
        val first_run = sharedPreferences.getBoolean("First", true)
        if (first_run) {
            sharedPreferences.edit().putBoolean("First", false).commit()
            val ViewPageActivityIntent = Intent(this, ViewPageActivity::class.java)
            startActivity(ViewPageActivityIntent)
        } else {
            setContentView(R.layout.activity_main)
        }
    }


}