package com.creyond.ecgdemo

import android.content.Intent
import android.os.Bundle

import com.google.gson.reflect.TypeToken
import kotlinx.android.synthetic.main.activity_login.*

import android.content.pm.PackageManager
import android.content.Context
import android.os.Handler
import android.view.View
import android.widget.TextView
import com.creyond.sdk.JsonUtil
import com.creyond.sdk.SharedUtil


class LoginActivity : BaseActivity() {
    val userList: MutableList<User> = mutableListOf()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_login)
        hideTitle()
        tv_version.text = "Demo" + versionName(this)

        tv_login.setOnClickListener {
            login(et_name.text.toString(), et_password.text.toString())
        }
        tv_register.setOnClickListener { startActivity(Intent(this@LoginActivity, RegisterActivity::class.java)) }
    }

    override fun onStart() {
        super.onStart()
        val str = SharedUtil.getString(this, LoginActivity.USER_LIST) ?: "[]"
        val list = JsonUtil.fromJson(str, object : TypeToken<List<User>>() {

        })
        userList.clear()
        userList.addAll(list!!)
    }

    internal fun login(name: String, password: String) {
        if (name.isEmpty()) {
            error("登录帐号规定为小于32位，只能为数字、字母")
            return
        }
        if (password.length < 6) {
            error("密码规定为6-16位字母+数字")
            return
        }
        if (userList.isEmpty()) {
            error("输入的账号不存在")
            return
        }
        var user: User? = userList.find { name == it.name }
        if (user == null) {
            error("输入的账号不存在")
            return
        }
        if (password != user.password) {
            error("账号密码错误")
            return
        }
        userList.add(user)
        SharedUtil.putString(this, USER_LIST, JsonUtil.toJson(userList))
        if (cb_auto.isChecked) {
            SharedUtil.putString(this, USER_INFO, JsonUtil.toJson(user))
        }
//        success("登录成功")
//        Handler().postDelayed({
        setResult(RESULT_OK, Intent().putExtra(USER_INFO, user))
        finish()
//        }, 2000)

    }

    override
    fun error(msg: String) {
        val tv_toast = findViewById<TextView>(R.id.tv_error)
        tv_toast.setText(msg)
        tv_toast.setBackgroundColor(-0x10000)
        tv_toast.setVisibility(View.VISIBLE)
        Handler().postDelayed({ tv_toast.setVisibility(View.GONE) }, 2000)
    }

    companion object {
        val USER_INFO = "USER_INFO"
        val USER_LIST = "USER_LIST"
    }

    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
    }


}
