package com.base.datarecovery.activity.privacyspace

import android.annotation.SuppressLint
import android.content.Intent
import android.graphics.Color
import android.view.View
import androidx.activity.addCallback
import androidx.fragment.app.Fragment
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import com.base.datarecovery.ads.AdmobInterstitialUtils
import com.base.datarecovery.bean.ConstObject.privacyPinPassword
import com.base.datarecovery.databinding.ActivityPrivacySpaceBinding
import com.base.datarecovery.fragment.PrivacyPageFragment
import com.base.datarecovery.help.BaseActivity
import com.base.datarecovery.utils.BarUtils
import com.base.datarecovery.view.DialogViews.showExitFunctionDialog

/**
 * 隐私空间
 */
class PrivacySpaceActivity : BaseActivity<ActivityPrivacySpaceBinding>() {

    override val binding: ActivityPrivacySpaceBinding by lazy {
        ActivityPrivacySpaceBinding.inflate(layoutInflater)
    }
    private val photosFragment: PrivacyPageFragment by lazy {
        PrivacyPageFragment("Photos")
    }

    private val videosFragment: PrivacyPageFragment by lazy {
        PrivacyPageFragment("Videos")
    }
    private val fragments by lazy {
        mutableListOf(photosFragment, videosFragment)
    }


    @SuppressLint("SetTextI18n")
    override fun initView() {
        BarUtils.setStatusBarLightMode(this, true)
        BarUtils.setStatusBarColor(this, Color.TRANSPARENT)

        binding.viewpager2.run {
            adapter = object : FragmentStateAdapter(this@PrivacySpaceActivity) {
                override fun getItemCount(): Int {
                    return fragments.size
                }

                override fun createFragment(position: Int): Fragment {
                    return fragments[position]
                }
            }
        }
        binding.viewpager2.registerOnPageChangeCallback(object :
            ViewPager2.OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                if (position == 0) {
                    binding.llPhotos.isSelected = true
                    binding.llVideos.isSelected = false
                } else {
                    binding.llVideos.isSelected = true
                    binding.llPhotos.isSelected = false
                }
            }
        })
        if (privacyPinPassword.isEmpty()) {
            binding.tvUninstallTip.setTextColor(Color.parseColor("#FAB44B"))
            binding.tvGoOrSetting.text = "Setting"
        } else {
            binding.tvPinTip.visibility = View.GONE
            binding.tvGoOrSetting.text = "Got it"
        }
    }

    override fun initListener() {
        super.initListener()

        binding.tvGoOrSetting.setOnClickListener {
            if (privacyPinPassword.isEmpty()) {
                startActivity(Intent(this, PrivacyPinOneActivity::class.java))
            } else {
                binding.llTip.visibility = View.GONE
            }
        }
        binding.llPhotos.setOnClickListener {
            binding.viewpager2.currentItem = 0
        }
        binding.llVideos.setOnClickListener {
            binding.viewpager2.currentItem = 1
        }
        onBackPressedDispatcher.addCallback {
            showExitFunctionDialog {
                AdmobInterstitialUtils.showInterstitialAd(this@PrivacySpaceActivity) {
                    finishToMain()
                }
            }
        }
        binding.flFanhui.setOnClickListener {
            onBackPressedDispatcher.onBackPressed()
        }

    }

}