package com.miya.fastcashier.ui.dialog

import android.app.Dialog
import android.content.Context
import android.view.Gravity
import android.view.WindowManager
import android.widget.ImageButton
import android.widget.ImageView
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import com.miya.fastcashier.R
import com.miya.fastcashier.utils.clickWithTrigger

class PayChooseDialog(context: Context) : Dialog(context){

    private lateinit var tvGeneratePayCode: TextView
    private lateinit var cWxPay: ConstraintLayout
    private lateinit var cAliPay: ConstraintLayout
    private lateinit var ivClose: ImageView
    private lateinit var ivWxPayChoose: ImageButton
    private lateinit var ibAliPayChoose: ImageButton

    private lateinit var generatePayCodeClickListener: GeneratePayCodeClickListener

    private var payType: String = "2"

    init {
       initWindow()
       initConfig()
       initView()
   }

    private fun initConfig() {
        setCancelable(true)
    }

    private fun initView() {
       setContentView(R.layout.dialog_pay_choose)
       ivClose = findViewById(R.id.ivClose)
       cWxPay = findViewById(R.id.cWxPay)
       cAliPay = findViewById(R.id.cAliPay)
        ivWxPayChoose = findViewById(R.id.ivWxPayChoose)
        ivWxPayChoose.isSelected = true
        ibAliPayChoose = findViewById(R.id.ibAliPayChoose)
        ibAliPayChoose.isSelected = false
        tvGeneratePayCode = findViewById(R.id.tvGeneratePayCode)

        ivClose.clickWithTrigger{
          dismiss()
        }

//        2是微信 1是支付宝
        cWxPay.setOnClickListener{
            payType = "2"
            ivWxPayChoose.isSelected = true
            ibAliPayChoose.isSelected = false
        }
        ivWxPayChoose.setOnClickListener{
            payType = "2"
            ivWxPayChoose.isSelected = true
            ibAliPayChoose.isSelected = false
        }

        cAliPay.setOnClickListener{
            payType = "1"
            ivWxPayChoose.isSelected = false
            ibAliPayChoose.isSelected = true
        }
        ibAliPayChoose.setOnClickListener{
            payType = "1"
            ivWxPayChoose.isSelected = false
            ibAliPayChoose.isSelected = true
        }

        tvGeneratePayCode.setOnClickListener {
            generatePayCodeClickListener.choose(payType)
        }
    }

    /**
     * 生成付款码
     */
    fun setGeneratePayCodeClickListener(generatePayCodeClickListener: GeneratePayCodeClickListener){
        this.generatePayCodeClickListener = generatePayCodeClickListener
    }

    interface GeneratePayCodeClickListener {
        fun choose(payType:String);
    }


    private fun initWindow() {
        val win = this.window
        win!!.decorView.setPadding(0, 0, 0, 0)
        val lp = win.attributes
        lp.width = WindowManager.LayoutParams.MATCH_PARENT
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT
        lp.gravity = Gravity.BOTTOM
        lp.windowAnimations = R.style.BottomInAndOutStyle
        win.attributes = lp
        win.setBackgroundDrawableResource(R.color.transparent)
    }


}