package com.example.watch.ui.activity

import android.bluetooth.BluetoothAdapter
import android.bluetooth.BluetoothManager
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import androidx.annotation.LayoutRes
import androidx.appcompat.app.AppCompatActivity
import com.android.chileaf.WearManager

abstract class BaseActivity : AppCompatActivity() {
    abstract val Timber: Any
    protected var mManager: WearManager? = null
    protected var mLoading: LoadingDialog? = null
    protected var mHandler = Handler(Looper.getMainLooper())
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(layoutId())
        initialize()
        initView()
        initData(savedInstanceState)
    }

    @LayoutRes
    protected abstract fun layoutId(): Int
    protected abstract fun initView()
    protected abstract fun initData(savedInstanceState: Bundle?)
    private fun initialize() {
        val ivBack = findViewById<ImageView>(R.id.iv_toolbar_back)
        ivBack?.setOnClickListener { v: View? -> onBackPressed() }
        mManager = WearManager.getInstance(this)
        mManager.setDebug(BuildConfig.DEBUG)
    }

    protected fun setTitle(title: String?) {
        val tvTitle = findViewById<TextView>(R.id.tv_toolbar_title)
        if (tvTitle != null) {
            tvTitle.text = title
        }
    }

    protected fun launchDetail(type: Int, stamp: Long) {
        val history = Intent(this, HistoryDetailActivity::class.java)
        history.putExtra(HistoryDetailActivity.EXTRA_TYPE, type)
        history.putExtra(HistoryDetailActivity.EXTRA_STAMP, stamp)
        startActivity(history)
    }

    protected fun showLoading(message: String? = getString(R.string.loading)) {
        mLoading = LoadingDialog.Builder(this)
            .setMessage(message)
            .build()
        mLoading.show()
    }

    protected fun showLoadingAutoDismiss(delay: Long) {
        showLoading()
        mHandler.postDelayed({ hideLoading() }, delay)
    }

    protected fun hideLoading() {
        if (mLoading != null && mLoading.isShowing()) {
            mLoading.dismiss()
        }
    }

    protected fun showToast(message: String?) {
        runOnUiThread {
            Toast.makeText(this, message, Toast.LENGTH_SHORT).show()
        }
    }

    protected fun ensureBLESupported() {
        if (!packageManager.hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
            Toast.makeText(this, R.string.no_ble, Toast.LENGTH_LONG).show()
            finish()
        }
    }

    protected val isBLEEnabled: Boolean
        protected get() {
            val bluetoothManager = getSystemService(BLUETOOTH_SERVICE) as BluetoothManager
            val adapter = bluetoothManager.adapter
            return adapter != null && adapter.isEnabled
        }

    protected fun showBLEDialog() {
        val enableIntent = Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE)
        startActivity(enableIntent)
    }
}

class LoadingDialog {

}
