package kr.co.vividnext.sodalive.settings

import android.annotation.SuppressLint
import android.content.Intent
import android.graphics.Paint
import android.os.Bundle
import android.view.View
import android.widget.Toast
import androidx.activity.viewModels
import com.google.android.gms.oss.licenses.OssLicensesMenuActivity
import kr.co.vividnext.sodalive.BuildConfig
import kr.co.vividnext.sodalive.audio_content.AudioContentPlayService
import kr.co.vividnext.sodalive.base.BaseActivity
import kr.co.vividnext.sodalive.base.SodaDialog
import kr.co.vividnext.sodalive.common.Constants
import kr.co.vividnext.sodalive.common.LoadingDialog
import kr.co.vividnext.sodalive.common.SharedPreferenceManager
import kr.co.vividnext.sodalive.databinding.ActivitySettingsBinding
import kr.co.vividnext.sodalive.mypage.alarm.AlarmViewModel
import kr.co.vividnext.sodalive.settings.event.EventActivity
import kr.co.vividnext.sodalive.settings.notice.NoticeActivity
import kr.co.vividnext.sodalive.settings.notification.NotificationSettingsActivity
import kr.co.vividnext.sodalive.settings.signout.SignOutActivity
import kr.co.vividnext.sodalive.settings.terms.TermsActivity
import kr.co.vividnext.sodalive.splash.SplashActivity
import org.koin.android.ext.android.inject

class SettingsActivity : BaseActivity<ActivitySettingsBinding>(ActivitySettingsBinding::inflate) {
    private val logoutDialog: SodaDialog by lazy {
        SodaDialog(
            activity = this,
            layoutInflater = layoutInflater,
            title = "알림",
            desc = "로그아웃 하시겠어요?",
            confirmButtonTitle = "확인",
            confirmButtonClick = { logout() },
            cancelButtonTitle = "취소",
            cancelButtonClick = {}
        )
    }

    private val logoutAllDialog: SodaDialog by lazy {
        SodaDialog(
            activity = this,
            layoutInflater = layoutInflater,
            title = "알림",
            desc = "모든 기기에서 로그아웃 하시겠어요?",
            confirmButtonTitle = "확인",
            confirmButtonClick = { logoutAllDevice() },
            cancelButtonTitle = "취소",
            cancelButtonClick = {}
        )
    }

    private val viewModel: SettingsViewModel by inject()
    private val alarmViewModel: AlarmViewModel by viewModels()

    private lateinit var loadingDialog: LoadingDialog

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

    @SuppressLint("SetTextI18n")
    override fun setupView() {
        binding.toolbar.tvBack.text = "설정"
        binding.toolbar.tvBack.setOnClickListener { finish() }

        loadingDialog = LoadingDialog(this, layoutInflater)

        binding.rlNotice.setOnClickListener {
            startActivity(
                Intent(
                    applicationContext,
                    NoticeActivity::class.java
                )
            )
        }

        binding.rlEvent.setOnClickListener {
            startActivity(
                Intent(
                    applicationContext,
                    EventActivity::class.java
                )
            )
        }

        binding.rlNotificationSettings.setOnClickListener {
            startActivity(
                Intent(
                    applicationContext,
                    NotificationSettingsActivity::class.java
                )
            )
        }

        if (SharedPreferenceManager.isAuth) {
            binding.rlContentSettings.visibility = View.VISIBLE
            binding.rlContentSettings.setOnClickListener {
                startActivity(
                    Intent(
                        applicationContext,
                        ContentSettingsActivity::class.java
                    )
                )
            }
        } else {
            binding.rlContentSettings.visibility = View.GONE
        }

        binding.rlTerms.setOnClickListener {
            val intent = Intent(applicationContext, TermsActivity::class.java)
            intent.putExtra(Constants.EXTRA_TERMS, Constants.EXTRA_TERMS)
            startActivity(intent)
        }

        binding.rlPrivacyPolicy.setOnClickListener {
            val intent = Intent(applicationContext, TermsActivity::class.java)
            intent.putExtra(Constants.EXTRA_TERMS, Constants.EXTRA_PRIVACY)
            startActivity(intent)
        }

        binding.rlOssLicense.setOnClickListener {
            startActivity(Intent(applicationContext, OssLicensesMenuActivity::class.java))
            OssLicensesMenuActivity.setActivityTitle("오픈소스 라이선스")
        }

        binding.tvVersion.text = "ver ${BuildConfig.VERSION_NAME}"

        binding.tvLogOut.setOnClickListener {
            logoutDialog.show(screenWidth)
        }

        binding.tvLogOutAll.setOnClickListener {
            logoutAllDialog.show(screenWidth)
        }

        binding.tvSignOut.paintFlags = binding.tvSignOut.paintFlags.or(Paint.UNDERLINE_TEXT_FLAG)
        binding.tvSignOut.setOnClickListener {
            startActivity(Intent(applicationContext, SignOutActivity::class.java))
        }
    }

    private fun bindData() {
        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() }
        }
    }

    private fun logout() {
        startService(
            Intent(applicationContext, AudioContentPlayService::class.java).apply {
                action = AudioContentPlayService.MusicAction.STOP.name
            }
        )

        viewModel.logout {
            SharedPreferenceManager.clear()
            alarmViewModel.truncate()
            finishAffinity()
            startActivity(Intent(applicationContext, SplashActivity::class.java))
        }
    }

    private fun logoutAllDevice() {
        startService(
            Intent(applicationContext, AudioContentPlayService::class.java).apply {
                action = AudioContentPlayService.MusicAction.STOP.name
            }
        )

        viewModel.logoutAllDevice() {
            SharedPreferenceManager.clear()
            finishAffinity()
            startActivity(Intent(applicationContext, SplashActivity::class.java))
        }
    }
}
