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

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.widget.Toast
import androidx.activity.OnBackPressedCallback
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.LoadingDialog
import kr.co.vividnext.sodalive.databinding.ActivityCanStatusBinding
import kr.co.vividnext.sodalive.extensions.moneyFormat
import kr.co.vividnext.sodalive.main.MainActivity
import kr.co.vividnext.sodalive.mypage.can.charge.CanChargeActivity
import kr.co.vividnext.sodalive.mypage.can.status.charge.CanChargeStatusFragment
import kr.co.vividnext.sodalive.mypage.can.status.use.CanUseStatusFragment
import org.koin.android.ext.android.inject

class CanStatusActivity : BaseActivity<ActivityCanStatusBinding>(
    ActivityCanStatusBinding::inflate
) {

    private val viewModel: CanStatusViewModel by inject()
    private val onBackPressedCallback = object : OnBackPressedCallback(true) {
        override fun handleOnBackPressed() {
            onClickBackButton()
        }
    }

    private lateinit var loadingDialog: LoadingDialog

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        onBackPressedDispatcher.addCallback(this, onBackPressedCallback)

        bindData()
        viewModel.getCanStatus()
    }

    override fun setupView() {
        binding.toolbar.tvBack.text = "캔내역"
        binding.toolbar.tvBack.setOnClickListener { onClickBackButton() }
        binding.flChargeCan.setOnClickListener {
            startActivity(
                Intent(
                    applicationContext,
                    CanChargeActivity::class.java
                )
            )
        }
        loadingDialog = LoadingDialog(this, layoutInflater)

        val tabs = binding.tabs
        tabs.addTab(tabs.newTab().setText("충전내역").setTag("charge_status"))
        tabs.addTab(tabs.newTab().setText("사용내역").setTag("use_status"))

        changeFragment("charge_status")

        tabs.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
            override fun onTabSelected(tab: TabLayout.Tab) {
                val tag = tab.tag as String
                changeFragment(tag)
            }

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

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

    private fun changeFragment(tag: String) {
        val fragmentManager = supportFragmentManager
        val fragmentTransaction = fragmentManager.beginTransaction()

        val currentFragment = fragmentManager.primaryNavigationFragment
        if (currentFragment != null) {
            fragmentTransaction.hide(currentFragment)
        }

        var fragment = fragmentManager.findFragmentByTag(tag)
        if (fragment == null) {
            fragment = if (tag == "charge_status") {
                CanChargeStatusFragment(viewModel)
            } else {
                CanUseStatusFragment(viewModel)
            }

            fragmentTransaction.add(R.id.container, fragment, tag)
        } else {
            fragmentTransaction.show(fragment)
        }

        fragmentTransaction.setPrimaryNavigationFragment(fragment)
        fragmentTransaction.setReorderingAllowed(true)
        fragmentTransaction.commitNow()
    }

    @SuppressLint("SetTextI18n")
    private fun bindData() {
        viewModel.totalCanLiveData.observe(this) {
            binding.tvTotalCan.text = it.moneyFormat()
        }

        viewModel.paidCanLiveData.observe(this) {
            binding.tvPaidCan.text = it.moneyFormat()
        }

        viewModel.rewardCanLiveData.observe(this) {
            binding.tvRewardCan.text = it.moneyFormat()
        }

        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()
            }
        }
    }

    private fun onClickBackButton() {
        val intent = Intent(applicationContext, MainActivity::class.java)
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP)
        startActivity(intent)
        finish()
    }
}
