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.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.widgets.CancelConfirmDialog
import com.develop.setting.R
import com.develop.setting.databinding.ActivityRestoreBinding
import java.io.File

@Route(path = Screens.Setting.RESTORE)
class RestoreActivity : BaseActivity<ActivityRestoreBinding>() {
    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() {
                    ThreadUtils.runOnWorkThread({
                        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)
                        }
                        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")) {
                                    com.blankj.utilcode.util.FileUtils.delete(file)
                                }
                            }
                        }
                        runOnUiThread {
                            val pm = getSystemService(Context.POWER_SERVICE) as PowerManager
                            pm.reboot("")
                        }
                    })
                }

                override fun onCancel() {
                }

            }

            dailog.showDialog(supportFragmentManager,"confirm")


        }

    }
}