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

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import com.google.android.material.tabs.TabLayout
import kr.co.vividnext.sodalive.R
import kr.co.vividnext.sodalive.base.BaseActivity
import kr.co.vividnext.sodalive.common.Constants
import kr.co.vividnext.sodalive.databinding.ActivityCanChargeBinding
import kr.co.vividnext.sodalive.mypage.can.charge.iap.CanChargeIapFragment
import kr.co.vividnext.sodalive.mypage.can.charge.pg.CanChargePgFragment
import kr.co.vividnext.sodalive.mypage.can.charge.pg.CanResponse
import kr.co.vividnext.sodalive.mypage.can.payment.CanPaymentActivity
import kr.co.vividnext.sodalive.mypage.can.status.CanStatusActivity

class CanChargeActivity : BaseActivity<ActivityCanChargeBinding>(
    ActivityCanChargeBinding::inflate
) {
    private var gotoPrevPage: Boolean = false

    private lateinit var activityResultLauncher: ActivityResultLauncher<Intent>

    override fun onCreate(savedInstanceState: Bundle?) {
        activityResultLauncher = registerForActivityResult(
            ActivityResultContracts.StartActivityForResult()
        ) {
            if (it.resultCode == Activity.RESULT_OK) {
                finish()
            }
        }

        super.onCreate(savedInstanceState)

        gotoPrevPage = intent.getBooleanExtra(
            Constants.EXTRA_GO_TO_PREV_PAGE,
            false
        )

        supportFragmentManager.beginTransaction()
            .replace(
                R.id.fl_container,
                CanChargePgFragment()
            ).commit()
    }

    override fun setupView() {
        setupToolbar()
        setupTabs()
    }

    private fun setupToolbar() {
        binding.toolbar.tvBack.text = "충전하기"
        binding.toolbar.tvBack.setOnClickListener { finish() }
    }

    private fun setupTabs() {
        val tabs = binding.tabs

        tabs.visibility = View.VISIBLE
        tabs.addTab(tabs.newTab().setText("PG"))
        tabs.addTab(tabs.newTab().setText("인 앱 결제"))

        tabs.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
            override fun onTabSelected(tab: TabLayout.Tab) {
                when (tab.position) {
                    0 -> supportFragmentManager.beginTransaction()
                        .replace(R.id.fl_container, CanChargePgFragment()).commit()

                    1 -> supportFragmentManager.beginTransaction()
                        .replace(R.id.fl_container, CanChargeIapFragment()).commit()
                }
            }

            override fun onTabUnselected(tab: TabLayout.Tab) {
            }

            override fun onTabReselected(tab: TabLayout.Tab) {
            }
        })
    }

    fun selectCan(model: CanResponse) {
        val intent = Intent(applicationContext, CanPaymentActivity::class.java)
        intent.putExtra(Constants.EXTRA_CAN, model)
        intent.putExtra(Constants.EXTRA_GO_TO_PREV_PAGE, gotoPrevPage)
        activityResultLauncher.launch(intent)
    }

    fun successIapCharge() {
        if (gotoPrevPage) {
            setResult(RESULT_OK)
        } else {
            val intent = Intent(applicationContext, CanStatusActivity::class.java)
            startActivity(intent)
        }

        finish()
    }
}
