package kr.co.vividnext.sodalive.settings.notification

import android.os.Bundle
import android.widget.Toast
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.ActivityNotificationSettingsBinding
import org.koin.android.ext.android.inject

class NotificationSettingsActivity : BaseActivity<ActivityNotificationSettingsBinding>(
    ActivityNotificationSettingsBinding::inflate
) {

    private val viewModel: NotificationSettingsViewModel by inject()

    private lateinit var loadingDialog: LoadingDialog

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

        viewModel.getNotificationSettings()
    }

    override fun setupView() {
        binding.toolbar.tvBack.text = "알림 설정"
        binding.toolbar.tvBack.setOnClickListener { finish() }

        loadingDialog = LoadingDialog(this, layoutInflater)

        binding.ivNotifiedLive.setOnClickListener {
            viewModel.toggleFollowingChannelLiveNotice()
        }
        binding.ivNotifiedUploadContent.setOnClickListener {
            viewModel.toggleFollowingChannelUploadContentNotice()
        }
        binding.ivMessage.setOnClickListener { viewModel.toggleMessage() }

        viewModel.followingChannelLiveNotice.observe(this) {
            binding.ivNotifiedLive.setImageResource(
                if (it) {
                    R.drawable.btn_toggle_on_big
                } else {
                    R.drawable.btn_toggle_off_big
                }
            )
        }

        viewModel.followingChannelUploadContentNotice.observe(this) {
            binding.ivNotifiedUploadContent.setImageResource(
                if (it) {
                    R.drawable.btn_toggle_on_big
                } else {
                    R.drawable.btn_toggle_off_big
                }
            )
        }

        viewModel.isMessage.observe(this) {
            binding.ivMessage.setImageResource(
                if (it) {
                    R.drawable.btn_toggle_on_big
                } else {
                    R.drawable.btn_toggle_off_big
                }
            )
        }

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

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