package kr.co.vividnext.sodalive.mypage.can.coupon

import android.content.Intent
import android.os.Bundle
import android.text.InputFilter
import android.widget.Toast
import kr.co.vividnext.sodalive.base.BaseActivity
import kr.co.vividnext.sodalive.common.LoadingDialog
import kr.co.vividnext.sodalive.databinding.ActivityCanCouponBinding
import kr.co.vividnext.sodalive.mypage.can.status.CanStatusActivity
import org.koin.android.ext.android.inject


class CanCouponActivity : BaseActivity<ActivityCanCouponBinding>(
    ActivityCanCouponBinding::inflate
) {

    private val viewModel: CanCouponViewModel by inject()

    private lateinit var loadingDialog: LoadingDialog

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        bindData()
    }

    override fun setupView() {
        loadingDialog = LoadingDialog(this, layoutInflater)

        binding.toolbar.tvBack.text = "쿠폰등록"
        binding.toolbar.tvBack.setOnClickListener { finish() }
        binding.etCouponNumber.filters = arrayOf(InputFilter.AllCaps())

        binding.tvRegistration.setOnClickListener {
            val couponNumber = binding.etCouponNumber.text.toString()
            viewModel.useCoupon(couponNumber) {
                finish()
                startActivity(Intent(applicationContext, CanStatusActivity::class.java))
            }
        }
    }

    private fun bindData() {
        viewModel.toastLiveData.observe(this) {
            it?.let { Toast.makeText(applicationContext, it, Toast.LENGTH_LONG).show() }
        }

        viewModel.isLoading.observe(this) {
            if (it) {
                loadingDialog.show(screenWidth)
            } else {
                loadingDialog.dismiss()
            }
        }
    }
}
