package com.base.pdfviewerscannerwhite.ui.permission

import android.annotation.SuppressLint
import android.graphics.Color
import android.text.SpannableString
import android.text.SpannableStringBuilder
import android.text.style.ForegroundColorSpan
import androidx.activity.addCallback
import androidx.core.content.ContextCompat
import androidx.core.view.updatePadding
import com.base.pdfviewerscannerwhite.R
import com.base.pdfviewerscannerwhite.databinding.ActivityPermissionBinding
import com.base.pdfviewerscannerwhite.helper.BaseActivity
import com.base.pdfviewerscannerwhite.utils.BarUtils
import com.base.pdfviewerscannerwhite.utils.PermissionUtils.requestStoragePermission

class PermissionActivity : BaseActivity<ActivityPermissionBinding>() {

    override val binding: ActivityPermissionBinding by lazy {
        ActivityPermissionBinding.inflate(layoutInflater)
    }

    @SuppressLint("SetTextI18n")
    override fun initView() {

        BarUtils.setStatusBarLightMode(this, true)
        BarUtils.setStatusBarColor(this, Color.parseColor("#F4F5FA"))
        binding.root.updatePadding(top = BarUtils.getStatusBarHeight())

        val tipString = getString(R.string.permission_tip_1)
        val redString = getString(R.string.permission_tip_2)

        val spannableString = SpannableString(redString)
        spannableString.setSpan(
            ForegroundColorSpan(ContextCompat.getColor(this, R.color.color_f92b39)),
            0,
            redString.length,
            SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE
        )
        val spannableStringBuilder = SpannableStringBuilder(tipString)
        spannableStringBuilder.append(spannableString)
        binding.tvTip.text = spannableStringBuilder

    }

    override fun initListener() {
        super.initListener()
        onBackPressedDispatcher.addCallback {
            finishToMain()
        }
        binding.tvLater.setOnClickListener {
            onBackPressedDispatcher.onBackPressed()
        }
        binding.tvAllow.setOnClickListener {
            requestStoragePermission(launcher) {
                onBackPressedDispatcher.onBackPressed()
            }
        }
    }

}