package com.zxhyis.toolss.ui.home


import android.app.PendingIntent
import android.content.Intent
import android.content.pm.ShortcutInfo
import android.content.pm.ShortcutManager
import android.graphics.Color
import android.graphics.drawable.Icon
import android.net.Uri
import android.os.Build
import androidx.activity.OnBackPressedCallback
import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import com.blankj.utilcode.util.AppUtils
import com.blankj.utilcode.util.BarUtils
import com.zxhyis.toolss.R
import com.zxhyis.toolss.base.TopActivity
import com.zxhyis.toolss.databinding.ActivityMainBinding
import com.zxhyis.toolss.ui.dialog.ExitDialog
import com.zxhyis.toolss.ui.dialog.RateStarPop
import com.zxhyis.toolss.ui.page.PopExitWindow
import com.zxhyis.toolss.ui.page.notifaction.HistoryDatabase
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch


class MainActivity : TopActivity<ActivityMainBinding>() {
    private lateinit var f1: NewHomeFragment
    private lateinit var f2: ToolsFragment

    private var dialog: ExitDialog? = null


    override fun getViewBinding1(): ActivityMainBinding {
        return ActivityMainBinding.inflate(layoutInflater)
    }

    // private var ifPause = 0
    override fun onPause() {
        super.onPause()
        //  ifPause += 1
    }

    override fun onResume() {
        super.onResume()
        if (AppUtils.isAppForeground()) {
            lifecycleScope.launch(Dispatchers.Main) {
                delay(1000)
                RateStarPop.show(this@MainActivity)
            }
        }
    }

    private fun createShortcut() {
        // 快捷方式名称
        val shortcutName = "My Shortcut"

        // 快捷方式图标
        val shortcutIcon = Icon.createWithResource(this, R.mipmap.logo)

        // 快捷方式意图
        val shortcutIntent = Intent(Intent.ACTION_VIEW, Uri.parse("http://www.example.com"))
        shortcutIntent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK

        // 创建快捷方式
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val shortcutManager = getSystemService(ShortcutManager::class.java)
            if (shortcutManager.isRequestPinShortcutSupported) {
                val shortcut = ShortcutInfo.Builder(this, shortcutName)
                    .setIcon(shortcutIcon)
                    .setShortLabel("my shortcut")
                    .setIntent(shortcutIntent)
                    .build()

                // 回调Intent
                val pinnedShortcutCallbackIntent =
                    shortcutManager.createShortcutResultIntent(shortcut)

                // 快捷方式回调广播
                val successCallback =
                    PendingIntent.getBroadcast(
                        this, 0, pinnedShortcutCallbackIntent,
                        PendingIntent.FLAG_IMMUTABLE
                    )

                // 请求固定快捷方式
                shortcutManager.requestPinShortcut(shortcut, successCallback.intentSender)
            }
        } else {
            val addIntent = Intent()
            addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent)
            addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, shortcutName)
            addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON, shortcutIcon)

            // 添加快捷方式
            addIntent.action = "com.android.launcher.action.INSTALL_SHORTCUT"
            sendBroadcast(addIntent)
        }
    }

    override fun init() {
        BarUtils.setStatusBarLightMode(this, true)
        BarUtils.setStatusBarColor(this, Color.TRANSPARENT)
        //   vb.root.updatePadding(top = BarUtils.getStatusBarHeight())
        initBavAndVp2()
        HistoryDatabase.getInstance(this)?.historyDao()
//        createShortcut()
//        requestToPinWidget()

        onBackPressedDispatcher.addCallback(object : OnBackPressedCallback(true) {
            override fun handleOnBackPressed() {
                PopExitWindow.show(this@MainActivity)
            }
        })

    }

    private fun initBavAndVp2() {
        f1 = NewHomeFragment()
        f2 = ToolsFragment()
        val fList = listOf(f1, f2)

        //配置vp2
        vb.vp2.adapter = object : FragmentStateAdapter(this) {
            override fun getItemCount(): Int = fList.size

            override fun createFragment(position: Int): Fragment = fList[position]

        }

        vb.vp2.apply {
            isUserInputEnabled = false
            offscreenPageLimit = fList.size
            currentItem = 0
            registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
                override fun onPageSelected(position: Int) {
                    super.onPageSelected(position)
                    when (position) {
                        0 -> {
                            vb.bav.selectedItemId = R.id.bav1
                        }

                        1 -> {
                            vb.bav.selectedItemId = R.id.bav2
                        }
                    }
                }
            })
        }

        vb.bav.setOnItemSelectedListener { item ->
            when (item.itemId) {
                R.id.bav1 -> {
                    vb.vp2.setCurrentItem(0, false)
                    true
                }

                R.id.bav2 -> {
                    vb.vp2.setCurrentItem(1, false)
                    true
                }

                else -> false
            }
        }

    }

    override fun onDestroy() {
        super.onDestroy()
        if (dialog != null) {
            dialog?.dismiss()
        }
    }

//    override fun onPermissionsResult(isGranted: Boolean) {
//        if (isGranted) {
//            f2.getRenctImg()
//        } else {
//            f2.noPersion()
//        }
//    }
}