package com.creyond.ecgdemo

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Bundle
import android.view.View
import android.widget.Toast
import com.creyond.sdk.SharedUtil
import kotlinx.android.synthetic.main.activity_other.*

class OtherActivity : BaseActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_other)
        title = "其他"
        tv_version.text = "Demo" + versionName(this)
        tv_sdk_version.text = versionName(this)
        fl_result.setOnClickListener(View.OnClickListener {
            var record: String? = SharedUtil.getString(this, "record")
            if (record == null) {
                Toast.makeText(this, "暂无心电记录", Toast.LENGTH_SHORT).show()
                return@OnClickListener
            }

            startActivity(Intent(this@OtherActivity, PlaybackActivity::class.java))
        })
        tv_screen_on.setOnCheckedChangeListener { buttonView, isChecked ->
            SharedUtil.putBoolean(this, "screen_on", isChecked);
        }
        tv_exit.setOnClickListener {
            if (DemoApplication.recording) {
                error("当前正在记录，请稍后退出")
                return@setOnClickListener
            }
            SharedUtil.remove(this, LoginActivity.USER_INFO)
            setResult(Activity.RESULT_OK)
            finish()
        }
    }

    fun versionName(context: Context): String? {
        val manager = context.packageManager
        var name: String? = null
        try {
            val info = manager.getPackageInfo(context.packageName, 0)
            name = info.versionName
        } catch (e: PackageManager.NameNotFoundException) {
            e.printStackTrace()
        }

        return name
    }
}
