package kr.co.vividnext.sodalive.settings.notice

import android.annotation.SuppressLint
import androidx.core.content.IntentCompat
import androidx.webkit.WebSettingsCompat
import androidx.webkit.WebViewFeature
import kr.co.vividnext.sodalive.base.BaseActivity
import kr.co.vividnext.sodalive.common.Constants
import kr.co.vividnext.sodalive.databinding.ActivityNoticeDetailBinding

class NoticeDetailActivity : BaseActivity<ActivityNoticeDetailBinding>(
    ActivityNoticeDetailBinding::inflate
) {
    private lateinit var notice: NoticeItem

    @SuppressLint("SetJavaScriptEnabled")
    override fun setupView() {
        if (WebViewFeature.isFeatureSupported(WebViewFeature.FORCE_DARK)) {
            WebSettingsCompat.setForceDark(
                binding.webView.settings,
                WebSettingsCompat.FORCE_DARK_ON
            )
        }

        notice = IntentCompat.getParcelableExtra(
            intent,
            Constants.EXTRA_NOTICE,
            NoticeItem::class.java
        )!!

        binding.toolbar.tvBack.text = "공지사항 상세"
        binding.toolbar.tvBack.setOnClickListener { finish() }

        binding.tvTitle.text = notice.title
        binding.tvDate.text = notice.date

        val viewPort = "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">"
        val data = viewPort + notice.content
        binding.webView.settings.apply {
            javaScriptEnabled = true // 자바스크립트 실행 허용
            javaScriptCanOpenWindowsAutomatically = false // 자바스크립트에서 새창 실 행 허용
            setSupportMultipleWindows(false) // 새 창 실행 허용
            loadWithOverviewMode = true // 메타 태그 허용

            useWideViewPort = true // 화면 사이즈 맞추기 허용
            setSupportZoom(false) // 화면 줌 허용
            builtInZoomControls = false // 화면 확대 축소 허용 여부
        }
        binding.webView.loadData(
            data,
            "text/html; charset=utf-8",
            "utf-8"
        )
    }
}
