package com.example.watch.ui.activity
//
//import android.bluetooth.BluetoothDevice
//import android.content.Context
//import android.content.DialogInterface
//import android.content.Intent
//import android.content.pm.PackageInfo
//import android.net.Uri
//import android.os.Build
//import android.os.Bundle
//import android.provider.Settings
//import android.provider.Settings.SettingNotFoundException
//import android.view.View
//import android.widget.Button
//import android.widget.TextView
//import androidx.appcompat.app.AlertDialog
//import com.android.chileaf.fitness.callback.WearManagerCallbacks
//import com.example.watch.R
//import com.yanzhenjie.permission.AndPermission
//import com.yanzhenjie.permission.runtime.Permission
//import timber.log.Timber
//
//class MainActivity : BaseActivity(), ScannerFragment.OnDeviceSelectedListener,
//    WearManagerCallbacks {
//    private var mDeviceConnected = false
//    private var mTvAppVersion: TextView? = null
//    private var mTvDeviceName: TextView? = null
//    private var mTvVersion: TextView? = null
//    private var mTvRssi: TextView? = null
//    private var mTvBattery: TextView? = null
//    private var mTvSport: TextView? = null
//    private var mTvHeartRate: TextView? = null
//    private var mBtnConnect: Button? = null
//    protected fun layoutId(): Int {
//        return R.layout.activity_main
//    }
//
//    protected fun initView() {
//        mTvAppVersion = findViewById(R.id.tv_app_version)
//        mTvDeviceName = findViewById(R.id.tv_device_name)
//        mTvRssi = findViewById(R.id.tv_rssi)
//        mTvVersion = findViewById(R.id.tv_version)
//        mTvBattery = findViewById(R.id.tv_battery)
//        mTvSport = findViewById(R.id.tv_sport)
//        mTvHeartRate = findViewById(R.id.tv_hr)
//        mBtnConnect = findViewById(R.id.btn_connect)
//        mTvAppVersion!!.text = "App Version:$versionName"
//
//        //User information
//        findViewById(R.id.btn_user_info).setOnClickListener({ view ->
//            startActivity(
//                Intent(
//                    this,
//                    UserInfoActivity::class.java
//                )
//            )
//        })
//        //Restoration
//        findViewById(R.id.btn_restoration).setOnClickListener({ view -> mManager.Restoration() })
//        //Get 7 days sport history
//        findViewById(R.id.btn_history_sport).setOnClickListener({ view ->
//            launchHistory(
//                HistoryActivity.TYPE_SPORT
//            )
//        })
//        //Heart rate history record
//        findViewById(R.id.btn_history_heart).setOnClickListener({ view ->
//            launchHistory(
//                HistoryActivity.TYPE_HEART
//            )
//        })
//        //Get the number of steps in the interval
//        findViewById(R.id.btn_interval).setOnClickListener({ view -> launchHistory(HistoryActivity.TYPE_INTERVAL) })
//        //Get historical data for a single key press
//        findViewById(R.id.btn_single).setOnClickListener({ view -> launchHistory(HistoryActivity.TYPE_SINGLE) })
//        mBtnConnect!!.setOnClickListener { view: View? ->
//            if (isBLEEnabled()) {
//                if (!mDeviceConnected) {
//                    showDeviceScanningDialog()
//                } else {
//                    mManager.disConnect()
//                }
//            } else {
//                showBLEDialog()
//            }
//        }
//    }
//
//    protected fun initData(savedInstanceState: Bundle?) {
//        ensureBLESupported()
//        if (!isBLEEnabled()) {
//            showBLEDialog()
//        }
//        mManager.setManagerCallbacks(this)
//    }
//
//    private fun launchHistory(type: Int) {
//        val history = Intent(this, HistoryActivity::class.java)
//        history.putExtra(HistoryActivity.EXTRA_HISTORY, type)
//        startActivity(history)
//    }
//
//    //Scan device
//    private fun showDeviceScanningDialog() {
//        if (isLocationEnabled(this)) {
//            AndPermission.with(this)
//                .runtime()
//                .permission(
//                    Permission.ACCESS_FINE_LOCATION,
//                    Permission.ACCESS_COARSE_LOCATION,
//                    Permission.ACCESS_BACKGROUND_LOCATION
//                )
//                .onGranted({ permissions ->
//                    runOnUiThread {
//                        val dialog: ScannerFragment = ScannerFragment.getInstance()
//                        dialog.show(getSupportFragmentManager(), "scan_fragment")
//                    }
//                })
//                .onDenied({ permissions ->
//                    if (AndPermission.hasAlwaysDeniedPermission(this@MainActivity, permissions)) {
//                        AlertDialog.Builder(this)
//                            .setTitle(getString(R.string.permission_required))
//                            .setMessage(getString(R.string.permission_location_info))
//                            .setPositiveButton("OK",
//                                DialogInterface.OnClickListener { dialog: DialogInterface?, which: Int -> onPermissionSettings() })
//                            .setNegativeButton("Cancel", null)
//                            .show()
//                    }
//                })
//                .start()
//        } else {
//            AlertDialog.Builder(this)
//                .setTitle(getString(R.string.location_permission_title))
//                .setMessage(getString(R.string.location_permission_info))
//                .setPositiveButton("OK",
//                    DialogInterface.OnClickListener { dialog: DialogInterface?, which: Int -> onEnableLocation() })
//                .setNegativeButton("Cancel", null)
//                .show()
//        }
//    }
//
//    fun onEnableLocation() {
//        val intent = Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS)
//        startActivity(intent)
//    }
//
//    fun onPermissionSettings() {
//        val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
//        intent.data = Uri.fromParts("package", getPackageName(), null)
//        startActivity(intent)
//    }
//
//    private fun defaultUI() {
//        mTvDeviceName!!.text = "Device name"
//        mTvVersion!!.text = "Version:--"
//        mTvRssi!!.text = "Rssi:--"
//        mTvBattery!!.text = "Battery:--"
//        mTvSport!!.text = "Sport:--"
//        mTvHeartRate!!.text = "Heart Rate:--"
//        mBtnConnect.setText(getString(R.string.action_connect))
//    }
//
//    private val versionName: String?
//        private get() {
//            try {
//                val packageInfo: PackageInfo =
//                    getPackageManager().getPackageInfo(getPackageName(), 0)
//                return packageInfo.versionName
//            } catch (e: Exception) {
//                e.printStackTrace()
//            }
//            return null
//        }
//
//    fun onDeviceSelected(device: BluetoothDevice?, name: String?) {
//        mManager.connect(device, false)
//        mTvDeviceName.setText(getString(R.string.device_name, name))
//    }
//
//    override fun onError(device: BluetoothDevice, message: String, errorCode: Int) {
//        Timber.e("onError: ($errorCode)")
//        showToast("$message ($errorCode)")
//    }
//
//    override fun onDeviceNotSupported(device: BluetoothDevice) {
//        showToast(getString(R.string.not_supported))
//    }
//
//    override fun onSoftwareVersion(device: BluetoothDevice, software: String) {
//        runOnUiThread { mTvVersion!!.text = "Version:$software" }
//    }
//
//    override fun onRssiRead(device: BluetoothDevice, rssi: Int) {
//        runOnUiThread { mTvRssi!!.text = "Rssi:" + rssi + "dBm" }
//    }
//
//    override fun onBatteryLevelChanged(device: BluetoothDevice, batteryLevel: Int) {
//        runOnUiThread { mTvBattery.setText(getString(R.string.battery, batteryLevel)) }
//    }
//
//    override fun onHeartRateMeasurementReceived(
//        device: BluetoothDevice,
//        heartRate: Int,
//        contactDetected: Boolean?,
//        energyExpanded: Int?,
//        rrIntervals: List<Int>?
//    ) {
//        runOnUiThread {
//            mTvHeartRate.setText(getString(R.string.heart_rate, heartRate))
//            if (rrIntervals != null) {
//                Timber.e("rrIntervals:%s", rrIntervals.toString())
//            }
//        }
//    }
//
//    override fun onSportReceived(device: BluetoothDevice, step: Int, distance: Int, calorie: Int) {
//        runOnUiThread {
//            mTvSport.setText(
//                getString(
//                    R.string.sport,
//                    step,
//                    distance / 100f,
//                    calorie / 10f
//                )
//            )
//        }
//    }
//
//    override fun onDeviceConnected(device: BluetoothDevice) {
//        mDeviceConnected = true
//        runOnUiThread { mBtnConnect.setText(R.string.action_disconnect) }
//    }
//
//    override fun onDeviceDisconnected(device: BluetoothDevice) {
//        runOnUiThread { defaultUI() }
//        mDeviceConnected = false
//        mManager.close()
//    }
//
//    fun onBackPressed() {
//        mManager.disConnect()
//        super.onBackPressed()
//    }
//
//    companion object {
//        fun isLocationEnabled(context: Context): Boolean {
//            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
//                var locationMode = Settings.Secure.LOCATION_MODE_OFF
//                try {
//                    locationMode = Settings.Secure.getInt(
//                        context.contentResolver,
//                        Settings.Secure.LOCATION_MODE
//                    )
//                } catch (e: SettingNotFoundException) {
//                    // do nothing
//                }
//                return locationMode != Settings.Secure.LOCATION_MODE_OFF
//            }
//            return true
//        }
//    }
//}
//
//private fun TextView?.setText(string: Any): Any {
//
//}
