package kr.co.vividnext.sodalive.settings

import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.activity.OnBackPressedCallback
import kr.co.vividnext.sodalive.R
import kr.co.vividnext.sodalive.base.BaseActivity
import kr.co.vividnext.sodalive.common.SharedPreferenceManager
import kr.co.vividnext.sodalive.databinding.ActivityContentSettingsBinding
import kr.co.vividnext.sodalive.splash.SplashActivity
import org.koin.android.ext.android.inject

class ContentSettingsActivity : BaseActivity<ActivityContentSettingsBinding>(
    ActivityContentSettingsBinding::inflate
) {

    private val viewModel: ContentSettingsViewModel by inject()

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

        onBackPressedDispatcher.addCallback(this, object : OnBackPressedCallback(true) {
            override fun handleOnBackPressed() {
                handleFinish()
            }
        })
    }

    override fun setupView() {
        binding.toolbar.tvBack.text = "콘텐츠 보기 설정"
        binding.toolbar.tvBack.setOnClickListener { handleFinish() }

        // 본인 인증 체크
        if (SharedPreferenceManager.isAuth) {
            binding.llAdultContentVisible.visibility = View.VISIBLE

            // 19금 콘텐츠 보기 체크
            if (SharedPreferenceManager.isAdultContentVisible) {
                binding.llAdultContentPreference.visibility = View.VISIBLE

            } else {
                binding.llAdultContentPreference.visibility = View.GONE
            }

            // 19금 콘텐츠 보기 스위치 액션
            binding.ivAdultContentVisible.setOnClickListener {
                viewModel.toggleAdultContentVisible()
            }

            binding.tvContentAll.setOnClickListener {
                viewModel.setAdultContentPreference(ContentType.ALL)
            }

            binding.tvContentMale.setOnClickListener {
                viewModel.setAdultContentPreference(ContentType.MALE)
            }

            binding.tvContentFemale.setOnClickListener {
                viewModel.setAdultContentPreference(ContentType.FEMALE)
            }
        } else {
            binding.llAdultContentVisible.visibility = View.GONE
        }
    }

    private fun bindData() {
        viewModel.isAdultContentVisible.observe(this) {
            if (it) {
                binding.ivAdultContentVisible.setImageResource(R.drawable.btn_toggle_on_big)
                binding.llAdultContentPreference.visibility = View.VISIBLE
            } else {
                binding.ivAdultContentVisible.setImageResource(R.drawable.btn_toggle_off_big)
                binding.llAdultContentPreference.visibility = View.GONE
            }
        }

        viewModel.adultContentPreference.observe(this) {
            binding.tvContentAll.isSelected = false
            binding.tvContentMale.isSelected = false
            binding.tvContentFemale.isSelected = false

            when (it) {
                ContentType.ALL -> binding.tvContentAll.isSelected = true
                ContentType.MALE -> binding.tvContentMale.isSelected = true
                ContentType.FEMALE -> binding.tvContentFemale.isSelected = true
                else -> {}
            }
        }
    }

    private fun handleFinish() {
        if (viewModel.isChangedAdultContentVisible) {
            startActivity(
                Intent(
                    this@ContentSettingsActivity,
                    SplashActivity::class.java
                ).apply {
                    addFlags(
                        Intent.FLAG_ACTIVITY_CLEAR_TASK or
                            Intent.FLAG_ACTIVITY_NEW_TASK
                    )
                }
            )
            finish()
        } else {
            finish()
        }
    }
}
