package com.yzx.ebrand.fragment

import android.text.InputType
import com.blankj.utilcode.util.KeyboardUtils
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.ScreenUtils
import com.qmuiteam.qmui.widget.dialog.QMUIDialog
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog
import com.yzx.ebrand.R
import com.yzx.ebrand.activity.WebActivity
import com.yzx.ebrand.config.Config
import com.yzx.ebrand.fragment.base.BaseFragment
import com.yzx.ebrand.model.TeacherInfo
import com.yzx.ebrand.model.TimeShow
import com.yzx.ebrand.model.User
import com.yzx.ebrand.presenter.MyPresenter
import com.yzx.ebrand.presenter.MyView
import com.yzx.ebrand.utils.hideNavigationBar
import kotlinx.android.synthetic.main.fragment_my.*
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
import kotlin.system.exitProcess

class MyFragment : BaseFragment<MyPresenter>(), MyView {

    companion object {

        @JvmStatic
        fun getNewInstance() =
            MyFragment()
    }

    private var count = 0
    private val run = Runnable {
        count = 0
    }

    override val inflateId: Int
        get() = R.layout.fragment_my

    override fun initView() {
        sysExitBtn.setOnClickListener {
            count++
            sysExitBtn.removeCallbacks(run)
            sysExitBtn.postDelayed(run, 1000)
            if (count >= 5) {
                showExitDialog()
            }
        }
    }

    private fun showExitDialog() {
        val builder = QMUIDialog.EditTextDialogBuilder(activity)

        builder.setPlaceholder("输入密码")
            .setTitle("请输入密码")
            .setInputType(InputType.TYPE_NUMBER_VARIATION_PASSWORD or InputType.TYPE_CLASS_NUMBER)
            .addAction(
                "取消"
            ) { dialog, index ->
                KeyboardUtils.hideSoftInput(builder.editText)
                dialog?.dismiss()
            }
            .addAction(
                "确定"
            ) { dialog, index ->
                val text = builder.editText.text
                LogUtils.d(text)
                LogUtils.d("112233".equals(text))
                if ("112233" == text.toString()) {
                    activity?.finish()
                    exitProcess(0)
                } else {
//                    ToastUtils.showShort("密码不正确")
                    showPwdErrorTip()
                }
            }
            .setCancelable(false)
            .setCanceledOnTouchOutside(false)
        val dialog = builder.show()

        dialog.hideNavigationBar()
    }

    private fun showPwdErrorTip() {
        val tipDialog = QMUITipDialog.Builder(activity)
            .setIconType(QMUITipDialog.Builder.ICON_TYPE_FAIL)
            .setTipWord("密码错误")
            .create()
        tipDialog.show()
        tipDialog.hideNavigationBar()
        sysExitBtn.postDelayed({
            tipDialog.dismiss()
        }, 1500)
    }

    override fun initData() {

        density.text =
            "density:${ScreenUtils.getScreenDensity()},dpi:${ScreenUtils.getScreenDensityDpi()}"

        logoutBtn.setOnClickListener {
            activity?.apply {
                User.clearUserInfo()
                WebActivity.active(this, "${Config.OA_URL}/login?logout=1")
                finish()
            }
        }
    }

    override fun onResume() {
        super.onResume()
        mPresenter?.getTeacherInfo(User.getUser().user_id)
    }

    override fun initPresenter(): MyPresenter = MyPresenter(this)
    override fun onUserInfoSuccess(info: TeacherInfo) {
        userName.text = info.teacher_name
        sex.text = when (info.sex) {
            1 -> "男"
            2 -> "女"
            else -> "--"
        }
        phone.text = info.phone
        idCard.text = info.card_no

        if (info.teacherUnits.isNotEmpty()) {
            val unit = info.teacherUnits[0]
            if (unit.teacherUnitGroups.isNotEmpty()) {
                val group = unit.teacherUnitGroups[0]
                unitName.text = unit.unit_name
                groupName.text = group.group_name
                jobType.text = if (group.job_type_name.isNotEmpty()) {
                    group.job_type_name
                } else {
                    "-"
                }
                isLeader.text = if (group.is_leader == 0) {
                    "是"
                } else {
                    "否"
                }
                jobName.text = group.job_name
            }


        }

    }

    override fun onUserInfoError(error: String) {

    }


    override fun onStop() {
        super.onStop()
        EventBus.getDefault().unregister(this)
    }


    override fun onStart() {
        super.onStart()
        EventBus.getDefault().register(this)
    }

    //接收消息
    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onMessageEvent(event: TimeShow) {
        timeView.setTime(event)
    }

}