package com.develop.setting.init.ui

import android.content.Context
import android.os.Bundle
import android.os.PowerManager
import android.view.LayoutInflater
import com.alibaba.android.arouter.facade.annotation.Route
import com.bumptech.glide.Glide
import com.bumptech.glide.request.FutureTarget
import com.bumptech.glide.util.Executors
import com.develop.food.base.manager.wifi.WifiHelp
import com.develop.food.base.repo.CURRENT_USER_ID
import com.develop.food.base.repo.FoodDataProvider
import com.develop.food.base.router.Screens
import com.develop.food.base.ui.BaseActivity
import com.develop.food.base.utils.ThreadUtils
import com.develop.food.base.utils.TuyaUtils.context
import com.develop.food.base.widgets.CancelConfirmDialog
import com.develop.setting.R
import com.develop.setting.databinding.ActivityRestoreBinding
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.io.File


@Route(path = Screens.Setting.RESTORE)
class RestoreActivity : BaseActivity<ActivityRestoreBinding>() {
    private val recipesConfirmDialog by lazy {
        CancelConfirmDialog()
    }

    override fun createViewBinding(inflater: LayoutInflater): ActivityRestoreBinding {
        return ActivityRestoreBinding.inflate(layoutInflater)
    }

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


        binding.topBar.setTopBarTitle(getString(R.string.reset))
        binding.topBar.listenBackClick {
            finish()
        }

        binding.tvYea.setOnClickListener {

            var dailog = CancelConfirmDialog()
            dailog.title = getString(R.string.restore_confirm_tips)

            dailog.onDialogClickListener = object : CancelConfirmDialog.OnDialogClickListener {
                override fun onConfirm() {
                    recipesConfirmDialog.showDialog(supportFragmentManager, "delete_recipes")
                    dailog.dismiss()
                }

                override fun onCancel() {
                }

            }

            dailog.showDialog(supportFragmentManager, "confirm")


        }

        recipesConfirmDialog.title = getString(R.string.delete_old_recipes)
        recipesConfirmDialog.showCancel = true
        recipesConfirmDialog.showConfirm = true
        recipesConfirmDialog.onDialogClickListener =
            object : CancelConfirmDialog.OnDialogClickListener {
                override fun onConfirm() {
                    glideClear(true)
                }

                override fun onCancel() {
                    glideClear(false)
                }

            }

    }


    private fun glideClear(boolean: Boolean){
        //把glide缓存清除
        Glide.get(applicationContext).clearMemory()
        GlobalScope.launch(Dispatchers.IO) {
            Glide.get(applicationContext).clearDiskCache()
            withContext(Dispatchers.Main){
                delete(boolean)
            }
        }
    }
    private fun delete(delete: Boolean) {

        ThreadUtils.runOnWorkThread({

            //2024.01.09 是否要删除增量食谱包,删除的话，把包括本地的增量都删，不删的话，就保持原样
            if (delete) {
                FoodDataProvider.deleteAll()
            }
            val cacheDirectory = cacheDir
            WifiHelp.Build(this@RestoreActivity).build().removeAllWifi()
            FoodDataProvider.getUserDatabase().userInfoDao().apply {
                removeAllOnlineRecipe(CURRENT_USER_ID)
                removeAllFavouriteRecipe(CURRENT_USER_ID)
                removeAllHistoryRecipe(CURRENT_USER_ID)
            }
            if (delete){
                FoodDataProvider.getUserDatabase().devConfigDao().apply {
                    removeAllDevVersion()
                }
            }
            val applicationDirectory = cacheDirectory.parent?.let { File(it) }
            if (applicationDirectory?.exists() == true) {

                val files = applicationDirectory.listFiles() ?: emptyArray()
                for (file in files) {
                    if (!file.name.equals("lib")) {
                        //2024.01.09 是否要删除增量食谱包,删除的话，把包括本地的增量都删，不删的话，就保持原样
                        //databases目录放着db文件
                        if (file.name.equals("databases")) {
                            if (delete) {
                                com.blankj.utilcode.util.FileUtils.delete(file)
                            }
                        } else {
                            com.blankj.utilcode.util.FileUtils.delete(file)
                        }
                    }
                }
            }


//                        MmkvUtils.save()
            runOnUiThread {
                val pm = getSystemService(Context.POWER_SERVICE) as PowerManager
                pm.reboot("")
            }
        }
        )
    }
    fun  clearGlide(){

    }
}