package com.gh.common.pop

import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.core.view.updateLayoutParams
import com.gh.common.dialog.OnReserveSuccessListener
import com.gh.gamecenter.R
import com.gh.gamecenter.common.databinding.LayoutPopupContainerBinding
import com.gh.gamecenter.common.view.BugFixedPopupWindow
import com.gh.gamecenter.core.utils.DisplayUtils
import com.gh.gamecenter.databinding.LayoutAddKaifuPopupItemBinding

class EditBindPhoneInfoPop(
    private val hasValidated: Boolean,
    private val binding: LayoutPopupContainerBinding,
    private val listener: OnReserveSuccessListener
) : BugFixedPopupWindow(binding.root, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT) {

    private val context
        get() = binding.root.context

    init {
        initView()
    }

    private fun initView() {
        isOutsideTouchable = false
        isFocusable = true
        binding.container.updateLayoutParams<ViewGroup.LayoutParams> {
            width = DisplayUtils.dip2px(128F)
        }
        val inflater = LayoutInflater.from(context)
        fun createItemView(textResId: Int) =
            LayoutAddKaifuPopupItemBinding.inflate(inflater, binding.container, false)
                .also {
                    it.hintText.setText(textResId)
                    binding.container.addView(it.root)
                }.root

        val tvChangePhoneNumber = createItemView(R.string.change_phone_number)
        if(!hasValidated){
            val tvVerifyPhoneNumber = createItemView(R.string.verify_phone_number)
            tvVerifyPhoneNumber.setOnClickListener {
                dismiss()
                listener.verifyPhoneNumber()
            }
        }

        val tvTurnOffSmsReminder = createItemView(R.string.turn_off_sms_reminders)

        tvChangePhoneNumber.setOnClickListener {
            dismiss()
            listener.changePhoneNumber()
        }

        tvTurnOffSmsReminder.setOnClickListener {
            dismiss()
            listener.updateSmsReminder(false)
        }
    }

    companion object {

        fun create(context: Context, hasValidated: Boolean, listener: OnReserveSuccessListener): EditBindPhoneInfoPop {
            val binding = LayoutPopupContainerBinding.inflate(LayoutInflater.from(context))
            return EditBindPhoneInfoPop(hasValidated, binding, listener)
        }
    }
}