package com.miya.fastcashier.log

import android.app.Dialog
import android.content.Context
import com.miya.fastcashier.R
import android.widget.TextView
import android.graphics.Bitmap
import com.miya.fastcashier.log.CustomImageCenterDialog
import android.os.Build
import android.view.WindowManager
import android.text.TextUtils
import com.miya.print.utils.QRCodeUtil
import com.blankj.utilcode.util.SizeUtils
import android.view.Gravity
import android.view.View
import android.view.Window
import android.widget.ImageView

class CustomImageCenterDialog(context: Context) : Dialog(context, R.style.CommonDialog) {
    private var tvOk: TextView? = null
    private var tvHint: TextView? = null
    private var ivContent: ImageView? = null
    private var isFullScreen = false
    private var mImageContent: String? = null
    private var mHintContentText: String? = null
    private var mConfirmContent: String? = null
    private var mQrCodeBitmap: Bitmap? = null

    init {
        initWindows()
        setCanceledOnTouchOutside(false)
        setContentView(R.layout.dialog_center_image)
        setCancelable(true)
        initView()
    }

    protected fun initWindows() {
        val win = this.window
        win!!.requestFeature(Window.FEATURE_NO_TITLE)
        win.decorView.setPadding(0, 0, 0, 0)
        val lp = win.attributes
        lp.width = WindowManager.LayoutParams.MATCH_PARENT
        lp.height = WindowManager.LayoutParams.MATCH_PARENT
        //        lp.windowAnimations = R.style.LightInAndOutStyle;
        lp.gravity = Gravity.CENTER
        win.attributes = lp
        win.setBackgroundDrawableResource(android.R.color.transparent)
    }

    private fun initView() {
        tvOk = findViewById(R.id.tvOk)
        tvHint = findViewById(R.id.tvSubContent)
        ivContent = findViewById(R.id.ivContent)
    }

    override fun onWindowFocusChanged(hasFocus: Boolean) {
        super.onWindowFocusChanged(hasFocus)
        if (isFullScreen) {
            setFullScreen()
        }
    }

    /**
     * 隐藏虚拟按键，并且全屏
     */
    protected fun setFullScreen() {
        if (Build.VERSION.SDK_INT > 11 && Build.VERSION.SDK_INT < 19) { // lower api
            val v = this.window!!.decorView
            v.systemUiVisibility = View.GONE
        } else if (Build.VERSION.SDK_INT >= 19) {
            //for new api versions.
            val decorView = window!!.decorView
            val uiOptions =
                (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                        //                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION//暂时不需要hideNavigation
                        //                    | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION // hide nav bar
                        or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                        or View.SYSTEM_UI_FLAG_FULLSCREEN // hide status bar
                        or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY)
            decorView.systemUiVisibility = uiOptions
            window!!.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION)
        }
    }

    /**
     * 设置是否全屏显示
     *
     * @param isFullScreen
     * @return
     */
    fun setFullScreenStatus(isFullScreen: Boolean): CustomImageCenterDialog {
        this.isFullScreen = isFullScreen
        return this
    }

    fun setQrContent(msg: String?): CustomImageCenterDialog {
        mImageContent = msg
        return this
    }

    fun setConfirmContent(msg: String?): CustomImageCenterDialog {
        mConfirmContent = msg
        return this
    }

    fun setHintContent(msg: String?): CustomImageCenterDialog {
        mHintContentText = msg
        return this
    }

    /**
     * 创建dialog
     *
     * @return
     */
    fun build(): CustomImageCenterDialog {
        if (!TextUtils.isEmpty(mConfirmContent)) {
            tvOk!!.text = mConfirmContent
        }
        if (!TextUtils.isEmpty(mHintContentText)) {
            tvHint!!.text = mHintContentText
        }
        if (!TextUtils.isEmpty(mImageContent)) {
            mQrCodeBitmap = QRCodeUtil.createQRCodeBitmap(mImageContent, SizeUtils.dp2px(300f))
            ivContent!!.setImageBitmap(mQrCodeBitmap)
        }
        tvOk!!.setOnClickListener {
            if (mQrCodeBitmap != null) {
                mQrCodeBitmap!!.recycle()
            }
            dismiss()
        }
        if (isFullScreen) {
            setFullScreen()
        }
        return this
    }
}