package mtp.polymer.com.autowidget.dialog

import android.app.Dialog
import android.graphics.Color
import android.os.Bundle

import android.text.method.HideReturnsTransformationMethod
import android.text.method.PasswordTransformationMethod
import android.view.*
import android.view.WindowManager.LayoutParams.*
import android.view.inputmethod.EditorInfo
import android.widget.EditText
import androidx.appcompat.app.AppCompatActivity
import com.blankj.utilcode.util.KeyboardUtils
import com.google.android.material.bottomsheet.BottomSheetDialogFragment

import mtp.polymer.com.autowidget.*
import org.jetbrains.anko.*



class BottomInputDialogFragment : BottomSheetDialogFragment() {

    companion object {
        fun createInstance(appCompatActivity: AppCompatActivity,
                           titleStr: String,
                           hintStr: String,
                           inputType: Int = EditorInfo.TYPE_TEXT_VARIATION_PASSWORD,
                           actionStr: String = "下一步"): BottomInputDialogFragment {
            return BottomInputDialogFragment().apply {
                this.appCompatActivity = appCompatActivity
                arguments = Bundle().apply {
                    putString("title", titleStr)
                    putString("hint", hintStr)
                    putString("actionStr", actionStr)
                    putInt("inputType", inputType)
                }
            }
        }
    }

    var onInputBlock: ((CharSequence) -> Unit)? = null

    private var appCompatActivity: AppCompatActivity? = null
    private val titleStr: String by lazy { arguments?.getString("title") ?: "" }
    private val hintStr: String by lazy { arguments?.getString("hint") ?: "" }
    private val actionStr: String by lazy { arguments?.getString("actionStr") ?: "" }
    private val inputType: Int by lazy { arguments?.getInt("inputType", EditorInfo.TYPE_TEXT_VARIATION_PASSWORD) ?: EditorInfo.TYPE_TEXT_VARIATION_PASSWORD }

    private lateinit var etInput: EditText

    fun show() {
        (appCompatActivity ?: activity)?.let { activity ->
            show(activity.supportFragmentManager, javaClass.name)
        }
    }

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        return super.onCreateDialog(savedInstanceState).apply {
            window?.setSoftInputMode(SOFT_INPUT_ADJUST_RESIZE or SOFT_INPUT_STATE_VISIBLE)
            setOnShowListener { KeyboardUtils.showSoftInput(etInput) }
            setOnDismissListener { KeyboardUtils.hideSoftInput(etInput) }
        }
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        val isPasswordType = this@BottomInputDialogFragment.inputType == EditorInfo.TYPE_TEXT_VARIATION_PASSWORD ||
                this@BottomInputDialogFragment.inputType == EditorInfo.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD ||
                this@BottomInputDialogFragment.inputType == EditorInfo.TYPE_NUMBER_VARIATION_PASSWORD ||
                this@BottomInputDialogFragment.inputType == EditorInfo.TYPE_TEXT_VARIATION_WEB_PASSWORD
        return _FrameLayout((appCompatActivity ?: activity)!!).apply {
            backgroundColor = Color.parseColor("#66000000")
//            val commonPadding = autoSize(32)
//            verticalLayout {
//                backgroundColor = Color.WHITE
//                relativeLayout {
//                    imageView {
//                        padding = autoSize(25)
//                        setImageDrawable(context.getTintDrawable(R.drawable.ic_close, "#159dfd"))
//                        onThrottleFirstClick { dialog.dismiss() }
//                    }.lparams(autoSize(92), matchParent) {
//                        marginStart = autoSize(10)
//                        alignParentStart()
//                        centerVertically()
//                    }
//                    textView {
//                        gravity = Gravity.CENTER
//                        textSizeAuto = 40
//                        textColorStr = "#333333"
//                        text = titleStr
//                    }.lparams(wrapContent, matchParent) {
//                        centerInParent()
//                    }
//                    textView {
//                        gravity = Gravity.CENTER
//                        text = actionStr
//                        setPadding(autoSize(30), 0, autoSize(30), 0)
//                        textSizeAuto = 36
//                        textColorStr = "#159dfd"
//                        onThrottleFirstClick {
//                            dialog.dismiss()
//                            onInputBlock?.invoke(etInput.text.toString())
//                        }
//                    }.lparams(wrapContent, matchParent) {
//                        alignParentEnd()
//                        centerVertically()
//                    }
//                }.lparams(matchParent, autoSize(110))
//                divider("#e5e5e5", paddingStart = 0)
//                space().lparams(0, 0, 1f)
//                linearLayout {
//                    setPadding(commonPadding, 0, 0, 0)
//                    backgroundDrawable = createRoundRectStokeDrawable("#e5e5e5", 0, 1)
//                    gravity = Gravity.CENTER_VERTICAL
//                    etInput = editText {
//                        gravity = Gravity.CENTER_VERTICAL
//                        inputType = this@BottomInputDialogFragment.inputType
//                        if (isPasswordType) {
//                            transformationMethod = PasswordTransformationMethod.getInstance()
//                        }
//                        hintColorStr = "#cccccc"
//                        textColorStr = "#333333"
//                        hint = hintStr
//                        backgroundDrawable = null
//                        textSizeAuto = 36
//                    }.lparams(0, matchParent, 1f)
//                    if (isPasswordType) {
//                        imageView {
//                            val normal = ContextCompat.getDrawable(context, R.drawable.ic_eye)!!
//                            val selected = context.getTintDrawable(normal, "#159DFD")
//                            setImageDrawable(createSelectStateDrawable(normal = normal, selected = selected))
//                            setPadding(autoSize(29), 0, autoSize(29), 0)
//                            onThrottleFirstClick {
//                                val selection = etInput.selectionStart
//                                etInput.transformationMethod = if (isSelected) PasswordTransformationMethod.getInstance() else HideReturnsTransformationMethod.getInstance()
//                                etInput.setSelection(selection)
//                                isSelected = isSelected.not()
//                            }
//                        }.lparams(autoSize(100), autoSize(100))
//                    }
//                }.lparams(matchParent, autoSize(112)) {
//                    marginStart = commonPadding
//                    marginEnd = commonPadding
//                }
//                space().lparams(0, 0, 1f)
//            }.lparams(matchParent, autoSize(405)) {
//                gravity = Gravity.BOTTOM
//            }
        }
    }

}