package com.example.chemical.ui.login

import android.content.IntentFilter
import android.hardware.usb.UsbManager
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.view.KeyEvent
import android.view.LayoutInflater
import android.view.MotionEvent
import android.view.View
import android.widget.TextView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.request.RequestOptions
import com.example.chemical.ChemicalApp
import com.example.chemical.R
import com.example.chemical.databinding.ActivitySwipeBinding
import com.example.chemical.receiver.OnSerialScanListener
import com.example.chemical.receiver.PortScanHelper
import com.example.chemical.receiver.UsbReceiver
import com.example.chemical.ui.MainActivity
import com.example.chemical.ui.common.BaseCountDownActivity
import com.example.chemical.utils.MediaPlayerHelper
import com.example.chemical.utils.UiManager
import com.example.chemical.weidith.AuthenticationDialog
import com.example.chemical.weidith.CustomDialog
import com.rc.core.log.RcLog
import com.rc.httpcore.HttpClient
import com.rc.httpcore.HttpConfig
import com.rc.httpcore.client.ApiRepository
import com.rc.httpcore.exception.NetException
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
import retrofit2.HttpException
import java.net.ConnectException
import java.net.SocketTimeoutException

/**
 * 刷卡登录
 */
class SwipeActivity : BaseCountDownActivity<ActivitySwipeBinding>() {
    private var mUsbReceiver: UsbReceiver? = null  // 刷卡广播注册
    private var mHandleScanEvent = false //当前是否已经获取过 usb返回的参数


    override fun createViewBinding() = ActivitySwipeBinding.inflate(LayoutInflater.from(this))

    override fun initViews(savedInstanceState: Bundle?) {
        super.initViews(savedInstanceState)
        MediaPlayerHelper.playRawMp3(this, R.raw.login_shua_ka)
        //注册广播
        EventBus.getDefault().register(this)
        viewBinding.tvReturn.text = "返回${ChemicalApp.confs!!.backTime}s"
        viewBinding.tvReturn.setOnClickListener {
            UiManager.switcherCashier(this, MainActivity::class.java)
        }

        val stringExtra = intent.getStringExtra("mtypes")
        when (stringExtra) {
            "1" -> {
                viewBinding.linType.visibility = View.GONE
            }
            "6" -> {
                viewBinding.tvFace.visibility = View.GONE
            }
            "4" -> {
                viewBinding.tvWx.visibility = View.GONE
            }
        }

        viewBinding.deptName.text = "${ChemicalApp.confs!!.deptName}-${ChemicalApp.confs!!.roomNum}"
        Glide.with(this)
            .load("${HttpConfig.API_BASE_IMG_URL}${ChemicalApp.confs!!.circularLogo}")
            .apply(RequestOptions.diskCacheStrategyOf(DiskCacheStrategy.AUTOMATIC))
            .into(viewBinding.image)
        val map = mutableMapOf<String, String>()
        if (stringExtra != null) {
            map["mtypes"] = stringExtra
        }
        try {
            val face = intent.getStringExtra("faceList")
            if (face != null) {
                map["faceList"] = face
            }
        } catch (e: Exception) {

        }
        viewBinding.tvFace.setOnClickListener {
            UiManager.switcher(this, map, FacialLoginActivity::class.java)
            finish()
        }

        viewBinding.tvWx.setOnClickListener {
            if (stringExtra != null) {
                map["mtypes"] = stringExtra
            }
            UiManager.switcher(this, map, ScanLoginActivity::class.java)
            finish()
        }

    }


    override fun onResume() {
        super.onResume()
        mPortScanHelper.onResume()
        registerUsbBroadcast()
    }


    private val mPortScanHelper by lazy {
        PortScanHelper(this, object : OnSerialScanListener {
            override fun dispatchScanEvent(type: OnSerialScanListener.ScanType, content: String) {
                if (!mHandleScanEvent) {
                    if (content.isNotBlank()) {
                        mHandleScanEvent = true
                        handleScanEvent(content)
                    }

                }
            }

        })
    }

    //刷卡usb链接
    private fun registerUsbBroadcast() {
        if (null == mUsbReceiver) {
            val filter = IntentFilter().apply {
                addAction(UsbReceiver.ACTION_USB_PERMISSION)
                addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED)
                addAction(UsbManager.ACTION_USB_DEVICE_DETACHED)
                addAction(UsbReceiver.ACTION_USB_STATE) // usb连接状态广播
            }
            mUsbReceiver = UsbReceiver()
            registerReceiver(mUsbReceiver, filter)
        }
    }

    //调用刷卡信息
    private fun handleScanEvent(cont: String) {
        showLoading("登录中...")
        HttpClient.token = null
        val disposable = ApiRepository.cardNum(cont)
            .subscribe({ data ->
                ChemicalApp.userData = data
                authenticationInfo(data.userId, ChemicalApp.subjectId!!)
            }, { throwable ->
                dismissLoading()
                throwableView(throwable)
                mHandleScanEvent = false
            })
        addDisposable(disposable)
    }

    //验证当前人员身份
    private fun authenticationInfo(userId: String, subId: String) {
        showLoading("验证中...")
        val disposable = ApiRepository.userCardValidation(userId, subId)
            .subscribe({ data ->
                dismissLoading()
                val allFalse = with(data) {
                    cabinetAdmin == false &&
                            belongUser == false &&
                            toipcUser == false &&
                            safeUser == false &&
                            collegeAdmin == false &&
                            schoolLevelAdmin == false &&
                            adminUser == false
                }
                ChemicalApp.responsibles = false
                ChemicalApp.administrators = false
                if (allFalse) {
                    HttpClient.token = null
                    ChemicalApp.userData = null
                    customDialogView(2, "当前身份不符合")
                    mHandleScanEvent = false
                } else {
                    //校级管理员 schoolLevelAdmin
                    //院级管理员  collegeAdmin
                    //实验室负责人 adminUser
                    //安全负责人 safeUser
                    //柜锁管理员 cabinetAdmin
                    //是否化学品归属人 belongUser
                    //是否化学品归属课题组下成员  toipcUser
                    if (data.schoolLevelAdmin == true || data.collegeAdmin == true) {
                        //院级管理员 or 校级管理员
                        ChemicalApp.administrators = true
                        authenticationDialog(data.faceImg, data.userName)
                    } else if (data.adminUser == true || data.safeUser == true || data.cabinetAdmin == true) {
                        //实验室负责人 or  安全负责人 or 柜锁管理员
                        ChemicalApp.responsibles = true
                        authenticationDialog(data.faceImg, data.userName)
                    } else if (data.belongUser == true || data.toipcUser == true) {  //当前身份 归属人or课题组
                        ChemicalApp.responsibles = false
                        ChemicalApp.administrators = false
                        authenticationDialog(data.faceImg, data.userName)
                    } else {
                        HttpClient.token = null
                        ChemicalApp.userData = null
                        customDialogView(2, "当前身份不符合")
                        mHandleScanEvent = false
                    }

                }

            }, { throwable ->
                dismissLoading()
                throwableView(throwable)
                HttpClient.token = null
                ChemicalApp.userData = null
                mHandleScanEvent = false
            })
        addDisposable(disposable)
    }

    //获取刷卡信息
    override fun dispatchKeyEvent(event: KeyEvent?): Boolean {
        mPortScanHelper.dispatchKeyEvent(event)
        return super.dispatchKeyEvent(event)
    }

    //停止 销毁广播传递
    override fun onPause() {
        mPortScanHelper.onPause()
        super.onPause()
    }

    override fun onDestroy() {
        super.onDestroy()
        mPortScanHelper.onPause()
        // 移除回调，以防止内存泄漏
        try {
            handlerBack.removeCallbacks(countdownRunnable)
        } catch (e: Exception) {

        }
        unregisterReceiver(mUsbReceiver)
        // 停止定时更新
        EventBus.getDefault().unregister(this) //关闭广播
    }

    //必须写这个方法  防止注册失败
    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onUpdateEventEvent(event: KeyEvent) {

    }


    override fun onBackPressed() {
        super.onBackPressed()
        UiManager.switcherCashier(this, MainActivity::class.java)
    }

    private val handlerBack = Handler(Looper.getMainLooper())
    private var timeLeftInSeconds = 5
    private var mTvView: TextView? = null
    private var mDialogsAut: AuthenticationDialog? = null

    //身份认证成功
    private fun authenticationDialog(faceImg: String?, userName: String) {
        MediaPlayerHelper.playRawMp3(this, R.raw.login_ren_zheng_tong_hua)
        mDialogsAut = AuthenticationDialog(
            this,
            faceImg,
            ChemicalApp.confs!!.subName,
            ChemicalApp.confs!!.deptName,
            "${ChemicalApp.confs!!.buildName}${ChemicalApp.confs!!.floorName}",
            userName, object : AuthenticationDialog.IClickLit {
                override fun onUpView(tvView: TextView) {
                    mTvView = tvView
                }
            })
        mDialogsAut!!.show()
        // 开始倒计时
        handlerBack.post(countdownRunnable)
        // 获取对话框的 Window 对象
        mDialogsAut!!.window?.decorView?.setOnTouchListener { _, event ->
            // 判断是否点击了对话框外部空白区域
            if (event.action == MotionEvent.ACTION_DOWN) {
                val x = event.x
                val y = event.y
                val dialogView = mDialogsAut!!.window?.decorView
                if (dialogView != null && (x < 0 || x > dialogView.width || y < 0 || y > dialogView.height)) {
                    // 在此处执行点击对话框外部空白区域时的操作
                    // 例如关闭对话框
                    // 移除回调，以防止内存泄漏
                    mDialogsAut!!.dismiss()
                    finish()
                    return@setOnTouchListener true
                }
            }
            return@setOnTouchListener false
        }

    }

    private val countdownRunnable = object : Runnable {
        override fun run() {
            if (timeLeftInSeconds > 0) {
                mTvView!!.text = "${timeLeftInSeconds}秒后自动返回首页"
                timeLeftInSeconds--
                handlerBack.postDelayed(this, 1000)
            } else {
                mDialogsAut!!.dismiss()
                finish()
            }
        }
    }

    /**
     * 0  没有图标 1 绿色   2红色
     *   失败或者成功的弹框
     */
    private fun customDialogView(types: Int, msg: String) {
        if (!this.isFinishing && !this.isDestroyed) {
            val customDialog = CustomDialog(this, types, msg)
            customDialog.show()
        }
    }

    /**
     * 异常处理
     */
    private fun throwableView(throwable: Throwable) {
        when (throwable) {
            is NetException -> {
                if (throwable.message.isNullOrEmpty()) {
                    "接口请求失败(${throwable.code})"
                } else {
                    throwable.message!!
                }
            }
            is SocketTimeoutException -> "请求超时，请稍后重试"
            is ConnectException -> "无法连接服务器，请检查网络"
            is HttpException -> "服务器繁忙，请稍后重试"
            else -> null
        }?.let { customDialogView(2, "$it") }
    }

}